Skip to content

Apply SpriteList removals in one pass when many are removed - #2930

Merged
pvcraven merged 1 commit into
developmentfrom
perf/sprite-list-remove
Oct 6, 2026
Merged

pvcraven merged 1 commit into
developmentfrom
perf/sprite-list-remove

Conversation

@pvcraven

@pvcraven pvcraven commented Oct 6, 2026

Copy link
Copy Markdown
Member

Summary

Makes removing many sprites from a large SpriteList much 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(), which Sprite.remove_from_sprite_lists() and kill() use, has to find the sprite in the list. That's an O(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 leaves sprite_slot, its sprite_lists, and the spatial hash. So in, collisions, and sprite.sprite_lists see the removal immediately. The sprite and its buffer slot are recorded as pending.
  • sprite_list is now a property that applies pending removals before returning the list. Every SpriteList method reads it before using the index buffer, and _write_sprite_buffers_to_gpu() and append() also apply them explicitly. So the list is never read, changed or drawn with removals pending.
  • Applying removals: fewer than 8 are removed the old way, one at a time, so a few removals cost the same as before. With more, there's one pass: filterfalse over 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.
  • Behavior: draw order is kept, and no public API changes.

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:

Sprites Removed per frame Before After
10,000 100 4.5–5.4 0.9–1.0
10,000 500 20–21 1.7–1.9
41,000 20 4.4–4.8 3.1–3.6
41,000 100 19–20 3.5–3.8
41,000 500 101–109 5.5–5.7
166,000 20 25–29 15
166,000 100 122–129 15
166,000 500 629–659 17–18

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:

  • Order: removals keep the order of the remaining sprites.
  • Immediate effect: a removed sprite is gone from in, sprite_lists, and collisions right away, and removing it again raises ValueError.
  • Slot reuse: new sprites can reuse the removed sprites' buffer slots.
  • Adding back: a removed sprite added back before the list is used again (append and insert) ends up in the right place.
  • Other operations: pop (end and front), insert, swap, __setitem__, reverse, sort, shuffle and clear, each after pending removals.
  • Random rounds: 30 frames of random removals and additions stay consistent.
  • Drawing: removed sprites aren't drawn, checked with pixels.

These pass on development too, since the behavior is unchanged. Another test checks the change itself: removing 40 sprites does no list.index() searches. That one fails on development, 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

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>
@pvcraven
pvcraven merged commit 30286ef into development Oct 6, 2026
7 checks passed
@pvcraven
pvcraven deleted the perf/sprite-list-remove branch October 6, 2026 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant