Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.09k stars 429 forks source link

2.0.0-exp2 Distributed Authority has some undue GC every frame #2891

Open Yoraiz0r opened 1 month ago

Yoraiz0r commented 1 month ago

Description

In NGO 2.0.0-exp2, simply enabling "distributed authority" in the network manager's session mode results in garbage allocation every frame.

The issue occurs under NetworkSpawnManager.DeferredDespawnUpdate, due to closure of currentTick near the end of the method.

Reproduce Steps

  1. Create a blank scene with NGO version 2.0.0exp2
  2. Add a network manager, and set its session mode to "distributed authority"
  3. Enter playmode
  4. Open the profiler and witness that the GC per frame is "20"
  5. Exit playmode
  6. Change the network manager's session mode to "client server"
  7. Enter playmode
  8. Witness that GC per frame is "0"

Actual Outcome

GC per frame with distributed authority is 20

Expected Outcome

GC per frame with distributed authority is 0

Environment

Fixing this

Change the following code from this:

            var despawnObjects = DeferredDespawnObjects.Where((c) => c.TickToDespawn < currentTick).ToList();
            foreach (var deferredObjectEntry in despawnObjects)
            {

To this:

            foreach (var deferredObjectEntry in DeferredDespawnObjects)
            {
                if (deferredObjectEntry.TickToDespawn >= currentTick)
                {
                    continue;
                }