Skip to content

Commit 933c8cf

Browse files
author
Unity Technologies
committed
## [1.0.15] - 2023-07-27 ### Changed * Updated com.unity.entities dependency to 1.0.14 * Use of non required TempJob allocation and use Allocator.Temp instead. ### Fixed * Runtime EntityQuery leaks and reduce runtime memory pressure due to continuously allocating queries without disposing. * Reduced memory usage in Editor tests, by avoiding allocating queries continuously in hot paths.
1 parent b414c7e commit 933c8cf

39 files changed

+516
-555
lines changed

CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
# Changelog
22

33

4+
## [1.0.15] - 2023-07-27
5+
6+
### Changed
7+
8+
* Updated com.unity.entities dependency to 1.0.14
9+
* Use of non required TempJob allocation and use Allocator.Temp instead.
10+
11+
### Fixed
12+
13+
* Runtime EntityQuery leaks and reduce runtime memory pressure due to continuously allocating queries without disposing.
14+
* Reduced memory usage in Editor tests, by avoiding allocating queries continuously in hot paths.
15+
16+
417
## [1.0.12] - 2023-06-19
518

619
### Changed
@@ -37,6 +50,8 @@
3750

3851
* exceptions when NetworkRequestListen and/or. NetworkRequestConnect are handled and proper handling of multiple (erroneous) requests presents.
3952
* A problem with InterpolatedTick, going back and not recovering correctly in presence of large application, either the server or the client, stalls (i.e after loading).
53+
* `MultiplayerPlayModeWindow > Dump Packet Logs` now works more reliably, now works with NUnit tests, and dump files are named with more context.
54+
* Fixed bug in `GhostSendSystem` that caused it to not replicate ghosts when enabling packet dumps. `GhostValuesAreSerialized_WithPacketDumpsEnabled` test added.
4055

4156

4257
## [1.0.8] - 2023-04-17

Editor/MultiplayerPlayModeWindow.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -973,9 +973,8 @@ static void DisconnectSpecificClient(EntityManager entityManager, NetworkId netw
973973
{
974974
entityManager.CompleteAllTrackedJobs();
975975
using var activeConnectionsQuery = entityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>(), ComponentType.Exclude<NetworkStreamRequestDisconnect>());
976-
977-
using var entities = activeConnectionsQuery.ToEntityArray(Allocator.Temp);
978-
using var networkIds = activeConnectionsQuery.ToComponentDataArray<NetworkId>(Allocator.Temp);
976+
var entities = activeConnectionsQuery.ToEntityArray(Allocator.Temp);
977+
var networkIds = activeConnectionsQuery.ToComponentDataArray<NetworkId>(Allocator.Temp);
979978
for (var i = 0; i < entities.Length; i++)
980979
{
981980
if (networkIds[i].Value == networkId.Value)
@@ -990,8 +989,7 @@ static void DisconnectAllClients(EntityManager entityManager, NetworkStreamDisco
990989
{
991990
entityManager.CompleteAllTrackedJobs();
992991
using var activeConnectionsQuery = entityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>(), ComponentType.Exclude<NetworkStreamRequestDisconnect>());
993-
994-
using var entities = activeConnectionsQuery.ToEntityArray(Allocator.Temp);
992+
var entities = activeConnectionsQuery.ToEntityArray(Allocator.Temp);
995993
for (var i = 0; i < entities.Length; i++)
996994
{
997995
// TODO - Convert to batch when API supports 1 NetworkStreamRequestDisconnect for n entities.

Runtime/Analytics/NetCodeAnalyticsState.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static int GetPlayerCount()
2121

2222
public static uint GetUpdateLength(World world)
2323
{
24-
var query = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<GhostSendSystemAnalyticsData>());
24+
using var query = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<GhostSendSystemAnalyticsData>());
2525
if (query.CalculateEntityCount() != 1)
2626
{
2727
return 0;

Runtime/Authoring/Hybrid/GhostAuthoringComponentBaker.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,8 @@ partial class GhostAuthoringBakingSystem : SystemBase
199199

200200
protected override void OnCreate()
201201
{
202-
base.OnCreate();
203-
204202
// Query to get all the root entities baked before
205-
m_NoLongerBakedRootEntities = EntityManager.CreateEntityQuery(new EntityQueryDesc
203+
m_NoLongerBakedRootEntities = GetEntityQuery(new EntityQueryDesc
206204
{
207205
None = new []
208206
{
@@ -218,7 +216,7 @@ protected override void OnCreate()
218216
m_NoLongerBakedRootEntitiesMask = m_NoLongerBakedRootEntities.GetEntityQueryMask();
219217

220218
// Query to get all the root entities baked before
221-
m_GhostEntities = EntityManager.CreateEntityQuery(new EntityQueryDesc
219+
m_GhostEntities = GetEntityQuery(new EntityQueryDesc
222220
{
223221
All = new []
224222
{

Runtime/ClientServerWorld/ClientServerBootstrap.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ public void OnCreate(ref SystemState state)
560560
var world = World.All[i];
561561
if (world.IsClient())
562562
{
563-
var driver = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkStreamDriver>());
563+
using var driver = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkStreamDriver>());
564564
UnityEngine.Assertions.Assert.IsFalse(driver.IsEmpty);
565565
var driverData = driver.ToComponentDataArray<NetworkStreamDriver>(Allocator.Temp);
566566
UnityEngine.Assertions.Assert.IsTrue(driverData.Length == 1);

Runtime/Connection/DriverMigrationSystem.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,12 @@ public int StoreWorld(World sourceWorld)
102102

103103
driverMap.Add(ticket, default);
104104

105-
var driverSingletonQuery = sourceWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
105+
using var driverSingletonQuery = sourceWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
106106
ref var driverSingleton = ref driverSingletonQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW;
107107
driverSingletonQuery.CompleteDependency();
108-
driverSingletonQuery.Dispose();
109108
Store(driverSingleton.StoreMigrationState(), ticket);
110109

111-
var filter = sourceWorld.EntityManager.CreateEntityQuery(typeof(NetworkStreamConnection));
110+
using var filter = sourceWorld.EntityManager.CreateEntityQuery(typeof(NetworkStreamConnection));
112111
var backupWorld = new World(sourceWorld.Name, sourceWorld.Flags);
113112

114113
backupWorld.EntityManager.MoveEntitiesFrom(sourceWorld.EntityManager, filter);

Runtime/Connection/NetworkStreamDriver.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ public Entity Connect(EntityManager entityManager, NetworkEndpoint endpoint, Ent
166166
if (DriverStore.DriversCount != 1)
167167
throw new InvalidOperationException("Too many NetworkDriver created for the client. Only one NetworkDriver instance should exist");
168168
var builder = new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkSnapshotAck>();
169-
if (!entityManager.CreateEntityQuery(builder).IsEmpty)
169+
using var query = entityManager.CreateEntityQuery(builder);
170+
if (!query.IsEmpty)
170171
throw new InvalidOperationException("Connection to server already initiated, only one connection allowed at a time.");
171172
#endif
172173
#if UNITY_EDITOR || !UNITY_CLIENT

Runtime/Debug/NetCodeDebugConfig.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ internal partial struct DebugConnections : ISystem
4242
[BurstCompile]
4343
public void OnCreate(ref SystemState state)
4444
{
45-
m_ConnectionsQueryWithout = state.EntityManager.CreateEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkStreamConnection>().WithNone<EnablePacketLogging>());
46-
m_ConnectionsQueryWith = state.EntityManager.CreateEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkStreamConnection>().WithAll<EnablePacketLogging>());
45+
m_ConnectionsQueryWithout = state.GetEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkStreamConnection>().WithNone<EnablePacketLogging>());
46+
m_ConnectionsQueryWith = state.GetEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkStreamConnection>().WithAll<EnablePacketLogging>());
4747
}
4848

4949
public void OnUpdate(ref SystemState state)
@@ -78,13 +78,6 @@ public void OnUpdate(ref SystemState state)
7878
state.EntityManager.RemoveComponent<EnablePacketLogging>(m_ConnectionsQueryWith);
7979
}
8080
}
81-
82-
[BurstCompile]
83-
public void OnDestroy(ref SystemState state)
84-
{
85-
m_ConnectionsQueryWithout.Dispose();
86-
m_ConnectionsQueryWith.Dispose();
87-
}
8881
}
8982
#endif
9083
}

Runtime/Rpc/RpcCommandRequest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public partial class RpcCommandRequestSystemGroup : ComponentSystemGroup
8989
protected override void OnCreate()
9090
{
9191
base.OnCreate();
92-
m_Query = EntityManager.CreateEntityQuery(ComponentType.ReadOnly<SendRpcCommandRequest>());
92+
m_Query = GetEntityQuery(ComponentType.ReadOnly<SendRpcCommandRequest>());
9393
}
9494
protected override void OnUpdate()
9595
{

Runtime/Snapshot/GhostCollectionSystem.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ public void OnCreate(ref SystemState state)
182182

183183
entityQueryBuilder.Reset();
184184
entityQueryBuilder.WithAll<NetworkStreamInGame>();
185-
m_InGameQuery = state.EntityManager.CreateEntityQuery(entityQueryBuilder);
185+
m_InGameQuery = state.GetEntityQuery(entityQueryBuilder);
186186
entityQueryBuilder.Reset();
187187
entityQueryBuilder.WithAll<NetworkId>();
188-
m_AllConnectionsQuery = state.EntityManager.CreateEntityQuery(entityQueryBuilder);
188+
m_AllConnectionsQuery = state.GetEntityQuery(entityQueryBuilder);
189189
entityQueryBuilder.Reset();
190190
entityQueryBuilder.WithAll<Prefab, GhostType>().WithNone<GhostPrefabRuntimeStrip>();
191-
m_RegisterGhostTypesQuery = state.EntityManager.CreateEntityQuery(entityQueryBuilder);
191+
m_RegisterGhostTypesQuery = state.GetEntityQuery(entityQueryBuilder);
192192

193193
if (!SystemAPI.TryGetSingletonEntity<CodeGhostPrefab>(out m_CodePrefabSingleton))
194194
m_CodePrefabSingleton = state.EntityManager.CreateSingletonBuffer<CodeGhostPrefab>();
@@ -207,8 +207,6 @@ public void OnDestroy(ref SystemState state)
207207
m_AllComponentTypes.Dispose();
208208
if (m_StableHashToComponentTypeIndex.IsCreated)
209209
m_StableHashToComponentTypeIndex.Dispose();
210-
m_InGameQuery.Dispose();
211-
m_AllConnectionsQuery.Dispose();
212210
state.EntityManager.DestroyEntity(m_CollectionSingleton);
213211
#if UNITY_EDITOR || NETCODE_DEBUG
214212
m_PredictionErrorNames.Dispose();

Runtime/Snapshot/GhostDistancePartitioningSystem.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void OnCreate(ref SystemState state)
140140
state.RequireForUpdate<GhostDistanceData>();
141141
var builder = new EntityQueryBuilder(Allocator.Temp)
142142
.WithAll<GhostDistancePartitionShared, LocalTransform, GhostInstance>();
143-
m_DistancePartitionedEntitiesQuery = state.WorldUnmanaged.EntityManager.CreateEntityQuery(builder);
143+
m_DistancePartitionedEntitiesQuery = state.GetEntityQuery(builder);
144144
}
145145

146146
[BurstCompile]

Runtime/Snapshot/GhostPrefabCreation.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,8 @@ public static void ConvertToGhostPrefab(EntityManager entityManager, Entity pref
816816
var variants = new NativeArray<ulong>(allComponents.Length, Allocator.Temp);
817817

818818
// TODO - Consider changing the API to pass this as an arg.
819-
var collectionDataQuery = entityManager.CreateEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<GhostComponentSerializerCollectionData>());
819+
using var collectionDataQuery = entityManager.CreateEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<GhostComponentSerializerCollectionData>());
820820
var collectionData = collectionDataQuery.GetSingleton<GhostComponentSerializerCollectionData>();
821-
collectionDataQuery.Dispose();
822821

823822
#if ENABLE_UNITY_COLLECTIONS_CHECKS
824823
{
@@ -869,13 +868,12 @@ public static void ConvertToGhostPrefab(EntityManager entityManager, Entity pref
869868
FinalizePrefabComponents(config, entityManager, prefab, ghostType, linkedEntitiesArray,
870869
allComponents, componentCounts, target, prefabTypes);
871870

872-
var codePrefabQuery = entityManager.CreateEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<CodeGhostPrefab>());
871+
using var codePrefabQuery = entityManager.CreateEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<CodeGhostPrefab>());
873872
if (!codePrefabQuery.TryGetSingletonEntity<CodeGhostPrefab>(out var codePrefabSingleton))
874873
codePrefabSingleton = entityManager.CreateSingletonBuffer<CodeGhostPrefab>();
875874
var codePrefabs = entityManager.GetBuffer<CodeGhostPrefab>(codePrefabSingleton);
876-
codePrefabQuery.Dispose();
877875

878-
#if NETCODE_DEBUG
876+
#if NETCODE_DEBUG
879877
for (int i = 0; i < codePrefabs.Length; ++i)
880878
{
881879
if (entityManager.GetComponentData<GhostType>(codePrefabs[i].entity) == ghostType)

Runtime/Snapshot/GhostReceiveSystem.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public void OnCreate(ref SystemState state)
204204

205205
builder.Reset();
206206
builder.WithAll<SubSceneWithGhostClenup>();
207-
m_SubSceneQuery = state.EntityManager.CreateEntityQuery(builder);
207+
m_SubSceneQuery = state.GetEntityQuery(builder);
208208

209209
m_CompressionModel = StreamCompressionModel.Default;
210210

Runtime/Snapshot/GhostSendSystem.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public void OnCreate(ref SystemState state)
359359
};
360360
ghostSpawnQuery = state.GetEntityQuery(filterSpawn);
361361
ghostDespawnQuery = state.GetEntityQuery(filterDespawn);
362-
prespawnSharedComponents = state.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<SubSceneGhostComponentHash>());
362+
prespawnSharedComponents = state.GetEntityQuery(ComponentType.ReadOnly<SubSceneGhostComponentHash>());
363363

364364
m_FreeGhostIds = new NativeQueue<int>(Allocator.Persistent);
365365
m_AllocatedGhostIds = new NativeArray<int>(2, Allocator.Persistent);

Runtime/Stats/GhostStatsSystem.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void OpenDebugger()
2727
}
2828
#endif
2929

30-
struct GhostStatsToSend : IDisposable
30+
struct GhostStatsToSend
3131
{
3232
readonly public World managedWorld;
3333
public EntityQuery singletonQuery;
@@ -37,10 +37,6 @@ public GhostStatsToSend(World world, EntityQuery query)
3737
managedWorld = world;
3838
singletonQuery = query;
3939
}
40-
public void Dispose()
41-
{
42-
singletonQuery.Dispose();
43-
}
4440
}
4541
private DebugWebSocket m_Socket;
4642
private List<GhostStatsToSend> m_StatsCollections;
@@ -82,7 +78,6 @@ public void Update()
8278

8379
if (!collectionDataQry.HasSingleton<GhostStatsCollectionData>())
8480
{
85-
collectionDataQry.Dispose();
8681
continue;
8782
}
8883
SetStatIndex(collectionDataQry, m_StatsCollections.Count);

Tests/Editor/CommandBufferSerialization.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,10 @@ public void CommandDataBuffer_OwnerPredicted_InterpolatedClientes_ShouldNotRecei
277277
//Assign the owner on the respective clients. Client3 is passive (no entity)
278278
for(int i=0;i<2;++i)
279279
{
280-
var query = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(typeof(GhostOwner));
280+
using var query = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(typeof(GhostOwner));
281281
var entities = query.ToEntityArray(Allocator.Temp);
282282
var owners = query.ToComponentDataArray<GhostOwner>(Allocator.Temp);
283-
var connQuery = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(
284-
typeof(NetworkId));
283+
using var connQuery = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(typeof(NetworkId));
285284
var conn = connQuery.GetSingletonEntity();
286285
var networkId = connQuery.GetSingleton<NetworkId>();
287286
for(int e=0;e<entities.Length;++e)
@@ -299,7 +298,7 @@ public void CommandDataBuffer_OwnerPredicted_InterpolatedClientes_ShouldNotRecei
299298

300299
for (int i = 0; i < 3; ++i)
301300
{
302-
var query = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(typeof(GhostOwner));
301+
using var query = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(typeof(GhostOwner));
303302
var entities = query.ToEntityArray(Allocator.Temp);
304303
for (int e = 0; e < entities.Length; ++e)
305304
{
@@ -346,8 +345,9 @@ private static Entity SpawnEntityAndAssignOwnerOnServer(NetCodeTestWorld testWor
346345
var net1 = testWorld.TryGetSingletonEntity<NetworkId>(testWorld.ClientWorlds[clientOwner]);
347346
var netId1 = testWorld.ClientWorlds[clientOwner].EntityManager.GetComponentData<NetworkId>(net1);
348347

349-
var entities = testWorld.ServerWorld.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>())
350-
.ToEntityArray(Allocator.Temp);
348+
//TODO: dispose this
349+
using var entitiesQuery = testWorld.ServerWorld.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>());
350+
var entities = entitiesQuery.ToEntityArray(Allocator.Temp);
351351
testWorld.ServerWorld.EntityManager.SetComponentData(serverEnt, new GhostOwner {NetworkId = netId1.Value});
352352
testWorld.ServerWorld.EntityManager.SetComponentData(serverEnt, new GhostGen_IntStruct {IntValue = 1000});
353353
for (int i = 0; i < entities.Length; ++i)

Tests/Editor/CommandDataTests.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -350,17 +350,15 @@ public void MultipleAutoCommandTargetSendsData()
350350
for (int i = 0; i < 16; ++i)
351351
testWorld.Tick(deltaTime);
352352

353-
var query = testWorld.ClientWorlds[0].EntityManager.CreateEntityQuery(typeof(GhostOwner));
353+
using var query = testWorld.ClientWorlds[0].EntityManager.CreateEntityQuery(typeof(GhostOwner));
354354
var clientEnts = query.ToEntityArray(Allocator.Temp);
355355
Assert.AreEqual(2, clientEnts.Length);
356356
Assert.AreNotEqual(Entity.Null, clientEnts[0]);
357357
Assert.AreNotEqual(Entity.Null, clientEnts[1]);
358358
if (testWorld.ClientWorlds[0].EntityManager.GetComponentData<GhostInstance>(clientEnts[0]).ghostId != testWorld.ServerWorld.EntityManager.GetComponentData<GhostInstance>(serverEnt).ghostId)
359359
{
360360
// swap 0 and 1
361-
var temp = clientEnts[0];
362-
clientEnts[0] = clientEnts[1];
363-
clientEnts[1] = temp;
361+
(clientEnts[0], clientEnts[1]) = (clientEnts[1], clientEnts[0]);
364362
}
365363

366364
testWorld.ServerWorld.EntityManager.AddComponent<CommandDataTestsTickInput>(serverEnt);

0 commit comments

Comments
 (0)