Make various ValueList enumerators use spans (#5019)

* Make various ValueList enumerators use spans

* Remove reference to EntityEventBus.OrderedRegistration

---------

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
Leon Friedrich
2024-03-31 19:01:03 +13:00
committed by GitHub
parent fdc1de2430
commit 2b55d39e51
9 changed files with 96 additions and 13 deletions

View File

@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using Robust.Shared.Analyzers;
using Robust.Shared.Collections;
namespace Robust.Benchmarks.Collections;
[Virtual]
public class ValueListEnumerationBenchmarks
{
[Params(4, 16, 64)]
public int N { get; set; }
private sealed class Data(int i)
{
public readonly int I = i;
}
private ValueList<Data> _valueList;
private Data[] _array = default!;
[GlobalSetup]
public void Setup()
{
var list = new List<Data>(N);
for (var i = 0; i < N; i++)
{
list.Add(new(i));
}
_array = list.ToArray();
_valueList = new(list.ToArray());
}
[Benchmark]
public int ValueList()
{
var total = 0;
foreach (var ev in _valueList)
{
total += ev.I;
}
return total;
}
[Benchmark]
public int ValueListSpan()
{
var total = 0;
foreach (var ev in _valueList.Span)
{
total += ev.I;
}
return total;
}
[Benchmark]
public int Array()
{
var total = 0;
foreach (var ev in _array)
{
total += ev.I;
}
return total;
}
[Benchmark]
public int Span()
{
var total = 0;
foreach (var ev in _array.AsSpan())
{
total += ev.I;
}
return total;
}
}

View File

@@ -107,7 +107,7 @@ namespace Robust.Client.GameObjects
toDelete.Add(id);
}
foreach (var dead in toDelete)
foreach (var dead in toDelete.Span)
{
component.Containers.Remove(dead);
}
@@ -142,7 +142,7 @@ namespace Robust.Client.GameObjects
toRemove.Add(entity);
}
foreach (var entity in toRemove)
foreach (var entity in toRemove.Span)
{
Remove(
(entity, TransformQuery.GetComponent(entity), MetaQuery.GetComponent(entity)),
@@ -162,7 +162,7 @@ namespace Robust.Client.GameObjects
removedExpected.Add(netEntity);
}
foreach (var entityUid in removedExpected)
foreach (var entityUid in removedExpected.Span)
{
RemoveExpectedEntity(entityUid, out _);
}

View File

@@ -57,7 +57,7 @@ namespace Robust.Client.UserInterface
toRemove.Add(key);
}
foreach (var key in toRemove)
foreach (var key in toRemove.Span)
{
_playingAnimations.Remove(key);
AnimationCompleted?.Invoke(key);

View File

@@ -329,7 +329,7 @@ namespace Robust.Shared.GameObjects
ref Unit unitRef,
EventData subs)
{
foreach (var handler in subs.BroadcastRegistrations)
foreach (var handler in subs.BroadcastRegistrations.Span)
{
if ((handler.Mask & source) != 0)
handler.Handler(ref unitRef);

View File

@@ -13,7 +13,7 @@ namespace Robust.Shared.GameObjects
EventData sub,
ref ValueList<OrderedEventDispatch> found)
{
foreach (var handler in sub.BroadcastRegistrations)
foreach (var handler in sub.BroadcastRegistrations.Span)
{
if ((handler.Mask & source) != 0)
found.Add(new OrderedEventDispatch(handler.Handler, handler.Order));
@@ -44,7 +44,7 @@ namespace Robust.Shared.GameObjects
{
found.Sort(OrderedEventDispatchComparer.Instance);
foreach (var (handler, _) in found)
foreach (var (handler, _) in found.Span)
{
handler(ref eventArgs);
}

View File

@@ -67,7 +67,7 @@ public sealed partial class EntityLookupSystem
}
}
foreach (var uid in toAdd)
foreach (var uid in toAdd.Span)
{
intersecting.Add(uid);
}

View File

@@ -58,7 +58,7 @@ public sealed partial class EntityLookupSystem
}
}
foreach (var uid in toAdd)
foreach (var uid in toAdd.Span)
{
intersecting.Add(uid);
}

View File

@@ -180,7 +180,7 @@ namespace Robust.Shared.GameObjects
toRemove.Add((oldId, oldFixture));
}
foreach (var (id, fixture) in toRemove)
foreach (var (id, fixture) in toRemove.Span)
{
// TODO add a DestroyFixture() override that takes in a list.
// reduced broadphase lookups
@@ -194,7 +194,7 @@ namespace Robust.Shared.GameObjects
}
// Anything remaining is a new fixture (or at least, may have not serialized onto the chunk yet).
foreach (var (id, fixture) in newFixtures)
foreach (var (id, fixture) in newFixtures.Span)
{
var existingFixture = _fixtures.GetFixtureOrNull(uid, id, manager: manager);
// Check if it's the same (otherwise remove anyway).

View File

@@ -289,7 +289,7 @@ namespace Robust.Shared.Physics.Systems
// TODO add a DestroyFixture() override that takes in a list.
// reduced broadphase lookups
foreach (var (id, fixture) in toRemoveFixtures)
foreach (var (id, fixture) in toRemoveFixtures.Span)
{
computeProperties = true;
DestroyFixture(uid, id, fixture, false, physics, component);
@@ -298,7 +298,7 @@ namespace Robust.Shared.Physics.Systems
// TODO: We also still need event listeners for shapes (Probably need C# events)
// Or we could just make it so shapes can only be updated via fixturesystem which handles it
// automagically (friends or something?)
foreach (var (id, fixture) in toAddFixtures)
foreach (var (id, fixture) in toAddFixtures.Span)
{
computeProperties = true;
CreateFixture(uid, id, fixture, false, component, physics, xform);