diff --git a/src/Arch/Arch.csproj b/src/Arch/Arch.csproj index 35b5ce92..09fb2781 100644 --- a/src/Arch/Arch.csproj +++ b/src/Arch/Arch.csproj @@ -14,7 +14,7 @@ Arch Arch - 2.0.0 + 2.1.0 genaray Apache-2.0 A high performance c# net.7 and net.8 archetype based ECS ( Entity component system ). diff --git a/src/Arch/Buffer/SparseSet.cs b/src/Arch/Buffer/SparseSet.cs index 9920645d..fe433392 100644 --- a/src/Arch/Buffer/SparseSet.cs +++ b/src/Arch/Buffer/SparseSet.cs @@ -1,5 +1,5 @@ using Arch.Core; -using Arch.Core.Utils; +using Arch.Core.Extensions.Internal; namespace Arch.Buffer; @@ -88,9 +88,8 @@ public void Add(int index) if (index >= Capacity) { // Calculate new array size that fits the passed index - var amountOfMultiplications = (int)Math.Ceiling(Math.Log((index+1) / (float)Capacity, 2.0f)); - var newLength = (int)Math.Pow(2, amountOfMultiplications) * Capacity; - newLength = Math.Max(Capacity, newLength+1); + var newCapacity = MathExtensions.NextPowerOfTwo(index + 1); + var newLength = Math.Max(Capacity, newCapacity); // keep existing capacity if already larger // Resize entities array Array.Resize(ref Entities, newLength); diff --git a/src/Arch/Core/ComponentRegistry.cs b/src/Arch/Core/ComponentRegistry.cs index 83cfbe49..9d90f853 100644 --- a/src/Arch/Core/ComponentRegistry.cs +++ b/src/Arch/Core/ComponentRegistry.cs @@ -126,10 +126,9 @@ private static ComponentType Add(Type type, int typeSize) } // Register and assign component id - var id = Size + 1; - meta = new ComponentType(id, typeSize); + meta = new ComponentType(Size, typeSize); _typeToComponentType.Add(type, meta); - _types = _types.Add(id, type); + _types = _types.Add(Size, type); Size++; return meta; diff --git a/src/Arch/Core/Edges/World.Edges.cs b/src/Arch/Core/Edges/World.Edges.cs index b7839844..cb8c9be1 100644 --- a/src/Arch/Core/Edges/World.Edges.cs +++ b/src/Arch/Core/Edges/World.Edges.cs @@ -16,7 +16,7 @@ public partial class World private Archetype GetOrCreateArchetypeByAddEdge(in ComponentType type, Archetype oldArchetype) { Archetype archetype; - var edgeIndex = type.Id - 1; + var edgeIndex = type.Id; if (!oldArchetype.HasAddEdge(edgeIndex)) { @@ -42,7 +42,7 @@ private Archetype GetOrCreateArchetypeByAddEdge(in ComponentType type, Archetype private Archetype GetOrCreateArchetypeByRemoveEdge(in ComponentType type, Archetype oldArchetype) { Archetype archetype; - var edgeIndex = type.Id - 1; + var edgeIndex = type.Id; if (!oldArchetype.HasRemoveEdge(edgeIndex)) { diff --git a/src/Arch/Core/Extensions/Internal/MathExtensions.cs b/src/Arch/Core/Extensions/Internal/MathExtensions.cs index e57eab74..4b90214a 100644 --- a/src/Arch/Core/Extensions/Internal/MathExtensions.cs +++ b/src/Arch/Core/Extensions/Internal/MathExtensions.cs @@ -17,4 +17,22 @@ public static int Max(int a, int b) { return a - ((a - b) & ((a - b) >> 31)); } + + + public static int NextPowerOfTwo(int x) + { + if (x <= 1) + { + return 1; + } + + x--; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + x++; + return x; + } }