Repository navigation
Apply SpriteList removals in one pass when many are removed - #2930
Merged
Merged
Conversation
SpriteList.remove() searched the list for the sprite, so removing k sprites from a list of N cost O(N) each: removing 100 sprites from 41,000 took 19 ms, and 500 from 166,000 took 630 ms. The order has to be kept, since it's the drawing order, so each search is unavoidable. Now remove() does the cheap part right away (the sprite leaves the list's slot map, its sprite_lists, and the spatial hash) and records it. The sprite_list property applies the pending removals before the list is read, changed, or drawn: fewer than 8 the old way, more with one pass that filters the list and the index buffer. The removed sprites' buffer slots are freed then, since the index buffer used them until then. Removing 100 from 41,000 now takes 3.3 ms, and 500 from 166,000 takes 17 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
Makes removing many sprites from a large
SpriteListmuch faster. I found this while measuring a performance showcase demo. Removing a sprite is about 1 ms at 166,000 sprites, and about 0.25 ms at 41,000.The problem
SpriteList.remove(), whichSprite.remove_from_sprite_lists()andkill()use, has to find the sprite in the list. That's anO(N)search, about 80% of the time in a profile. Removing several sprites per frame, like every bullet that hit something, repeats that search for each one. The search itself already runs at C speed (about 6 ns per sprite), and draw order has to be kept, so no faster search is possible. What's possible is doing the work for many removals in one pass.The fix
remove()does the cheap part right away: the sprite leavessprite_slot, itssprite_lists, and the spatial hash. Soin, collisions, andsprite.sprite_listssee the removal immediately. The sprite and its buffer slot are recorded as pending.sprite_listis now a property that applies pending removals before returning the list. EverySpriteListmethod reads it before using the index buffer, and_write_sprite_buffers_to_gpu()andappend()also apply them explicitly. So the list is never read, changed or drawn with removals pending.filterfalseover the sprite list, and over the index buffer by the removed sprites' buffer slots. Those slots go back on the free list only then, since the index buffer used them until that point.Measurements
ms per frame for the removals, including the next
draw(), which applies them and uploads the index buffer. Each number is the median of 8 frames, minus a plain draw. Two runs each:With 1–5 removals per frame, the numbers match before and after within run-to-run noise. Those removals take the old path.
Tests
New
tests/unit/spritelist/test_spritelist_deferred_remove.py. The main tests run with 1, 7, 8 and 40 removals, which covers both sides of the threshold. Each one checks the list,len, indexing,index(),sprite_slot, the free slots, and the draw order read back from the GPU index buffer:in,sprite_lists, and collisions right away, and removing it again raisesValueError.pop(end and front),insert,swap,__setitem__,reverse,sort,shuffleandclear, each after pending removals.These pass on
developmenttoo, since the behavior is unchanged. Another test checks the change itself: removing 40 sprites does nolist.index()searches. That one fails ondevelopment, which does 40.Full suite: 1644 passed, no warnings. Ruff is clean, and mypy has no new errors.
The changelog has an entry under Unreleased → Misc Changes.
🤖 Generated with Claude Code