From ca30caec03b5f22f04d4f8fcaf8da2669a642146 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Wed, 26 Aug 2020 11:53:22 +1000 Subject: [PATCH] Recycler optimise (#1873) Recyclers only check for intersecting entities that have been triggered via collision instead of actively querying it every tick. This means they pretty much don't show up on the profiler when idle. Co-authored-by: Metal Gear Sloth --- .../Components/Recycling/RecyclerComponent.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/Content.Server/GameObjects/Components/Recycling/RecyclerComponent.cs b/Content.Server/GameObjects/Components/Recycling/RecyclerComponent.cs index 93229746f5..47285443dc 100644 --- a/Content.Server/GameObjects/Components/Recycling/RecyclerComponent.cs +++ b/Content.Server/GameObjects/Components/Recycling/RecyclerComponent.cs @@ -1,4 +1,5 @@ -using System.Diagnostics.CodeAnalysis; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using Content.Server.GameObjects.Components.Conveyor; using Content.Server.GameObjects.Components.Items.Storage; using Content.Server.GameObjects.Components.Mobs; @@ -31,6 +32,8 @@ namespace Content.Server.GameObjects.Components.Recycling [Dependency] private readonly IEntityManager _entityManager = default!; public override string Name => "Recycler"; + + private List _intersecting = new List(); /// /// Whether or not sentient beings will be recycled @@ -87,6 +90,11 @@ namespace Content.Server.GameObjects.Components.Recycling private void Recycle(IEntity entity) { + if (!_intersecting.Contains(entity)) + { + _intersecting.Add(entity); + } + // TODO: Prevent collision with recycled items if (CanGib(entity)) { @@ -166,16 +174,19 @@ namespace Content.Server.GameObjects.Components.Recycling { if (!CanRun()) { + _intersecting.Clear(); return; } - var intersecting = _entityManager.GetEntitiesIntersecting(Owner, true); var direction = Vector2.UnitX; - foreach (var entity in intersecting) + for (var i = _intersecting.Count - 1; i >= 0; i--) { - if (!CanMove(entity)) + var entity = _intersecting[i]; + + if (!CanMove(entity) || !_entityManager.IsIntersecting(Owner, entity)) { + _intersecting.RemoveAt(i); continue; }