Skip to content

Commit

Permalink
Outsourced a condition to make adding entities faster.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Apr 17, 2024
1 parent f26c729 commit 7adff48
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Arch/Core/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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++;
Expand Down

0 comments on commit 7adff48

Please sign in to comment.