Reduce do_after allocations (#7439)

This commit is contained in:
metalgearsloth
2022-04-06 15:20:55 +10:00
committed by GitHub
parent 7dd794b338
commit c2867cd9e5

View File

@@ -175,9 +175,7 @@ namespace Content.Client.DoAfter
/// Mark a DoAfter as cancelled and show a cancellation graphic. /// Mark a DoAfter as cancelled and show a cancellation graphic.
/// </summary> /// </summary>
/// Actual removal is handled by DoAfterEntitySystem. /// Actual removal is handled by DoAfterEntitySystem.
/// <param name="id"></param> public void Cancel(DoAfterComponent component, byte id)
/// <param name="currentTime"></param>
public void Cancel(DoAfterComponent component, byte id, TimeSpan? currentTime = null)
{ {
foreach (var (_, cancelled) in component.CancelledDoAfters) foreach (var (_, cancelled) in component.CancelledDoAfters)
{ {
@@ -204,7 +202,7 @@ namespace Content.Client.DoAfter
return; return;
// ReSharper disable once ConvertToLocalFunction // ReSharper disable once ConvertToLocalFunction
var predicate = (EntityUid uid, (EntityUid compOwner, EntityUid? attachedEntity) data) var predicate = static (EntityUid uid, (EntityUid compOwner, EntityUid? attachedEntity) data)
=> uid == data.compOwner || uid == data.attachedEntity; => uid == data.compOwner || uid == data.attachedEntity;
var viewbox = _eyeManager.GetWorldViewport().Enlarged(2.0f); var viewbox = _eyeManager.GetWorldViewport().Enlarged(2.0f);
@@ -213,7 +211,7 @@ namespace Content.Client.DoAfter
foreach (var (comp, xform) in EntityManager.EntityQuery<DoAfterComponent, TransformComponent>(true)) foreach (var (comp, xform) in EntityManager.EntityQuery<DoAfterComponent, TransformComponent>(true))
{ {
var doAfters = comp.DoAfters.ToList(); var doAfters = comp.DoAfters;
var compPos = xform.MapPosition; var compPos = xform.MapPosition;
if (doAfters.Count == 0 || if (doAfters.Count == 0 ||
@@ -239,6 +237,7 @@ namespace Content.Client.DoAfter
Enable(comp); Enable(comp);
var userGrid = xform.Coordinates; var userGrid = xform.Coordinates;
var toRemove = new RemQueue<ClientDoAfter>();
// Check cancellations / finishes // Check cancellations / finishes
foreach (var (id, doAfter) in doAfters) foreach (var (id, doAfter) in doAfters)
@@ -248,7 +247,7 @@ namespace Content.Client.DoAfter
// If we've passed the final time (after the excess to show completion graphic) then remove. // If we've passed the final time (after the excess to show completion graphic) then remove.
if (elapsedTime > doAfter.Delay + ExcessTime) if (elapsedTime > doAfter.Delay + ExcessTime)
{ {
Remove(comp, doAfter); toRemove.Add(doAfter);
continue; continue;
} }
@@ -261,7 +260,7 @@ namespace Content.Client.DoAfter
{ {
if (!userGrid.InRange(EntityManager, doAfter.UserGrid, doAfter.MovementThreshold)) if (!userGrid.InRange(EntityManager, doAfter.UserGrid, doAfter.MovementThreshold))
{ {
Cancel(comp, id, currentTime); Cancel(comp, id);
continue; continue;
} }
} }
@@ -272,12 +271,17 @@ namespace Content.Client.DoAfter
!Transform(doAfter.Target.Value).Coordinates.InRange(EntityManager, doAfter.TargetGrid, !Transform(doAfter.Target.Value).Coordinates.InRange(EntityManager, doAfter.TargetGrid,
doAfter.MovementThreshold)) doAfter.MovementThreshold))
{ {
Cancel(comp, id, currentTime); Cancel(comp, id);
continue; continue;
} }
} }
} }
foreach (var doAfter in toRemove)
{
Remove(comp, doAfter);
}
var count = comp.CancelledDoAfters.Count; var count = comp.CancelledDoAfters.Count;
// Remove cancelled DoAfters after ExcessTime has elapsed // Remove cancelled DoAfters after ExcessTime has elapsed
for (var i = count - 1; i >= 0; i--) for (var i = count - 1; i >= 0; i--)