[3.x] Fast child iteration in Node
, Spatial
, CanvasItem
#107702
+26
−25
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.
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 needlessERR_FAIL_INDEX
checks.Really inside loops in single threaded code, there is no good reason to use the
[]
operator inLocalVector
. 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:
Then the pointer may become invalid (as the vector may be moved in memory).
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)
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
children_lock
fromSpatial
as it is unused.