Skip to content

Commit

Permalink
Fixed #173 now really.
Browse files Browse the repository at this point in the history
Might break some Arch.Extended libs? E.g. the System.SourceGenerator one?
  • Loading branch information
genaray committed Nov 23, 2023
1 parent 15fb43e commit d734018
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 119 deletions.
10 changes: 5 additions & 5 deletions src/Arch.Benchmarks/ArchetypeIterationBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void Setup()
[Benchmark]
public void IterationNormalTwoComponents()
{
var size = _globalArchetype.Size;
var size = _globalArchetype.ChunkCount;
var chunks = _globalArchetype.Chunks;

for (var chunkIndex = 0; chunkIndex < size; chunkIndex++)
Expand All @@ -63,7 +63,7 @@ public void IterationNormalTwoComponents()
[Benchmark]
public void IterationUnsafeAddTwoComponents()
{
var size = _globalArchetype.Size;
var size = _globalArchetype.ChunkCount;
ref var chunk = ref _globalArchetype.Chunks[0];

for (var chunkIndex = 0; chunkIndex < size; chunkIndex++)
Expand Down Expand Up @@ -92,7 +92,7 @@ public void IterationUnsafeAddTwoComponents()
public void IterationParallelUnsafeAdd()
{
// Partition the entire source array.
var rangePartitioner = Partitioner.Create(0, _globalArchetype.Size);
var rangePartitioner = Partitioner.Create(0, _globalArchetype.ChunkCount);
Parallel.ForEach(rangePartitioner, range =>
{
var start = range.Item1;
Expand Down Expand Up @@ -126,7 +126,7 @@ public void IterationParallelUnsafeAdd()
[Benchmark]
public void IterationNormalEntityTwoComponents()
{
var size = _globalArchetype.Size;
var size = _globalArchetype.ChunkCount;
var chunks = _globalArchetype.Chunks;

for (var chunkIndex = 0; chunkIndex < size; chunkIndex++)
Expand Down Expand Up @@ -154,7 +154,7 @@ public void IterationNormalEntityTwoComponents()
[Benchmark]
public void IterationUnsafeAddEntityTwoComponents()
{
var size = _globalArchetype.Size;
var size = _globalArchetype.ChunkCount;
ref var chunk = ref _globalArchetype.Chunks[0];

for (var chunkIndex = 0; chunkIndex < size; chunkIndex++)
Expand Down
14 changes: 7 additions & 7 deletions src/Arch.Benchmarks/ArchetypeIterationTechnqiquesBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void IterationNormalTwoComponents()
{
var chunks = _globalArchetype.Chunks;

for (var chunkIndex = 0; chunkIndex < _globalArchetype.Size; chunkIndex++)
for (var chunkIndex = 0; chunkIndex < _globalArchetype.ChunkCount; chunkIndex++)
{
ref var chunk = ref chunks[chunkIndex];
var transforms = chunk.GetArray<Transform>();
Expand All @@ -67,7 +67,7 @@ public void IterationUnsafeAddTwoComponents()
{
ref var chunk = ref _globalArchetype.Chunks[0];

for (var chunkIndex = 0; chunkIndex < _globalArchetype.Size; chunkIndex++)
for (var chunkIndex = 0; chunkIndex < _globalArchetype.ChunkCount; chunkIndex++)
{
ref var currentChunk = ref Unsafe.Add(ref chunk, chunkIndex);
var transforms = currentChunk.GetArray<Transform>();
Expand All @@ -92,7 +92,7 @@ public void IterationNormalTwoComponentsSpan()
{
var chunks = _globalArchetype.Chunks;

for (var chunkIndex = 0; chunkIndex < _globalArchetype.Size; chunkIndex++)
for (var chunkIndex = 0; chunkIndex < _globalArchetype.ChunkCount; chunkIndex++)
{
ref var chunk = ref chunks[chunkIndex];
var transforms = chunk.GetSpan<Transform>();
Expand All @@ -114,7 +114,7 @@ public void IterationUnsafeAddTwoComponentsSpan()
{
ref var chunk = ref _globalArchetype.Chunks[0];

for (var chunkIndex = 0; chunkIndex < _globalArchetype.Size; chunkIndex++)
for (var chunkIndex = 0; chunkIndex < _globalArchetype.ChunkCount; chunkIndex++)
{
ref var currentChunk = ref Unsafe.Add(ref chunk, chunkIndex);
var transforms = currentChunk.GetSpan<Transform>();
Expand All @@ -139,7 +139,7 @@ public void IterationUnsafeAddTwoComponentsSpan()
public void IterationBackwardsUnsafeAdd()
{
ref var chunk = ref _globalArchetype.Chunks[0];
for (var chunkIndex = 0; chunkIndex < _globalArchetype.Size; chunkIndex++)
for (var chunkIndex = 0; chunkIndex < _globalArchetype.ChunkCount; chunkIndex++)
{
ref var currentChunk = ref Unsafe.Add(ref chunk, chunkIndex);
var chunkSize = currentChunk.Size;
Expand All @@ -157,7 +157,7 @@ public void IterationBackwardsUnsafeAdd()
public void IterationBackwardsUnsafeSubstract()
{
ref var chunk = ref _globalArchetype.Chunks[0];
for (var chunkIndex = 0; chunkIndex < _globalArchetype.Size; chunkIndex++)
for (var chunkIndex = 0; chunkIndex < _globalArchetype.ChunkCount; chunkIndex++)
{
ref var currentChunk = ref Unsafe.Add(ref chunk, chunkIndex);
var chunkSize = currentChunk.Size;
Expand All @@ -175,7 +175,7 @@ public void IterationBackwardsUnsafeSubstract()
public void IterationBackwardsLoop()
{
ref var chunk = ref _globalArchetype.Chunks[0];
for (var chunkIndex = 0; chunkIndex < _globalArchetype.Size; chunkIndex++)
for (var chunkIndex = 0; chunkIndex < _globalArchetype.ChunkCount; chunkIndex++)
{
ref var currentChunk = ref Unsafe.Add(ref chunk, chunkIndex);
var chunkSize = currentChunk.Size;
Expand Down
3 changes: 2 additions & 1 deletion src/Arch.Benchmarks/QueryBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public void WorldEntityQuery()
[Benchmark]
public void EntityExtensionQuery()
{
/*
_world.Query(in _queryDescription, (Entity entity) =>
{
var refs = entity.Get<Transform, Velocity>();
refs.t0.X += refs.t1.X;
refs.t0.Y += refs.t1.Y;
});
});*/
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/Arch.SourceGen/Queries/AddWithQueryDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void AppendAddWithQueryDescription(this StringBuilder sb, int amou
foreach (var archetype in query.GetArchetypeIterator())
{
// Archetype with T shouldnt be skipped to prevent undefined behaviour.
if(archetype.Entities == 0 || archetype.Has<{{generics}}>())
if(archetype.EntityCount == 0 || archetype.Has<{{generics}}>())
{
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Arch.SourceGen/Queries/InlineParallelQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void AppendHpParallelQuery(this StringBuilder builder, int amount)
var query = Query(in description);
foreach (var archetype in query.GetArchetypeIterator())
{
var archetypeSize = archetype.Size;
var archetypeSize = archetype.ChunkCount;
var part = new RangePartitioner(Environment.ProcessorCount, archetypeSize);
foreach (var range in part)
{
Expand Down Expand Up @@ -87,7 +87,7 @@ public static void AppendHpeParallelQuery(this StringBuilder builder, int amount
var query = Query(in description);
foreach (var archetype in query.GetArchetypeIterator()) {

var archetypeSize = archetype.Size;
var archetypeSize = archetype.ChunkCount;
var part = new RangePartitioner(Environment.ProcessorCount, archetypeSize);
foreach (var range in part)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Arch.SourceGen/Queries/ParallelQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static StringBuilder AppendParallelQuery(this StringBuilder sb, int amoun
var query = Query(in description);
foreach (var archetype in query.GetArchetypeIterator()) {

var archetypeSize = archetype.Size;
var archetypeSize = archetype.ChunkCount;
var part = new RangePartitioner(Environment.ProcessorCount, archetypeSize);
foreach (var range in part)
{
Expand Down Expand Up @@ -88,7 +88,7 @@ public static StringBuilder AppendParallelEntityQuery(this StringBuilder sb, int
var query = Query(in description);
foreach (var archetype in query.GetArchetypeIterator())
{
var archetypeSize = archetype.Size;
var archetypeSize = archetype.ChunkCount;
var part = new RangePartitioner(Environment.ProcessorCount, archetypeSize);
foreach (var range in part)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Arch.SourceGen/Queries/RemoveWithQueryDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static void AppendRemoveWithQueryDescription(this StringBuilder sb, int a
foreach (var archetype in query.GetArchetypeIterator())
{
// Archetype without T shouldnt be skipped to prevent undefined behaviour.
if(archetype.Entities <= 0 || !archetype.Has<{{generics}}>())
if(archetype.EntityCount <= 0 || !archetype.Has<{{generics}}>())
{
continue;
}
Expand Down
24 changes: 12 additions & 12 deletions src/Arch.Tests/ArchetypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ public void CreateChunk()
archetype.Add(entity, out _);
}

That(archetype.Size, Is.EqualTo(1)); // Since we filled it with n entities, it must have one single chunk.
That(archetype.ChunkCount, Is.EqualTo(1)); // Since we filled it with n entities, it must have one single chunk.
}

/// <summary>
/// Checks if the <see cref="Archetype.ChunkSize"/> increases when <see cref="Entity"/>s and their components become too large.
/// Checks if the <see cref="Archetype.ChunkSizeInBytes"/> increases when <see cref="Entity"/>s and their components become too large.
/// </summary>
[Test]
public void ScaleChunkCapacity()
{
var archetype = new Archetype(_heavyGroup);
That(archetype.ChunkSize, Is.EqualTo(Archetype.BaseSize * 2)); // heavyGroup should be large enough to force the chunk to pick a 32KB chunk instead of 16KB
That(archetype.ChunkSizeInBytes, Is.EqualTo(Archetype.BaseSize * 2)); // heavyGroup should be large enough to force the chunk to pick a 32KB chunk instead of 16KB
}

/// <summary>
Expand All @@ -64,7 +64,7 @@ public void CreateMultipleChunk()
archetype.Add(entity, out _);
}

That(archetype.Size, Is.EqualTo(2));
That(archetype.ChunkCount, Is.EqualTo(2));
}

/// <summary>
Expand All @@ -83,8 +83,8 @@ public void Reserve()
archetype.Add(entity, out _);
}

That(archetype.Size, Is.EqualTo(10));
That(archetype.Capacity, Is.EqualTo(10));
That(archetype.ChunkCount, Is.EqualTo(10));
That(archetype.ChunkCapacity, Is.EqualTo(10));
}

/// <summary>
Expand All @@ -105,8 +105,8 @@ public void RemoveFromChunkWithReplacement()
var slot = new Slot(0, 0);
archetype.Remove(ref slot, out _);

That(archetype.Size, Is.EqualTo(2));
That(archetype.Capacity, Is.EqualTo(2));
That(archetype.ChunkCount, Is.EqualTo(2));
That(archetype.ChunkCapacity, Is.EqualTo(2));
That(archetype.Chunks[0].Size, Is.EqualTo(entities - 50));
That(archetype.Chunks[1].Size, Is.EqualTo(49));
That(archetype.Chunks[0].Entities[0].Id, Is.EqualTo(archetype.CalculateEntitiesPerChunk(_group) + 50 - 1)); // Last entity from second chunk now replaced the removed entity and is in the first chunk
Expand All @@ -130,8 +130,8 @@ public void RemoveChunk()
var slot = new Slot(0, 0);
archetype.Remove(ref slot, out _);

That(archetype.Size, Is.EqualTo(1));
That(archetype.Capacity, Is.EqualTo(2));
That(archetype.ChunkCount, Is.EqualTo(1));
That(archetype.ChunkCapacity, Is.EqualTo(2));
That(archetype.Chunks[0].Size, Is.EqualTo(entities - 1));
That(archetype.Chunks[0].Entities[0].Id, Is.EqualTo(archetype.CalculateEntitiesPerChunk(_group))); // Last entity from second chunk now replaced the removed entity and is in the first chunk
}
Expand Down Expand Up @@ -256,7 +256,7 @@ public void CopyTo([Values(1111,2222,3333)] int sourceAmount, [Values(1111,2222,
}

// Make sure that EVERY single entity was copied correctly
That(destination.Entities, Is.EqualTo(sourceAmount+destinationAmount));
That(destination.EntityCount, Is.EqualTo(sourceAmount+destinationAmount));
That(countedSourceItems, Is.EqualTo(sourceAmount));
That(countedDestinationItems, Is.EqualTo(destinationAmount));
}
Expand Down Expand Up @@ -301,7 +301,7 @@ public void CopyToShift([Values(1111,2222,3333)] int sourceAmount, [Values(1111,
Archetype.Copy(source, destination);
source.Clear();

That(destination.Entities, Is.EqualTo(sourceAmount+destinationAmount));
That(destination.EntityCount, Is.EqualTo(sourceAmount+destinationAmount));
That(source.Entity(ref sourceSlot), Is.EqualTo(destination.Entity(ref resultSlot))); // Make sure entities were copied correctly.
}
}
4 changes: 2 additions & 2 deletions src/Arch.Tests/EnumeratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public void QueryChunkEnumeration()
counter++;
}

var archetype1ChunkCount = _world.Archetypes[0].Size;
var archetype2ChunkCount = _world.Archetypes[1].Size;
var archetype1ChunkCount = _world.Archetypes[0].ChunkCount;
var archetype2ChunkCount = _world.Archetypes[1].ChunkCount;
That(counter, Is.EqualTo(archetype1ChunkCount + archetype2ChunkCount));
}
}
16 changes: 8 additions & 8 deletions src/Arch.Tests/WorldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public void DestroyAll()
}

That(_world.Size, Is.EqualTo(0));
That(_world.Archetypes[0].Size, Is.EqualTo(1));
That(_world.Archetypes[1].Size, Is.EqualTo(1));
That(_world.Archetypes[0].ChunkCount, Is.EqualTo(1));
That(_world.Archetypes[1].ChunkCount, Is.EqualTo(1));
}

/// <summary>
Expand Down Expand Up @@ -276,8 +276,8 @@ public void TrimExcess()
var archetype = world.Archetypes[0];
That(world.Size, Is.EqualTo(1));
That(world.Capacity, Is.EqualTo(archetype.EntitiesPerChunk));
That(archetype.Size, Is.EqualTo(1));
That(archetype.Capacity, Is.EqualTo(1));
That(archetype.ChunkCount, Is.EqualTo(1));
That(archetype.ChunkCapacity, Is.EqualTo(1));
}

/// <summary>
Expand All @@ -304,7 +304,7 @@ public void TrimExcessEmptyArchetypes()

var archetype = world.Archetypes[0];
That(world.Archetypes.Count, Is.EqualTo(1));
That(world.Capacity, Is.EqualTo(archetype.Size * archetype.EntitiesPerChunk));
That(world.Capacity, Is.EqualTo(archetype.ChunkCount * archetype.EntitiesPerChunk));
}

/// <summary>
Expand Down Expand Up @@ -547,7 +547,7 @@ public void Remove()
_world.Remove<Transform>(entity2);

That(_world.GetArchetype(entity2), Is.EqualTo(_world.GetArchetype(entity)));
That(_world.GetArchetype(entity).Size, Is.EqualTo(1));
That(_world.GetArchetype(entity).ChunkCount, Is.EqualTo(1));
That(_world.GetArchetype(entity).Chunks[0].Size, Is.EqualTo(2));
}

Expand Down Expand Up @@ -601,7 +601,7 @@ public void Remove_NonGeneric()
_world.RemoveRange(entity2, typeof(Transform));

That(_world.GetArchetype(entity2), Is.EqualTo(_world.GetArchetype(entity)));
That(_world.GetArchetype(entity).Size, Is.EqualTo(1));
That(_world.GetArchetype(entity).ChunkCount, Is.EqualTo(1));
That(_world.GetArchetype(entity).Chunks[0].Size, Is.EqualTo(2));
}

Expand Down Expand Up @@ -671,7 +671,7 @@ public void GeneratedRemove()
_world.Remove<Rotation, Ai>(entity2);

That(_world.GetArchetype(entity2), Is.EqualTo(_world.GetArchetype(entity)));
That(_world.GetArchetype(entity).Size, Is.EqualTo(1));
That(_world.GetArchetype(entity).ChunkCount, Is.EqualTo(1));
That(_world.GetArchetype(entity).Chunks[0].Size, Is.EqualTo(2));
}

Expand Down
Loading

0 comments on commit d734018

Please sign in to comment.