DOTSNET icon indicating copy to clipboard operation
DOTSNET copied to clipboard

SortedDictionary is creating easily avoided garbage

Open Ro4m3r opened this issue 4 years ago • 1 comments

The SortedDictionary being enumerated in NetworkComponentSerializers is creating garbage every time the system runs that can be avoided with a minor change.

Instead of using a SortedDictionary, we can use a List and Dictionary and just sort the List whenever a system is added or removed in Register and Unregister.

Dictionary<ushort, NetworkComponentSerializerBase> systems = 
     new Dictionary<ushort, NetworkComponentSerializerBase>();

List<ushort> keys = new List<ushort>();

public void Register<T>(NetworkComponentSerializer<T> system)
    where T : unmanaged, NetworkComponent
{
    systems[system.Key] = system;
    keys.Add(system.Key);
    keys.Sort();
}

public void Unregister<T>(NetworkComponentSerializer<T> system)
    where T : unmanaged, NetworkComponent
{
    systems.Remove(system.Key);
    keys.Remove(system.Key);
    keys.Sort();
}
...
foreach (var system in keys)
      systems[system].SerializeAll(currentWorld);

...

foreach (var system in keys)
      systems[system].DeserializeAll(currentWorld);
...

Ro4m3r avatar Jun 22 '21 23:06 Ro4m3r

thanks, will take a look!

miwarnec avatar Nov 18 '21 10:11 miwarnec