Skip to content

Commit

Permalink
Re-use detached list
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr committed Jan 12, 2025
1 parent d2ec872 commit 46ab308
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions Robust.Client/GameStates/ClientGameStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
using Robust.Shared.Profiling;
Expand Down Expand Up @@ -58,6 +57,7 @@ public sealed class ClientGameStateManager : IClientGameStateManager
private readonly List<(EntityUid, TransformComponent)> _queuedBroadphaseUpdates = new();
private readonly HashSet<EntityUid> _sorted = new();
private readonly List<NetEntity> _created = new();
private readonly List<NetEntity> _detached = new();

private readonly record struct StateData(
EntityUid Uid,
Expand Down Expand Up @@ -754,10 +754,9 @@ public IEnumerable<NetEntity> ApplyGameState(GameState curState, GameState? next
_config.TickProcessMessages();
}

List<NetEntity> detached;
using (_prof.Group("Entity"))
{
detached = ApplyEntityStates(curState, nextState);
ApplyEntityStates(curState, nextState);
}

using (_prof.Group("Player"))
Expand All @@ -767,13 +766,13 @@ public IEnumerable<NetEntity> ApplyGameState(GameState curState, GameState? next

using (_prof.Group("Callback"))
{
GameStateApplied?.Invoke(new GameStateAppliedArgs(curState, detached));
GameStateApplied?.Invoke(new GameStateAppliedArgs(curState, _detached));
}

return _created;
}

private List<NetEntity> ApplyEntityStates(GameState curState, GameState? nextState)
private void ApplyEntityStates(GameState curState, GameState? nextState)
{
var metas = _entities.GetEntityQuery<MetaDataComponent>();
var xforms = _entities.GetEntityQuery<TransformComponent>();
Expand Down Expand Up @@ -841,7 +840,7 @@ private List<NetEntity> ApplyEntityStates(GameState curState, GameState? nextSta
// Detach entities to null space
var containerSys = _entitySystemManager.GetEntitySystem<ContainerSystem>();
var lookupSys = _entitySystemManager.GetEntitySystem<EntityLookupSystem>();
var detached = ProcessPvsDeparture(curState.ToSequence, metas, xforms, xformSys, containerSys, lookupSys);
ProcessPvsDeparture(curState.ToSequence, metas, xforms, xformSys, containerSys, lookupSys);

// Check next state (AFTER having created new entities introduced in curstate)
if (nextState != null)
Expand Down Expand Up @@ -949,11 +948,9 @@ private List<NetEntity> ApplyEntityStates(GameState curState, GameState? nextSta

_prof.WriteValue("State Size", ProfData.Int32(curSpan.Length));
_prof.WriteValue("Entered PVS", ProfData.Int32(enteringPvs));

return detached;
}

private void ApplyEntState(StateData data, GameTick toTick)
private void ApplyEntState(in StateData data, GameTick toTick)
{
try
{
Expand Down Expand Up @@ -1054,6 +1051,8 @@ private void SortStates(Dictionary<EntityUid, StateData> toApply)
{
AddToSorted(ent, data, ref i);
}

DebugTools.AssertEqual(i, toApply.Count);
}

private void AddToSorted(EntityUid ent, in StateData data, ref int i)
Expand All @@ -1074,13 +1073,18 @@ private void EnsureParentsSorted(EntityUid ent, in StateData data, ref int i)
if (_toApply.TryGetValue(parent, out var parentData))
{
AddToSorted(parent, parentData, ref i);
// The above method will handle the rest of the transform hierarchy, so we can just return early.
return;
}

parent = _entities.TransformQuery.GetComponent(parent).ParentUid;
}
}

/// <summary>
/// Get the entity's parent in the game state that is being applies. I.e., if the state contains a new
/// transform state, get the parent from that. Otherwise, return the entity's current parent.
/// </summary>
private EntityUid GetStateParent(EntityUid uid, in StateData data)
{
// TODO GAME STATE
Expand Down Expand Up @@ -1227,9 +1231,10 @@ public void DetachImmediate(List<NetEntity> entities)
var containerSys = _entitySystemManager.GetEntitySystem<ContainerSystem>();
var lookupSys = _entitySystemManager.GetEntitySystem<EntityLookupSystem>();
Detach(GameTick.MaxValue, null, entities, metas, xforms, xformSys, containerSys, lookupSys);
_detached.Clear();
}

private List<NetEntity> ProcessPvsDeparture(
private void ProcessPvsDeparture(
GameTick toTick,
EntityQuery<MetaDataComponent> metas,
EntityQuery<TransformComponent> xforms,
Expand All @@ -1238,25 +1243,23 @@ private List<NetEntity> ProcessPvsDeparture(
EntityLookupSystem lookupSys)
{
var toDetach = _processor.GetEntitiesToDetach(toTick, _pvsDetachBudget);
var detached = new List<NetEntity>();

if (toDetach.Count == 0)
return detached;
return;

// TODO optimize
// If an entity is leaving PVS, so are all of its children. If we can preserve the hierarchy we can avoid
// things like container insertion and ejection.

using var _ = _prof.Group("Leave PVS");
detached.EnsureCapacity(toDetach.Count);

_detached.Clear();
foreach (var (tick, ents) in toDetach)
{
Detach(tick, toTick, ents, metas, xforms, xformSys, containerSys, lookupSys, detached);
Detach(tick, toTick, ents, metas, xforms, xformSys, containerSys, lookupSys);
}

_prof.WriteValue("Count", ProfData.Int32(detached.Count));
return detached;
_prof.WriteValue("Count", ProfData.Int32(_detached.Count));
}

private void Detach(GameTick maxTick,
Expand All @@ -1266,8 +1269,7 @@ private void Detach(GameTick maxTick,
EntityQuery<TransformComponent> xforms,
SharedTransformSystem xformSys,
ContainerSystem containerSys,
EntityLookupSystem lookupSys,
List<NetEntity>? detached = null)
EntityLookupSystem lookupSys)
{
foreach (var netEntity in entities)
{
Expand Down Expand Up @@ -1312,7 +1314,7 @@ private void Detach(GameTick maxTick,
containerSys.AddExpectedEntity(netEntity, container);
}

detached?.Add(netEntity);
_detached.Add(netEntity);
}
}

Expand Down

0 comments on commit 46ab308

Please sign in to comment.