Fix AABB tree update/remove to prevent stale leaves, duplicate hits, and repeated-update crashes#15
Open
irondnb wants to merge 1 commit intocyberarm:masterfrom
Open
Fix AABB tree update/remove to prevent stale leaves, duplicate hits, and repeated-update crashes#15irondnb wants to merge 1 commit intocyberarm:masterfrom
irondnb wants to merge 1 commit intocyberarm:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While working on performance issues in my own Gosu games that use spatial partitioning, I used agentic AI to inspect the hot paths and bug-prone areas in collision structures. Since I’ve been happy with the results and have been following you, I wanted to check whether there were similar issues here as well.
This PR fixes two related problems in the AABB tree update path.
First,
AABBNode#remove_subtreewas not actually removing nodes in the normal case. Because of that,AABBTree#removewould leave stale leaves behind in the tree. WhenAABBTree#updateremoved and reinserted an object, the old leaf stayed in the structure and a new one was added. That caused duplicate search results and unnecessary tree growth over time.Second, after AABBTree#update reinserted the leaf, it did not restore the object-to-leaf entry in
@objects. That meant the first update could appear to work, but the second update for the same object failed because remove(object)returned nil, which then caused a crash when trying to assign
leaf.bounding_box.What was happening before
What this PR changes
AABBTree#updateso the reinserted leaf is put back into@objects.Performance:
In a small isolated reproduction, the broken version returned 2 hits for one moved object where the fixed version returned 1, and repeated search timing on that tiny case dropped from roughly 177.83 ms to 73.94 ms for 100k identical queries.