Skip to content

[3.x] Fast child iteration in Node, Spatial, CanvasItem #107702

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

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from

Conversation

lawnjelly
Copy link
Member

@lawnjelly lawnjelly commented Jun 19, 2025

Now that these node types are using LocalVector for children, we have the opportunity to speed up child access in loops by using the unguarded version (ptr()[]) and saving needless ERR_FAIL_INDEX checks.

Really inside loops in single threaded code, there is no good reason to use the [] operator in LocalVector. We have already checked the size condition at the start of the loop, so it should never trigger.

ptr()[]

Using ptr()[] we have to be aware of a race condition where the number of elements is changed from another thread during the loop. It is NOT thread safe (but neither is the previous).

Node ** children = data.children.ptr()

We can also alternatively grab ptr() before the loop (and I've done that in a couple of hotspots).

There are a few possible gotchas to watch for:

  1. If the child vector changes during the iteration.
    Then the pointer may become invalid (as the vector may be moved in memory).
  2. If the number of children changes during iteration
    Getting the number of children each loop is safer, and in many cases the compiler should optimize out (although this may be worth revisiting at a later stage to make sure in a register)
  3. Multithread access
    Again if this happens, all bets are off. There is already no thread protection for these functions in 3.x, so no change in assumptions here.

Notes

  • I've also removed children_lock from Spatial as it is unused.

Copy link
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this is more of an indicator to use iterators instead.

Iterators use ptr iteration (or should at least, not sure where 3.x is at), and so are optimal on that front. But we don't need to do the manual 'ptr()' dance to avoid redundant index checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants