Skip to content

Commit

Permalink
Some comp query optimisations (#4203)
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth authored Jul 29, 2023
1 parent 7c3634f commit 161b187
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Robust.Server/Bql/BqlQuerySelector.Builtin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override IEnumerable<EntityUid> DoInitialSelection(IReadOnlyList<object>
}

return entityManager.GetAllComponents((Type) arguments[0], includePaused: true)
.Select(x => x.Owner);
.Select(x => x.Uid);
}
}

Expand Down
14 changes: 6 additions & 8 deletions Robust.Shared/GameObjects/EntityManager.Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,28 +1270,26 @@ public IEnumerable<T> EntityQuery<T>(bool includePaused = false) where T : IComp
#endregion

/// <inheritdoc />
public IEnumerable<IComponent> GetAllComponents(Type type, bool includePaused = false)
public IEnumerable<(EntityUid Uid, Component Component)> GetAllComponents(Type type, bool includePaused = false)
{
var comps = _entTraitDict[type];

if (includePaused)
{
foreach (var comp in comps.Values)
foreach (var (uid, comp) in comps)
{
if (comp.Deleted) continue;

yield return comp;
yield return (uid, comp);
}
}
else
{
var metaQuery = GetEntityQuery<MetaDataComponent>();

foreach (var comp in comps.Values)
foreach (var (uid, comp) in comps)
{
if (comp.Deleted || !metaQuery.TryGetComponent(comp.Owner, out var meta) || meta.EntityPaused) continue;
if (comp.Deleted || !_metaQuery.TryGetComponent(uid, out var meta) || meta.EntityPaused) continue;

yield return comp;
yield return (uid, comp);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Robust.Shared/GameObjects/IEntityManager.Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ EntityQueryEnumerator<TComp1, TComp2, TComp3, TComp4> EntityQueryEnumerator<TCom
/// <param name="type">A trait or component type to check for.</param>
/// <param name="includePaused"></param>
/// <returns>All components that are the specified type.</returns>
IEnumerable<IComponent> GetAllComponents(Type type, bool includePaused = false);
IEnumerable<(EntityUid Uid, Component Component)> GetAllComponents(Type type, bool includePaused = false);

/// <summary>
/// Culls all components from the collection that are marked as deleted. This needs to be called often.
Expand Down
26 changes: 19 additions & 7 deletions Robust.Shared/GameObjects/Systems/EntityLookup.Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ private bool AnyEntitiesIntersecting(EntityUid lookupUid,
tuple.found = true;
return false;
}, localAABB, (flags & LookupFlags.Approximate) != 0x0);

if (state.found)
return true;
}

if ((flags & LookupFlags.Static) != 0x0)
Expand All @@ -156,6 +159,9 @@ private bool AnyEntitiesIntersecting(EntityUid lookupUid,
tuple.found = true;
return false;
}, localAABB, (flags & LookupFlags.Approximate) != 0x0);

if (state.found)
return true;
}

if ((flags & LookupFlags.StaticSundries) == LookupFlags.StaticSundries)
Expand All @@ -168,6 +174,9 @@ private bool AnyEntitiesIntersecting(EntityUid lookupUid,
tuple.found = true;
return false;
}, localAABB, (flags & LookupFlags.Approximate) != 0x0);

if (state.found)
return true;
}

if ((flags & LookupFlags.Sundries) != 0x0)
Expand All @@ -182,10 +191,7 @@ private bool AnyEntitiesIntersecting(EntityUid lookupUid,
}, localAABB, (flags & LookupFlags.Approximate) != 0x0);
}

if (state.found)
return true;

return false;
return state.found;
}

private bool AnyEntitiesIntersecting(EntityUid lookupUid,
Expand All @@ -209,6 +215,9 @@ private bool AnyEntitiesIntersecting(EntityUid lookupUid,
}, localAABB, (flags & LookupFlags.Approximate) != 0x0);
}

if (state.found)
return true;

if ((flags & LookupFlags.Static) != 0x0)
{
lookup.StaticTree.QueryAabb(ref state, (ref (EntityUid? ignored, bool found) tuple, in FixtureProxy value) =>
Expand All @@ -221,6 +230,9 @@ private bool AnyEntitiesIntersecting(EntityUid lookupUid,
}, localAABB, (flags & LookupFlags.Approximate) != 0x0);
}

if (state.found)
return true;

if ((flags & LookupFlags.StaticSundries) == LookupFlags.StaticSundries)
{
lookup.StaticSundriesTree.QueryAabb(ref state, static (ref (EntityUid? ignored, bool found) tuple, in EntityUid value) =>
Expand All @@ -233,6 +245,9 @@ private bool AnyEntitiesIntersecting(EntityUid lookupUid,
}, localAABB, (flags & LookupFlags.Approximate) != 0x0);
}

if (state.found)
return true;

if ((flags & LookupFlags.Sundries) != 0x0)
{
lookup.SundriesTree.QueryAabb(ref state, static (ref (EntityUid? ignored, bool found) tuple, in EntityUid value) =>
Expand All @@ -245,9 +260,6 @@ private bool AnyEntitiesIntersecting(EntityUid lookupUid,
}, localAABB, (flags & LookupFlags.Approximate) != 0x0);
}

if (state.found)
return true;

return state.found;
}

Expand Down
Loading

0 comments on commit 161b187

Please sign in to comment.