perf: NetworkServer.Update avoid costly NetworkIdentity null check. Catch exception and log useful warning instead. This amounted to ~5% CPU in 4k monsters test before.

This commit is contained in:
vis2k 2020-10-08 14:40:58 +02:00
parent 31b07ae02f
commit 9bd1d92b8e

View File

@ -514,16 +514,23 @@ public static void Update()
// update all server objects
foreach (KeyValuePair<uint, NetworkIdentity> kvp in NetworkIdentity.spawned)
{
NetworkIdentity identity = kvp.Value;
if (identity != null)
// try to update.
// spawned might have null entries if the user made a mistake.
//
// IMPORTANT: do not check kvp.Value != null. this is way too
// costly due to Unity's custom null check
// instead, catch the exception if it happens.
// (see 4k benchmark).
try
{
identity.ServerUpdate();
kvp.Value.ServerUpdate();
}
else
{
// spawned list should have no null entries because we
// always call Remove in OnObjectDestroy everywhere.
logger.LogWarning("Found 'null' entry in spawned list for netId=" + kvp.Key + ". Please call NetworkServer.Destroy to destroy networked objects. Don't use GameObject.Destroy.");
// but if it does, we should let the user know how it happens
catch (NullReferenceException)
{
Debug.LogWarning("Found 'null' entry in spawned list for netId=" + kvp.Key + ". Please call NetworkServer.Destroy to destroy networked objects. Don't use GameObject.Destroy.");
}
}
}