Skip to content
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

Attempt to optimize SpriteList.remove() #2392

Open
einarf opened this issue Oct 8, 2024 · 1 comment
Open

Attempt to optimize SpriteList.remove() #2392

einarf opened this issue Oct 8, 2024 · 1 comment

Comments

@einarf
Copy link
Member

einarf commented Oct 8, 2024

Removing a sprite is O(N) worst case. It's because we can't track what index a sprite is in a spritelist or performance will tank. If there was some magical ordered set that solved the problem, that would be great.

One idea is to store sprites using the slot index instead of the rendering order and resolve the actual index during spritelist iteration. This means that removing sprite by reference it fast but the drawback is that removing by index will be slow. There might also be some overhead when iterating a spritelist due to native->python integer conversion when reading from the index buffer.

In addition we could add

  • __del__(self, index: int)
  • remove_at_index(self, index: int)
  • popleft(self)
  • swap(self, sprite_a, sprite_b)

In addition the index buffer is resized for every call to remove((). This is an array.array that could potentially be batch updated once before draw()

  • Is there a way in python we can remove N indices from this structure without re-allocating it N times?
  • A possible way could be using a transform feedback to prune the index buffer but this might put additional stress on the rendering if only one thing is removed per frame
  • Keeping dead entries in the index buffer marked with a sentinel value and purge periodically could also be a solution but this also adds more complexity for custom shaders
  • Worst case see how converting the array to a list, remove items and create a new array from that
@DragonMoffon
Copy link
Collaborator

Could we maybe use a pool instead?

When you add a sprite the active index shifts up by one.
then when a sprite is removed we swap the data from the last active sprite with the removed sprite and decrement the active index. It doesn't preserve order of data, but we could still preserve idx order with some finangling

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

No branches or pull requests

2 participants