Reduce atmos allocs a bunch (#7228)
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Content.Server.Atmos.Components;
|
||||||
|
using Robust.Shared.Map;
|
||||||
|
|
||||||
|
namespace Content.Server.Atmos.EntitySystems;
|
||||||
|
|
||||||
|
public struct AtmosObstructionEnumerator
|
||||||
|
{
|
||||||
|
private AnchoredEntitiesEnumerator _enumerator;
|
||||||
|
private EntityQuery<AirtightComponent> _query;
|
||||||
|
|
||||||
|
public AtmosObstructionEnumerator(AnchoredEntitiesEnumerator enumerator, EntityQuery<AirtightComponent> query)
|
||||||
|
{
|
||||||
|
_enumerator = enumerator;
|
||||||
|
_query = query;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext([NotNullWhen(true)] out AirtightComponent? airtight)
|
||||||
|
{
|
||||||
|
if (!_enumerator.MoveNext(out var uid))
|
||||||
|
{
|
||||||
|
airtight = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No rider, it makes it uglier.
|
||||||
|
// ReSharper disable once ConvertIfStatementToReturnStatement
|
||||||
|
if (!_query.TryGetComponent(uid.Value, out airtight))
|
||||||
|
{
|
||||||
|
// ReSharper disable once TailRecursiveCall
|
||||||
|
return MoveNext(out airtight);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -195,13 +195,25 @@ namespace Content.Server.Atmos.EntitySystems
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public IEnumerable<AirtightComponent> GetObstructingComponents(IMapGrid mapGrid, Vector2i tile)
|
public IEnumerable<AirtightComponent> GetObstructingComponents(IMapGrid mapGrid, Vector2i tile)
|
||||||
{
|
{
|
||||||
foreach (var uid in mapGrid.GetAnchoredEntities(tile))
|
var airQuery = GetEntityQuery<AirtightComponent>();
|
||||||
|
var enumerator = mapGrid.GetAnchoredEntitiesEnumerator(tile);
|
||||||
|
|
||||||
|
while (enumerator.MoveNext(out var uid))
|
||||||
{
|
{
|
||||||
if (TryComp<AirtightComponent>(uid, out var ac))
|
if (!airQuery.TryGetComponent(uid.Value, out var airtight)) continue;
|
||||||
yield return ac;
|
yield return airtight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AtmosObstructionEnumerator GetObstructingComponentsEnumerator(IMapGrid mapGrid, Vector2i tile)
|
||||||
|
{
|
||||||
|
var ancEnumerator = mapGrid.GetAnchoredEntitiesEnumerator(tile);
|
||||||
|
var airQuery = GetEntityQuery<AirtightComponent>();
|
||||||
|
|
||||||
|
var enumerator = new AtmosObstructionEnumerator(ancEnumerator, airQuery);
|
||||||
|
return enumerator;
|
||||||
|
}
|
||||||
|
|
||||||
private AtmosDirection GetBlockedDirections(IMapGrid mapGrid, Vector2i indices)
|
private AtmosDirection GetBlockedDirections(IMapGrid mapGrid, Vector2i indices)
|
||||||
{
|
{
|
||||||
var value = AtmosDirection.Invalid;
|
var value = AtmosDirection.Invalid;
|
||||||
@@ -717,7 +729,9 @@ namespace Content.Server.Atmos.EntitySystems
|
|||||||
{
|
{
|
||||||
var directions = AtmosDirection.Invalid;
|
var directions = AtmosDirection.Invalid;
|
||||||
|
|
||||||
foreach (var obstructingComponent in GetObstructingComponents(mapGrid, tile))
|
var enumerator = GetObstructingComponentsEnumerator(mapGrid, tile);
|
||||||
|
|
||||||
|
while (enumerator.MoveNext(out var obstructingComponent))
|
||||||
{
|
{
|
||||||
if (!obstructingComponent.AirBlocked)
|
if (!obstructingComponent.AirBlocked)
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user