From b9a805b352722b18d075147da1aa61916cb8b594 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Wed, 1 Mar 2023 03:05:29 +1100 Subject: [PATCH] Reduce some state handling allocs (#14301) --- Content.Shared/Doors/Systems/SharedDoorSystem.cs | 5 +++-- Content.Shared/Mobs/Systems/MobStateSystem.cs | 7 ++++++- Content.Shared/Mobs/Systems/MobThresholdSystem.cs | 12 +++++++++++- .../StepTrigger/Systems/StepTriggerSystem.cs | 14 ++++++++++---- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/Content.Shared/Doors/Systems/SharedDoorSystem.cs b/Content.Shared/Doors/Systems/SharedDoorSystem.cs index c8db06b792..bb590a32d9 100644 --- a/Content.Shared/Doors/Systems/SharedDoorSystem.cs +++ b/Content.Shared/Doors/Systems/SharedDoorSystem.cs @@ -108,9 +108,10 @@ public abstract class SharedDoorSystem : EntitySystem if (args.Current is not DoorComponentState state) return; - if (!door.CurrentlyCrushing.Equals(state.CurrentlyCrushing)) + if (!door.CurrentlyCrushing.SetEquals(state.CurrentlyCrushing)) { - door.CurrentlyCrushing = new(state.CurrentlyCrushing); + door.CurrentlyCrushing.Clear(); + door.CurrentlyCrushing.UnionWith(state.CurrentlyCrushing); } door.State = state.DoorState; diff --git a/Content.Shared/Mobs/Systems/MobStateSystem.cs b/Content.Shared/Mobs/Systems/MobStateSystem.cs index 4be7715a98..ad2de1c801 100644 --- a/Content.Shared/Mobs/Systems/MobStateSystem.cs +++ b/Content.Shared/Mobs/Systems/MobStateSystem.cs @@ -104,7 +104,12 @@ public partial class MobStateSystem : EntitySystem return; component.CurrentState = state.CurrentState; - component.AllowedStates = new HashSet(state.AllowedStates); + + if (!component.AllowedStates.SetEquals(state.AllowedStates)) + { + component.AllowedStates.Clear(); + component.AllowedStates.UnionWith(state.AllowedStates); + } } private void OnGetComponentState(EntityUid uid, MobStateComponent component, ref ComponentGetState args) diff --git a/Content.Shared/Mobs/Systems/MobThresholdSystem.cs b/Content.Shared/Mobs/Systems/MobThresholdSystem.cs index c84fd097d9..dabd998ebc 100644 --- a/Content.Shared/Mobs/Systems/MobThresholdSystem.cs +++ b/Content.Shared/Mobs/Systems/MobThresholdSystem.cs @@ -340,7 +340,17 @@ public sealed class MobThresholdSystem : EntitySystem if (args.Current is not MobThresholdComponentState state) return; - component.Thresholds = new SortedDictionary(state.Thresholds); + if (component.Thresholds.Count != state.Thresholds.Count || + !component.Thresholds.SequenceEqual(state.Thresholds)) + { + component.Thresholds.Clear(); + + foreach (var threshold in state.Thresholds) + { + component.Thresholds.Add(threshold.Key, threshold.Value); + } + } + component.CurrentThresholdState = state.CurrentThresholdState; } diff --git a/Content.Shared/StepTrigger/Systems/StepTriggerSystem.cs b/Content.Shared/StepTrigger/Systems/StepTriggerSystem.cs index 72415ed839..b6398ca21d 100644 --- a/Content.Shared/StepTrigger/Systems/StepTriggerSystem.cs +++ b/Content.Shared/StepTrigger/Systems/StepTriggerSystem.cs @@ -150,11 +150,17 @@ public sealed class StepTriggerSystem : EntitySystem component.IntersectRatio = state.IntersectRatio; component.Active = state.Active; - component.CurrentlySteppedOn.Clear(); - component.Colliding.Clear(); + if (!component.CurrentlySteppedOn.SetEquals(state.CurrentlySteppedOn)) + { + component.CurrentlySteppedOn.Clear(); + component.CurrentlySteppedOn.UnionWith(state.CurrentlySteppedOn); + } - component.CurrentlySteppedOn.UnionWith(state.CurrentlySteppedOn); - component.Colliding.UnionWith(state.Colliding); + if (!component.Colliding.SetEquals(state.Colliding)) + { + component.Colliding.Clear(); + component.Colliding.UnionWith(state.Colliding); + } if (component.Colliding.Count > 0) { -- 2.52.0