Skip to content

Commit

Permalink
Added docs and fixed test.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Oct 21, 2024
1 parent d6999aa commit 4728be1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/Arch.Tests/WorldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,14 @@ public void CapacityTest()
public void EnsureCapacity()
{
using var world = World.Create();
var archetype = world.EnsureCapacity(_entityGroup, 11000);
var amount = 11_000;
var archetype = world.EnsureCapacity(_entityGroup, amount);

// Calculation for capacity
var calculatedChunkSize = Archetype.GetChunkSizeInBytesFor(archetype.MinimumAmountOfEntitiesPerChunk, _entityGroup);
var entityCapacityPerChunk = Archetype.GetEntityCountFor(calculatedChunkSize, _entityGroup);
var requiredEntityCapacity = Math.Ceiling((float)amount / entityCapacityPerChunk) * entityCapacityPerChunk;

var requiredEntityCapacity = Archetype.GetEntityCountFor(Archetype.GetByteCountFor(archetype.MinimumAmountOfEntitiesPerChunk, _entityGroup), _entityGroup);
That(world.Size, Is.EqualTo(0));
That(world.Capacity, Is.EqualTo(archetype.EntityCapacity));
That(archetype.EntityCount, Is.EqualTo(0));
Expand Down
10 changes: 6 additions & 4 deletions src/Arch/Core/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ internal Archetype(Signature signature)
Types = signature;

// Calculations
ChunkSizeInBytes = GetByteCountFor(MinimumAmountOfEntitiesPerChunk, signature);
ChunkSizeInBytes = GetChunkSizeInBytesFor(MinimumAmountOfEntitiesPerChunk, signature);
EntitiesPerChunk = GetEntityCountFor(ChunkSizeInBytes, signature);

// The bitmask/set
Expand Down Expand Up @@ -757,15 +757,17 @@ public sealed partial class Archetype
{

/// <summary>
/// Calculates how many bytes are needed to store the <see cref="entityAmount"/>.
/// Calculates the size of the memory in bytes required to store the number of <see cref="entityAmount"/>.
/// The <see cref="BaseSize"/> (L1 cache size) is taken into account and, if necessary, rounded up to a multiple of this to ensure maximum cache performance.
/// So if the number of <see cref="entityAmount"/> exceeds the <see cref="BaseSize"/> value, a multiple of this is used.
/// </summary>
/// <param name="entityAmount">The amount of entities.</param>
/// <param name="types">The component structure of the <see cref="Arch.Core.Entity"/>'s.</param>
/// <returns>The amount of bytes required to store the <see cref="Entity"/>s.</returns>
public unsafe static int GetByteCountFor(int entityAmount, Span<ComponentType> types)
public unsafe static int GetChunkSizeInBytesFor(int entityAmount, Span<ComponentType> types)
{
var entityBytes = (sizeof(Entity) + types.ToByteSize()) * entityAmount;
return (int)Math.Ceiling((float)entityBytes / BaseSize) * BaseSize;
return (int)Math.Ceiling((float)entityBytes / BaseSize) * BaseSize; // Calculates and rounds to a multiple of BaseSize to store the number of entities
}

/// <summary>
Expand Down

0 comments on commit 4728be1

Please sign in to comment.