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);
...
The
SortedDictionary
being enumerated inNetworkComponentSerializers
is creating garbage every time the system runs that can be avoided with a minor change.Instead of using a
SortedDictionary
, we can use aList
andDictionary
and just sort theList
whenever a system is added or removed inRegister
andUnregister
.