Skip to content

Commit

Permalink
com.unity.netcode@1.0.15
Browse files Browse the repository at this point in the history
## [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.
  • Loading branch information
Unity Technologies committed Jul 27, 2023
1 parent b414c7e commit 933c8cf
Show file tree
Hide file tree
Showing 39 changed files with 516 additions and 555 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# Changelog


## [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.0.12] - 2023-06-19

### Changed
Expand Down Expand Up @@ -37,6 +50,8 @@

* exceptions when NetworkRequestListen and/or. NetworkRequestConnect are handled and proper handling of multiple (erroneous) requests presents.
* 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).
* `MultiplayerPlayModeWindow > Dump Packet Logs` now works more reliably, now works with NUnit tests, and dump files are named with more context.
* Fixed bug in `GhostSendSystem` that caused it to not replicate ghosts when enabling packet dumps. `GhostValuesAreSerialized_WithPacketDumpsEnabled` test added.


## [1.0.8] - 2023-04-17
Expand Down
8 changes: 3 additions & 5 deletions Editor/MultiplayerPlayModeWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -973,9 +973,8 @@ static void DisconnectSpecificClient(EntityManager entityManager, NetworkId netw
{
entityManager.CompleteAllTrackedJobs();
using var activeConnectionsQuery = entityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>(), ComponentType.Exclude<NetworkStreamRequestDisconnect>());

using var entities = activeConnectionsQuery.ToEntityArray(Allocator.Temp);
using var networkIds = activeConnectionsQuery.ToComponentDataArray<NetworkId>(Allocator.Temp);
var entities = activeConnectionsQuery.ToEntityArray(Allocator.Temp);
var networkIds = activeConnectionsQuery.ToComponentDataArray<NetworkId>(Allocator.Temp);
for (var i = 0; i < entities.Length; i++)
{
if (networkIds[i].Value == networkId.Value)
Expand All @@ -990,8 +989,7 @@ static void DisconnectAllClients(EntityManager entityManager, NetworkStreamDisco
{
entityManager.CompleteAllTrackedJobs();
using var activeConnectionsQuery = entityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>(), ComponentType.Exclude<NetworkStreamRequestDisconnect>());

using var entities = activeConnectionsQuery.ToEntityArray(Allocator.Temp);
var entities = activeConnectionsQuery.ToEntityArray(Allocator.Temp);
for (var i = 0; i < entities.Length; i++)
{
// TODO - Convert to batch when API supports 1 NetworkStreamRequestDisconnect for n entities.
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Analytics/NetCodeAnalyticsState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static int GetPlayerCount()

public static uint GetUpdateLength(World world)
{
var query = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<GhostSendSystemAnalyticsData>());
using var query = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<GhostSendSystemAnalyticsData>());
if (query.CalculateEntityCount() != 1)
{
return 0;
Expand Down
6 changes: 2 additions & 4 deletions Runtime/Authoring/Hybrid/GhostAuthoringComponentBaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,8 @@ partial class GhostAuthoringBakingSystem : SystemBase

protected override void OnCreate()
{
base.OnCreate();

// Query to get all the root entities baked before
m_NoLongerBakedRootEntities = EntityManager.CreateEntityQuery(new EntityQueryDesc
m_NoLongerBakedRootEntities = GetEntityQuery(new EntityQueryDesc
{
None = new []
{
Expand All @@ -218,7 +216,7 @@ protected override void OnCreate()
m_NoLongerBakedRootEntitiesMask = m_NoLongerBakedRootEntities.GetEntityQueryMask();

// Query to get all the root entities baked before
m_GhostEntities = EntityManager.CreateEntityQuery(new EntityQueryDesc
m_GhostEntities = GetEntityQuery(new EntityQueryDesc
{
All = new []
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/ClientServerWorld/ClientServerBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public void OnCreate(ref SystemState state)
var world = World.All[i];
if (world.IsClient())
{
var driver = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkStreamDriver>());
using var driver = world.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkStreamDriver>());
UnityEngine.Assertions.Assert.IsFalse(driver.IsEmpty);
var driverData = driver.ToComponentDataArray<NetworkStreamDriver>(Allocator.Temp);
UnityEngine.Assertions.Assert.IsTrue(driverData.Length == 1);
Expand Down
5 changes: 2 additions & 3 deletions Runtime/Connection/DriverMigrationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,12 @@ public int StoreWorld(World sourceWorld)

driverMap.Add(ticket, default);

var driverSingletonQuery = sourceWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
using var driverSingletonQuery = sourceWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
ref var driverSingleton = ref driverSingletonQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW;
driverSingletonQuery.CompleteDependency();
driverSingletonQuery.Dispose();
Store(driverSingleton.StoreMigrationState(), ticket);

var filter = sourceWorld.EntityManager.CreateEntityQuery(typeof(NetworkStreamConnection));
using var filter = sourceWorld.EntityManager.CreateEntityQuery(typeof(NetworkStreamConnection));
var backupWorld = new World(sourceWorld.Name, sourceWorld.Flags);

backupWorld.EntityManager.MoveEntitiesFrom(sourceWorld.EntityManager, filter);
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Connection/NetworkStreamDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public Entity Connect(EntityManager entityManager, NetworkEndpoint endpoint, Ent
if (DriverStore.DriversCount != 1)
throw new InvalidOperationException("Too many NetworkDriver created for the client. Only one NetworkDriver instance should exist");
var builder = new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkSnapshotAck>();
if (!entityManager.CreateEntityQuery(builder).IsEmpty)
using var query = entityManager.CreateEntityQuery(builder);
if (!query.IsEmpty)
throw new InvalidOperationException("Connection to server already initiated, only one connection allowed at a time.");
#endif
#if UNITY_EDITOR || !UNITY_CLIENT
Expand Down
11 changes: 2 additions & 9 deletions Runtime/Debug/NetCodeDebugConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ internal partial struct DebugConnections : ISystem
[BurstCompile]
public void OnCreate(ref SystemState state)
{
m_ConnectionsQueryWithout = state.EntityManager.CreateEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkStreamConnection>().WithNone<EnablePacketLogging>());
m_ConnectionsQueryWith = state.EntityManager.CreateEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkStreamConnection>().WithAll<EnablePacketLogging>());
m_ConnectionsQueryWithout = state.GetEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkStreamConnection>().WithNone<EnablePacketLogging>());
m_ConnectionsQueryWith = state.GetEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkStreamConnection>().WithAll<EnablePacketLogging>());
}

public void OnUpdate(ref SystemState state)
Expand Down Expand Up @@ -78,13 +78,6 @@ public void OnUpdate(ref SystemState state)
state.EntityManager.RemoveComponent<EnablePacketLogging>(m_ConnectionsQueryWith);
}
}

[BurstCompile]
public void OnDestroy(ref SystemState state)
{
m_ConnectionsQueryWithout.Dispose();
m_ConnectionsQueryWith.Dispose();
}
}
#endif
}
2 changes: 1 addition & 1 deletion Runtime/Rpc/RpcCommandRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public partial class RpcCommandRequestSystemGroup : ComponentSystemGroup
protected override void OnCreate()
{
base.OnCreate();
m_Query = EntityManager.CreateEntityQuery(ComponentType.ReadOnly<SendRpcCommandRequest>());
m_Query = GetEntityQuery(ComponentType.ReadOnly<SendRpcCommandRequest>());
}
protected override void OnUpdate()
{
Expand Down
8 changes: 3 additions & 5 deletions Runtime/Snapshot/GhostCollectionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ public void OnCreate(ref SystemState state)

entityQueryBuilder.Reset();
entityQueryBuilder.WithAll<NetworkStreamInGame>();
m_InGameQuery = state.EntityManager.CreateEntityQuery(entityQueryBuilder);
m_InGameQuery = state.GetEntityQuery(entityQueryBuilder);
entityQueryBuilder.Reset();
entityQueryBuilder.WithAll<NetworkId>();
m_AllConnectionsQuery = state.EntityManager.CreateEntityQuery(entityQueryBuilder);
m_AllConnectionsQuery = state.GetEntityQuery(entityQueryBuilder);
entityQueryBuilder.Reset();
entityQueryBuilder.WithAll<Prefab, GhostType>().WithNone<GhostPrefabRuntimeStrip>();
m_RegisterGhostTypesQuery = state.EntityManager.CreateEntityQuery(entityQueryBuilder);
m_RegisterGhostTypesQuery = state.GetEntityQuery(entityQueryBuilder);

if (!SystemAPI.TryGetSingletonEntity<CodeGhostPrefab>(out m_CodePrefabSingleton))
m_CodePrefabSingleton = state.EntityManager.CreateSingletonBuffer<CodeGhostPrefab>();
Expand All @@ -207,8 +207,6 @@ public void OnDestroy(ref SystemState state)
m_AllComponentTypes.Dispose();
if (m_StableHashToComponentTypeIndex.IsCreated)
m_StableHashToComponentTypeIndex.Dispose();
m_InGameQuery.Dispose();
m_AllConnectionsQuery.Dispose();
state.EntityManager.DestroyEntity(m_CollectionSingleton);
#if UNITY_EDITOR || NETCODE_DEBUG
m_PredictionErrorNames.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Snapshot/GhostDistancePartitioningSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void OnCreate(ref SystemState state)
state.RequireForUpdate<GhostDistanceData>();
var builder = new EntityQueryBuilder(Allocator.Temp)
.WithAll<GhostDistancePartitionShared, LocalTransform, GhostInstance>();
m_DistancePartitionedEntitiesQuery = state.WorldUnmanaged.EntityManager.CreateEntityQuery(builder);
m_DistancePartitionedEntitiesQuery = state.GetEntityQuery(builder);
}

[BurstCompile]
Expand Down
8 changes: 3 additions & 5 deletions Runtime/Snapshot/GhostPrefabCreation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -816,9 +816,8 @@ public static void CollectAllComponents(EntityManager entityManager, NativeArray
var variants = new NativeArray<ulong>(allComponents.Length, Allocator.Temp);

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

#if ENABLE_UNITY_COLLECTIONS_CHECKS
{
Expand Down Expand Up @@ -869,13 +868,12 @@ public static void CollectAllComponents(EntityManager entityManager, NativeArray
FinalizePrefabComponents(config, entityManager, prefab, ghostType, linkedEntitiesArray,
allComponents, componentCounts, target, prefabTypes);

var codePrefabQuery = entityManager.CreateEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<CodeGhostPrefab>());
using var codePrefabQuery = entityManager.CreateEntityQuery(new EntityQueryBuilder(Allocator.Temp).WithAll<CodeGhostPrefab>());
if (!codePrefabQuery.TryGetSingletonEntity<CodeGhostPrefab>(out var codePrefabSingleton))
codePrefabSingleton = entityManager.CreateSingletonBuffer<CodeGhostPrefab>();
var codePrefabs = entityManager.GetBuffer<CodeGhostPrefab>(codePrefabSingleton);
codePrefabQuery.Dispose();

#if NETCODE_DEBUG
#if NETCODE_DEBUG
for (int i = 0; i < codePrefabs.Length; ++i)
{
if (entityManager.GetComponentData<GhostType>(codePrefabs[i].entity) == ghostType)
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Snapshot/GhostReceiveSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void OnCreate(ref SystemState state)

builder.Reset();
builder.WithAll<SubSceneWithGhostClenup>();
m_SubSceneQuery = state.EntityManager.CreateEntityQuery(builder);
m_SubSceneQuery = state.GetEntityQuery(builder);

m_CompressionModel = StreamCompressionModel.Default;

Expand Down
2 changes: 1 addition & 1 deletion Runtime/Snapshot/GhostSendSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void OnCreate(ref SystemState state)
};
ghostSpawnQuery = state.GetEntityQuery(filterSpawn);
ghostDespawnQuery = state.GetEntityQuery(filterDespawn);
prespawnSharedComponents = state.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<SubSceneGhostComponentHash>());
prespawnSharedComponents = state.GetEntityQuery(ComponentType.ReadOnly<SubSceneGhostComponentHash>());

m_FreeGhostIds = new NativeQueue<int>(Allocator.Persistent);
m_AllocatedGhostIds = new NativeArray<int>(2, Allocator.Persistent);
Expand Down
7 changes: 1 addition & 6 deletions Runtime/Stats/GhostStatsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void OpenDebugger()
}
#endif

struct GhostStatsToSend : IDisposable
struct GhostStatsToSend
{
readonly public World managedWorld;
public EntityQuery singletonQuery;
Expand All @@ -37,10 +37,6 @@ public GhostStatsToSend(World world, EntityQuery query)
managedWorld = world;
singletonQuery = query;
}
public void Dispose()
{
singletonQuery.Dispose();
}
}
private DebugWebSocket m_Socket;
private List<GhostStatsToSend> m_StatsCollections;
Expand Down Expand Up @@ -82,7 +78,6 @@ public void Update()

if (!collectionDataQry.HasSingleton<GhostStatsCollectionData>())
{
collectionDataQry.Dispose();
continue;
}
SetStatIndex(collectionDataQry, m_StatsCollections.Count);
Expand Down
12 changes: 6 additions & 6 deletions Tests/Editor/CommandBufferSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,10 @@ public void CommandDataBuffer_OwnerPredicted_InterpolatedClientes_ShouldNotRecei
//Assign the owner on the respective clients. Client3 is passive (no entity)
for(int i=0;i<2;++i)
{
var query = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(typeof(GhostOwner));
using var query = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(typeof(GhostOwner));
var entities = query.ToEntityArray(Allocator.Temp);
var owners = query.ToComponentDataArray<GhostOwner>(Allocator.Temp);
var connQuery = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(
typeof(NetworkId));
using var connQuery = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(typeof(NetworkId));
var conn = connQuery.GetSingletonEntity();
var networkId = connQuery.GetSingleton<NetworkId>();
for(int e=0;e<entities.Length;++e)
Expand All @@ -299,7 +298,7 @@ public void CommandDataBuffer_OwnerPredicted_InterpolatedClientes_ShouldNotRecei

for (int i = 0; i < 3; ++i)
{
var query = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(typeof(GhostOwner));
using var query = testWorld.ClientWorlds[i].EntityManager.CreateEntityQuery(typeof(GhostOwner));
var entities = query.ToEntityArray(Allocator.Temp);
for (int e = 0; e < entities.Length; ++e)
{
Expand Down Expand Up @@ -346,8 +345,9 @@ private static Entity SpawnEntityAndAssignOwnerOnServer(NetCodeTestWorld testWor
var net1 = testWorld.TryGetSingletonEntity<NetworkId>(testWorld.ClientWorlds[clientOwner]);
var netId1 = testWorld.ClientWorlds[clientOwner].EntityManager.GetComponentData<NetworkId>(net1);

var entities = testWorld.ServerWorld.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>())
.ToEntityArray(Allocator.Temp);
//TODO: dispose this
using var entitiesQuery = testWorld.ServerWorld.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<NetworkId>());
var entities = entitiesQuery.ToEntityArray(Allocator.Temp);
testWorld.ServerWorld.EntityManager.SetComponentData(serverEnt, new GhostOwner {NetworkId = netId1.Value});
testWorld.ServerWorld.EntityManager.SetComponentData(serverEnt, new GhostGen_IntStruct {IntValue = 1000});
for (int i = 0; i < entities.Length; ++i)
Expand Down
6 changes: 2 additions & 4 deletions Tests/Editor/CommandDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,15 @@ public void MultipleAutoCommandTargetSendsData()
for (int i = 0; i < 16; ++i)
testWorld.Tick(deltaTime);

var query = testWorld.ClientWorlds[0].EntityManager.CreateEntityQuery(typeof(GhostOwner));
using var query = testWorld.ClientWorlds[0].EntityManager.CreateEntityQuery(typeof(GhostOwner));
var clientEnts = query.ToEntityArray(Allocator.Temp);
Assert.AreEqual(2, clientEnts.Length);
Assert.AreNotEqual(Entity.Null, clientEnts[0]);
Assert.AreNotEqual(Entity.Null, clientEnts[1]);
if (testWorld.ClientWorlds[0].EntityManager.GetComponentData<GhostInstance>(clientEnts[0]).ghostId != testWorld.ServerWorld.EntityManager.GetComponentData<GhostInstance>(serverEnt).ghostId)
{
// swap 0 and 1
var temp = clientEnts[0];
clientEnts[0] = clientEnts[1];
clientEnts[1] = temp;
(clientEnts[0], clientEnts[1]) = (clientEnts[1], clientEnts[0]);
}

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

0 comments on commit 933c8cf

Please sign in to comment.