miwarnec / DOTSNET

Public DOTSNET issue tracker
20 stars 0 forks source link

SortedDictionary is creating easily avoided garbage #51

Open Ro4m3r opened 3 years ago

Ro4m3r commented 3 years ago

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);
...

miwarnec commented 2 years ago

thanks, will take a look!