Performance: Excess memory allocation in MyDataReceiver.UpdateBroadcastersInRange

Guest shared this bug 23 months ago
Solved

Game version: 1.200.032


Method: MyDataReceiver.UpdateBroadcastersInRange


This method always allocates new HashSets, posing a constant GC pressure:


HashSet<long> longSet1 = new HashSet<long>();
HashSet<long> longSet2 = new HashSet<long>();

It should use pooled objects and just clear them instead of allocating new ones.


Best is to use a per-thread array of instances: ThreadLocal<HashSet<long>[]>


For a working solution please see MyDataReceiverPatch in https://github.com/viktor-ferenczi/performance-improvements


Alternative would be to use a bucket pool: MyConcurrentBucketPool<HashSet<long>>


Do not add these instances as members to MyDataReceiver, because it would involve a lot of unnecessary objects, increasing the overall memory consumption and lowering cache locality.


Run this test world in DS to reproduce (OneDrive due to its size): https://1drv.ms/u/s!AqEgz8G_d8TSh8wPzLoPp_dQDBRb7g?e=eBhsuv

Replies (3)

photo
1

Hello, Viktor,

thanks for letting us know. This issue was reported internally.

Kind Regards

Keen Software House: QA Department

photo
1

I've checked your optimization in the public beta. While your new code uses only a single HashSet now, it still allocates it every time, which causes GC pressure. Please use pooled instances stored in a static ThreadLocal<HashSet<long>>. It is completely safe and allows for reusing the same already allocated memory every time this method is called. Just don't forget to clear the instance at the end of the method. I use this solution in the Performance Improvements plugin, it works perfectly.

photo
1

Hello, Engineers!


We're happy to inform you that the upcoming "203" update contains a fix for the bug you have reported. Thank you for taking your time to inform us about this issue and making Space Engineers better.


If you are still experiencing the bug on the new version, please let us know by commenting here or opening a new thread.


We are closing this thread as "Solved".


Kind Regards,

Keen Software House: QA Department

Leave a Comment
 
Attach a file