From c1f9c05c48802b914a315201e87fbf2cae1763d9 Mon Sep 17 00:00:00 2001 From: soul Date: Fri, 23 Oct 2026 00:38:33 +0200 Subject: [PATCH] Entity.GetBehavior walks Equipment by index: the per-round targeting-system reads no longer allocate an enumerator --- Assets/Scripts/ServerShared/Entity.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index f98ef8c32..510e3a51d 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -1782,13 +1782,18 @@ public abstract class Entity Parent = null; } + // Indexed, not foreach: Equipment is a ReactiveCollection whose enumerator is heap-allocated, and every round a gun fires + // asks for the targeting system several times (rounds per step: hundreds of rounds a step must not allocate each). public T GetBehavior() where T : Behavior { - foreach (var equippedItem in Equipment) - if(equippedItem.Behaviors != null) - foreach (var behavior in equippedItem.Behaviors) - if (behavior is T b) - return b; + for (var i = 0; i < Equipment.Count; i++) + { + var behaviors = Equipment[i].Behaviors; + if (behaviors == null) continue; + foreach (var behavior in behaviors) + if (behavior is T b) + return b; + } return null; }