Pool PVS entity state collections (#6752)

This commit is contained in:
metalgearsloth
2026-07-14 16:48:50 +10:00
committed by GitHub
parent 3211b437c9
commit 205a8a6479
5 changed files with 55 additions and 7 deletions
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using Microsoft.Extensions.ObjectPool;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Utility;
using SharpZstd.Interop;
@@ -19,9 +20,15 @@ internal sealed partial class PvsSystem
private readonly ObjectPool<List<PvsIndex>> _entDataListPool
= new DefaultObjectPool<List<PvsIndex>>(new ListPolicy<PvsIndex>(), MaxVisPoolSize);
private readonly ObjectPool<List<ComponentChange>> _componentChangeListPool
= new DefaultObjectPool<List<ComponentChange>>(new ListPolicy<ComponentChange>(), MaxVisPoolSize);
private readonly ObjectPool<HashSet<EntityUid>> _uidSetPool
= new DefaultObjectPool<HashSet<EntityUid>>(new SetPolicy<EntityUid>(), MaxVisPoolSize);
private readonly ObjectPool<HashSet<ushort>> _netComponentSetPool
= new DefaultObjectPool<HashSet<ushort>>(new SetPolicy<ushort>(), MaxVisPoolSize);
private readonly ObjectPool<PvsChunk> _chunkPool =
new DefaultObjectPool<PvsChunk>(new PvsChunkPolicy(), 256);
@@ -63,4 +70,38 @@ internal sealed partial class PvsSystem
CompressionContext.Dispose();
}
}
private List<ComponentChange> GetComponentChangeList(int capacity)
{
var list = _componentChangeListPool.Get();
list.EnsureCapacity(capacity);
return list;
}
private HashSet<ushort> GetNetComponentSet()
{
return _netComponentSetPool.Get();
}
private void ReturnEntityState(EntityState state)
{
if (state.ComponentChanges.Value is List<ComponentChange> changes)
_componentChangeListPool.Return(changes);
if (state.NetComponents is { } netComps)
{
_netComponentSetPool.Return(netComps);
state.NetComponents = null;
}
}
internal void ClearSessionState(PvsSession session)
{
foreach (var state in session.States)
{
ReturnEntityState(state);
}
session.ClearState();
}
}