From 8cbc05840fb0608647de69a18eeb2b1570cdfad2 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Thu, 11 Jan 2024 04:31:32 +1100 Subject: [PATCH] Remove some unnecessary GetEntityQuery (#4823) --- .../Containers/SharedContainerSystem.cs | 11 +++--- .../GameObjects/Systems/EntityLookupSystem.cs | 35 ++++++++----------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/Robust.Shared/Containers/SharedContainerSystem.cs b/Robust.Shared/Containers/SharedContainerSystem.cs index 3b00009eb7..707f8edff8 100644 --- a/Robust.Shared/Containers/SharedContainerSystem.cs +++ b/Robust.Shared/Containers/SharedContainerSystem.cs @@ -25,6 +25,7 @@ namespace Robust.Shared.Containers [Dependency] private readonly SharedTransformSystem _transform = default!; [Dependency] private readonly SharedJointSystem _joint = default!; + private EntityQuery _managerQuery; private EntityQuery _gridQuery; private EntityQuery _mapQuery; protected EntityQuery MetaQuery; @@ -42,6 +43,7 @@ namespace Robust.Shared.Containers SubscribeLocalEvent(OnContainerGetState); SubscribeLocalEvent(OnContainerManagerRemove); + _managerQuery = GetEntityQuery(); _gridQuery = GetEntityQuery(); _mapQuery = GetEntityQuery(); MetaQuery = GetEntityQuery(); @@ -437,8 +439,7 @@ namespace Robust.Shared.Containers /// public bool TryGetOuterContainer(EntityUid uid, TransformComponent xform, [NotNullWhen(true)] out BaseContainer? container) { - var xformQuery = GetEntityQuery(); - return TryGetOuterContainer(uid, xform, out container, xformQuery); + return TryGetOuterContainer(uid, xform, out container, TransformQuery); } public bool TryGetOuterContainer(EntityUid uid, TransformComponent xform, @@ -449,15 +450,13 @@ namespace Robust.Shared.Containers if (!uid.IsValid()) return false; - var conQuery = GetEntityQuery(); - var metaQuery = GetEntityQuery(); var child = uid; var parent = xform.ParentUid; while (parent.IsValid()) { - if (((metaQuery.GetComponent(child).Flags & MetaDataFlags.InContainer) == MetaDataFlags.InContainer) && - conQuery.TryGetComponent(parent, out var conManager) && + if (((MetaQuery.GetComponent(child).Flags & MetaDataFlags.InContainer) == MetaDataFlags.InContainer) && + _managerQuery.TryGetComponent(parent, out var conManager) && TryGetContainingContainer(parent, child, out var parentContainer, conManager)) { container = parentContainer; diff --git a/Robust.Shared/GameObjects/Systems/EntityLookupSystem.cs b/Robust.Shared/GameObjects/Systems/EntityLookupSystem.cs index 7eeb59bee1..01afac3ac9 100644 --- a/Robust.Shared/GameObjects/Systems/EntityLookupSystem.cs +++ b/Robust.Shared/GameObjects/Systems/EntityLookupSystem.cs @@ -121,12 +121,11 @@ public sealed partial class EntityLookupSystem : EntitySystem SubscribeLocalEvent(OnMapChange); _transform.OnGlobalMoveEvent += OnMove; + EntityManager.EntityInitialized += OnEntityInit; SubscribeLocalEvent(OnBodyTypeChange); SubscribeLocalEvent(OnBodyStartup); SubscribeLocalEvent(OnPhysicsUpdate); - - EntityManager.EntityInitialized += OnEntityInit; } private void OnBodyStartup(EntityUid uid, PhysicsComponent component, ComponentStartup args) @@ -354,8 +353,7 @@ public sealed partial class EntityLookupSystem : EntitySystem private void AddPhysicsTree(EntityUid uid, EntityUid broadUid, BroadphaseComponent broadphase, TransformComponent xform, PhysicsComponent body, FixturesComponent fixtures) { - var xformQuery = GetEntityQuery(); - var broadphaseXform = xformQuery.GetComponent(broadUid); + var broadphaseXform = _xformQuery.GetComponent(broadUid); if (broadphaseXform.MapID == MapId.Nullspace) return; @@ -363,7 +361,7 @@ public sealed partial class EntityLookupSystem : EntitySystem if (!_mapQuery.TryGetComponent(broadphaseXform.MapUid, out var physMap)) throw new InvalidOperationException($"Physics Broadphase is missing physics map. {ToPrettyString(broadUid)}"); - AddOrUpdatePhysicsTree(uid, broadUid, broadphase, broadphaseXform, physMap, xform, body, fixtures, xformQuery); + AddOrUpdatePhysicsTree(uid, broadUid, broadphase, broadphaseXform, physMap, xform, body, fixtures); } private void AddOrUpdatePhysicsTree( @@ -374,8 +372,7 @@ public sealed partial class EntityLookupSystem : EntitySystem PhysicsMapComponent physicsMap, TransformComponent xform, PhysicsComponent body, - FixturesComponent manager, - EntityQuery xformQuery) + FixturesComponent manager) { DebugTools.Assert(!_container.IsEntityOrParentInContainer(body.Owner, null, xform)); DebugTools.Assert(xform.Broadphase == null || xform.Broadphase == new BroadphaseData(broadphase.Owner, physicsMap.Owner, body.CanCollide, body.BodyType == BodyType.Static)); @@ -385,7 +382,7 @@ public sealed partial class EntityLookupSystem : EntitySystem var tree = body.BodyType == BodyType.Static ? broadphase.StaticTree : broadphase.DynamicTree; // TOOD optimize this. This function iterates UP through parents, while we are currently iterating down. - var (worldPos, worldRot) = _transform.GetWorldPositionRotation(xform, xformQuery); + var (worldPos, worldRot) = _transform.GetWorldPositionRotation(xform); var mapTransform = new Transform(worldPos, worldRot); // TODO BROADPHASE PARENTING this just assumes local = world @@ -486,9 +483,6 @@ public sealed partial class EntityLookupSystem : EntitySystem return; // We need to recursively update the cached data and remove children from the move buffer - var xformQuery = GetEntityQuery(); - var fixturesQuery = GetEntityQuery(); - DebugTools.Assert(HasComp(args.Sender)); DebugTools.Assert(!newMap.IsValid() || HasComp(newMap)); DebugTools.Assert(!oldMap.IsValid() || HasComp(oldMap)); @@ -498,7 +492,7 @@ public sealed partial class EntityLookupSystem : EntitySystem foreach (var child in args.Component._children) { - RecursiveOnGridChangedMap(child, oldMap, newMap, oldBuffer, newBuffer, xformQuery, fixturesQuery); + RecursiveOnGridChangedMap(child, oldMap, newMap, oldBuffer, newBuffer); } } @@ -507,17 +501,14 @@ public sealed partial class EntityLookupSystem : EntitySystem EntityUid oldMap, EntityUid newMap, Dictionary? oldBuffer, - Dictionary? newBuffer, - EntityQuery xformQuery, - EntityQuery fixturesQuery) + Dictionary? newBuffer) { - if (!xformQuery.TryGetComponent(uid, out var xform)) + if (!_xformQuery.TryGetComponent(uid, out var xform)) return; - foreach (var child in xform._children) { - RecursiveOnGridChangedMap(child, oldMap, newMap, oldBuffer, newBuffer, xformQuery, fixturesQuery); + RecursiveOnGridChangedMap(child, oldMap, newMap, oldBuffer, newBuffer); } if (xform.Broadphase == null || !xform.Broadphase.Value.CanCollide) @@ -526,7 +517,7 @@ public sealed partial class EntityLookupSystem : EntitySystem DebugTools.Assert(_netMan.IsClient || !xform.Broadphase.Value.PhysicsMap.IsValid() || xform.Broadphase.Value.PhysicsMap == oldMap); xform.Broadphase = xform.Broadphase.Value with { PhysicsMap = newMap }; - if (!fixturesQuery.TryGetComponent(uid, out var fixtures)) + if (!_fixturesQuery.TryGetComponent(uid, out var fixtures)) return; if (oldBuffer != null) @@ -543,15 +534,17 @@ public sealed partial class EntityLookupSystem : EntitySystem // TODO PERFORMANCE // track world position while recursively iterating down through children. - var (worldPos, worldRot) = _transform.GetWorldPositionRotation(xform, xformQuery); + var (worldPos, worldRot) = _transform.GetWorldPositionRotation(xform); var mapTransform = new Transform(worldPos, worldRot); foreach (var fixture in fixtures.Fixtures.Values) + { for (var i = 0; i < fixture.ProxyCount; i++) { var proxy = fixture.Proxies[i]; newBuffer[proxy] = fixture.Shape.ComputeAABB(mapTransform, i); } + } } private void UpdateParent(EntityUid uid, TransformComponent xform, EntityUid oldParent) @@ -685,7 +678,7 @@ public sealed partial class EntityLookupSystem : EntitySystem } else { - AddOrUpdatePhysicsTree(uid, broadUid, broadphase, broadphaseXform, physicsMap, xform, body, _fixturesQuery.GetComponent(uid), _xformQuery); + AddOrUpdatePhysicsTree(uid, broadUid, broadphase, broadphaseXform, physicsMap, xform, body, _fixturesQuery.GetComponent(uid)); } if (xform.ChildCount == 0 || !recursive)