-
-
Notifications
You must be signed in to change notification settings - Fork 137
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
base: master
Are you sure you want to change the base?
Conversation
…ion bump (cherry picked from commit e1d948f)
Thanks! |
Ok, looks like fixing the component registry uncovered some more issues and patches. I will push another round of fixes now and comment on them |
// 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); |
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.
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); |
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.
The new code uses bit shifting to find the next pow2 size without any floating point arithmetic.
private Archetype GetOrCreateArchetypeByAddEdge(in ComponentType type, Archetype oldArchetype) | ||
{ | ||
Archetype archetype; | ||
var edgeIndex = type.Id - 1; |
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.
These -1 were compensating for the off-by-one error on ComponentRegistry.
The component registry was incrementing the size twice, causing the 0th element to be null. Now it only increments after assigning.
Also, the version on the csproj was mismatched, causing local builds to generate an incorrect nupkg version.