SnpM / LockstepFramework

Framework for lockstep RTS, TD, and MOBA games.
MIT License
1.39k stars 348 forks source link

GridManager causing massive GC allocation #148

Closed tombali closed 6 years ago

tombali commented 6 years ago

In clean LS clone...

1) Change grid setting in GridSettings class to public GridSettings() { Init(512, 512, FixedMath.Create(-256), FixedMath.Create(-256), true); }

2) Make test script with following code

using UnityEngine; using Lockstep;

public class Test : MonoBehaviour { private void Update() { if (Input.GetKeyDown(KeyCode.G)) { GridManager.Initialize(); } } }

3) Attach it to some object, hit play with profiler open. After invoking GridManager.Initialize();, wait for CPU spike in profiler. Click on the spike. You should see 72MB allocated to GC.

ls

SnpM commented 6 years ago

Hey T, thanks for the heads up.

I did some quick approximating and 512x512 GridNodes should be about 20MB. There might be a couple variables that can be weeded out but the grid system (and LS in general) is optimized for speed over memory.

There are also ScanNodes for quickily acquiring a target. Each ScanNode costs about 200B because of the dictionary of lists which results in another 40MB. Bumping GridManager.ScanResolution to 8 could save 30MB and may also improve performance. I will try to ditch dictionary for a more efficient collection (this could be a huge optimization for mem/perf).

There's roughly 10MB unaccounted for but that could be low level system/Unity stuff. ScanNode is most likely the worst perpetrator with its controller culling system.

On my phone atm but I'm really excited to tackle these optimizations. Thanks again for bringing up this concern.

SnpM commented 6 years ago

I dove in and fixed a lot things in ScanNode. There were 2 collections - one for iterating and another for keeping track of agents/buckets. I combined these and took out KeyValuePairs so now it should be much more memory and performance efficient.

I also looked into pathfinding to see if there were any variables I could get rid of for memory. I knicked off 2 bytes from GridNode and implemented an awesome pathfinding vector field/degree system for size-based pathfinding. Now it's scalable and I might look into using these degrees to improve the pathfinding heuristic.

I don't have the time to test it yet so I'm keeping it in develop for now.

tombali commented 6 years ago

Awesome, thank you.