Skip to content

Commit

Permalink
Several changes and performance improvements :)
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Jul 31, 2024
1 parent 22fd4fe commit e741cd5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
17 changes: 10 additions & 7 deletions src/Arch/Core/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,25 +260,28 @@ public int EntityCapacity
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool Add(Entity entity, out Slot slot)
{
// Fill chunk
// Storing stack variables to prevent multiple times acessing those fields.
ref var lastChunk = ref LastChunk;
var chunkCount = ChunkCount;

// Fill chunk
if (lastChunk.Size != lastChunk.Capacity)
{
slot.Index = lastChunk.Add(entity);
slot.ChunkIndex = ChunkCount - 1;
slot.ChunkIndex = chunkCount - 1;
EntityCount++;

return false;
}

// Chunk full? Use next allocated chunk
if (ChunkCount < ChunkCapacity )
if (chunkCount < ChunkCapacity)
{
ChunkCount++;
lastChunk = ref LastChunk;

slot.Index = lastChunk.Add(entity);
slot.ChunkIndex = ChunkCount - 1;
slot.ChunkIndex = chunkCount;
EntityCount++;

return false;
Expand All @@ -287,12 +290,12 @@ internal bool Add(Entity entity, out Slot slot)
// No more free allocated chunks? Create new chunk
var newChunk = new Chunk(EntitiesPerChunk, _componentIdToArrayIndex, Types);
slot.Index = newChunk.Add(entity);
slot.ChunkIndex = chunkCount;
EntityCount++;
slot.ChunkIndex = ChunkCount;

// Resize chunks & map entity
EnsureChunkCapacity(ChunkCount + 1);
Chunks[ChunkCount] = newChunk;
EnsureChunkCapacity(chunkCount + 1);
Chunks[chunkCount] = newChunk;

// Increase size
ChunkCount++;
Expand Down
11 changes: 7 additions & 4 deletions src/Arch/Core/Chunk.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using Arch.Core.Events;
using Arch.Core.Extensions;
using Arch.Core.Extensions.Internal;
Expand Down Expand Up @@ -87,10 +88,12 @@ internal Chunk(int capacity, int[] componentIdToArrayIndex, Span<ComponentType>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal int Add(Entity entity)
{
Entities[Size] = entity;
Size++;
// Stack variable faster than accessing 3 times the Size field.
var size = Size;
Entities[size] = entity;
Size = size + 1;

return Size - 1;
return size;
}

/// <summary>
Expand Down Expand Up @@ -171,7 +174,7 @@ public EntityComponents<T> GetRow<T>(int index)
[Pure]
public ref Entity Entity(int index)
{
return ref Entities.AsSpan()[index];
return ref Entities.DangerousGetReferenceAt(index);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Arch/Core/Utils/BitSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public bool Exclusive(BitSet other)
public Span<uint> AsSpan()
{
var max = (_highestBit / (BitSize + 1)) + 1;
return _bits.AsSpan(0, max);
return MemoryMarshal.CreateSpan(ref _bits[0], max);
}

/// <summary>
Expand All @@ -433,7 +433,7 @@ public Span<uint> AsSpan(Span<uint> span, bool zero = true)
span[index] = 0;
}

return span[..length];
return MemoryMarshal.CreateSpan(ref span[0], length);
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Arch/Core/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,9 @@ public bool Has<T>(Entity entity)
[Pure]
public ref T Get<T>(Entity entity)
{
var slot = EntityInfo.GetSlot(entity.Id);
var archetype = EntityInfo.GetArchetype(entity.Id);
var entitySlot = EntityInfo.GetEntitySlot(entity.Id);
var slot = entitySlot.Slot;
var archetype = entitySlot.Archetype;
return ref archetype.Get<T>(ref slot);
}

Expand Down Expand Up @@ -1411,8 +1412,7 @@ public void AddRange(Entity entity, Span<ComponentType> components)
/// Causes a structural change.
/// </remarks>
/// <param name="entity">The <see cref="Entity"/>.</param>
/// <param name="type">The <see cref="ComponentType"/> to remove from the the <see cref="Entity"/>.</param>
[SkipLocalsInit]
/// <param name="type">The <see cref="ComponentType"/> to remove from the <see cref="Entity"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[StructuralChange]
public void Remove(Entity entity, ComponentType type)
Expand Down

0 comments on commit e741cd5

Please sign in to comment.