Excessive memory allocation in MyLargeTurretTargetingSystem
Game version: 1.200.032
Both server and client, but worse on multiplayer servers.
Problem: Memory allocations in frequently called methods of MyLargeTurretTargetingSystem.
How to reproduce:
- Download and extract the test world (OneDrive, due to its size):
- Load the test world into DS or client.
- Use dotTrace to capture a timeline snapshot for 10 seconds.
- Select .NET Memory Allocations in the Events filter.
- The turret targeting system methods listed below should be at or near the top of the hotspot list.
- UpdateVisibilityCache and the whole visibility cache logic
Please see the memory profiler screenshot attached, see the top 2 items listed.
- Do not use ArrayExtensions.EnsureCapacity if you don't need to keep the existing array items. Whenever it needs to "grow" the array, it creates a new one and copies all items over! We don't need it if the goal is only to reuse the array.
- Reuse collections as much as possible. Some of them reused, some others are missed and a new one is always allocated. It imposes GC pressure, causes slowness and lag.
- Do not use ConcurrentDictionary, it has horrible performance. You are way better off by just using a simple Dictionary<K, V> generic type and lock the whole collection on access.
Please see the related patches in the Performance Improvements plugin on how to fix the above.
Search for: MyLargeTurretTargetingSystemPatch