Repository navigation
Fix off-by-one error when registering components and add missing version bump #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new code uses bit shifting to find the next pow2 size without any floating point arithmetic. |
||
| var newLength = Math.Max(Capacity, newCapacity); // keep existing capacity if already larger | ||
|
|
||
| // Resize entities array | ||
| Array.Resize(ref Entities, newLength); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ public partial class World | |
| private Archetype GetOrCreateArchetypeByAddEdge(in ComponentType type, Archetype oldArchetype) | ||
| { | ||
| Archetype archetype; | ||
| var edgeIndex = type.Id - 1; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These -1 were compensating for the off-by-one error on ComponentRegistry. |
||
| 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)) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic was failing when trying to grow from capacity = 0 to anything different than capacity = 1.
Because capacity was being used as a denominator it was causing a division by 0 and amountOfMultiplications was undeflowing. Then the multiplication by Capacity neutralized that and made it 0 again, and newLength+1 made it work only for the case where the desired capacity was 1.