From 7adff48ab17e757472a630a42992b0fcd515f2e3 Mon Sep 17 00:00:00 2001 From: Lars Date: Wed, 17 Apr 2024 23:47:32 +0200 Subject: [PATCH] Outsourced a condition to make adding entities faster. --- src/Arch/Core/Archetype.cs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Arch/Core/Archetype.cs b/src/Arch/Core/Archetype.cs index 3da03481..82ea7b57 100644 --- a/src/Arch/Core/Archetype.cs +++ b/src/Arch/Core/Archetype.cs @@ -260,12 +260,8 @@ public int EntityCapacity [MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool Add(Entity entity, out Slot slot) { - // Increase size by one if the current chunk is full and theres capcity to prevent new chunk allocation. - ref var lastChunk = ref LastChunk; - ChunkCount = lastChunk.Size == lastChunk.Capacity && ChunkCount < ChunkCapacity ? ChunkCount + 1 : ChunkCount; - // Fill chunk - lastChunk = ref LastChunk; + ref var lastChunk = ref LastChunk; if (lastChunk.Size != lastChunk.Capacity) { slot.Index = lastChunk.Add(entity); @@ -275,7 +271,20 @@ internal bool Add(Entity entity, out Slot slot) return false; } - // Create new chunk + // Chunk full? Use next allocated chunk + if (ChunkCount < ChunkCapacity ) + { + ChunkCount++; + lastChunk = ref LastChunk; + + slot.Index = lastChunk.Add(entity); + slot.ChunkIndex = ChunkCount - 1; + EntityCount++; + + return false; + } + + // No more free allocated chunks? Create new chunk var newChunk = new Chunk(EntitiesPerChunk, _componentIdToArrayIndex, Types); slot.Index = newChunk.Add(entity); EntityCount++;