Skip to content

[3.x] Spatial and CanvasItem children change to LocalVector #107480

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

Merged
merged 1 commit into from
Jun 18, 2025

Conversation

lawnjelly
Copy link
Member

@lawnjelly lawnjelly commented Jun 13, 2025

Spatial maintains its own list of Spatial-only children (separate from the Node), in order to (in theory) make child iteration faster for only spatial children. The same happens with CanvasItem and CanvasItem children.

The problem is that these lists are stored as a linked list, and there is no reason to require linked list. Linked lists are bad for cache.

Benchmark

benchmark_spatial_children.zip
before: 157 fps
after: 280 fps

The benchmark creates a large number of Spatial children to the root node, then rotates the root. This stresses the _propagate_transform_changed() which utilizes the children list.

Notes

  • This is actually a no brainer, I also tested removing the Spatial children list entirely and utilizing the Node children list instead with fast casting, but this PR is still faster (because of no need for cast checks).
  • Some NULL checks in the original were redundant, I don't think these pointers can be NULL given the code here.
  • Also applicable in 4.x.
  • I may end up being able to use this list for SceneTreeFTI now it is faster, instead of Node children. Not 100% on this yet, but worth looking into.
  • Have done the same for CanvasItem, the same applies.

@lawnjelly lawnjelly added this to the 3.7 milestone Jun 13, 2025
@lawnjelly lawnjelly requested a review from a team as a code owner June 13, 2025 07:41
@AThousandShips AThousandShips changed the title [3.x] Convert Spatial children from linked list to linear [3.x] Convert Spatial children from linked list to vector Jun 13, 2025
@lawnjelly lawnjelly force-pushed the spat_children_linear_list branch from 87b1be2 to b459fd5 Compare June 13, 2025 08:52
@lawnjelly lawnjelly requested a review from a team as a code owner June 13, 2025 08:52
@lawnjelly lawnjelly changed the title [3.x] Convert Spatial children from linked list to vector [3.x] Spatial and CanvasItem children change to LocalVector Jun 13, 2025
struct Data {
LocalVector<CanvasItem *> children;
uint32_t parent_child_id = UINT32_MAX;
} data;
Copy link
Member Author

Choose a reason for hiding this comment

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

We should likely move all CanvasItem data into a Data struct as we do with Spatial and Node, to prevent name conflicts and subtle bugs. But this is for a follow up.

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.

Makes sense to me. LocalVector should be a straight upgrade over List for this use-case.
Code looks good to me, just two comments on this.

@lawnjelly lawnjelly force-pushed the spat_children_linear_list branch 2 times, most recently from edb7252 to 97ed666 Compare June 18, 2025 04:37
Comment on lines 224 to 225
uint32_t c = data.index_in_parent;
LocalVector<Spatial *> &parent_children = data.parent->data.children;
Copy link
Member

Choose a reason for hiding this comment

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

Personally I prefer not creating aliases if they aren't performance critical or change semantical meaning, because then you'll have to remember less information reading the code top to bottom.
But that's not critical, feel free to leave as is if you prefer the other way.

@lawnjelly lawnjelly force-pushed the spat_children_linear_list branch from 97ed666 to 8e48d57 Compare June 18, 2025 12:22
@lawnjelly lawnjelly merged commit 7172b03 into godotengine:3.x Jun 18, 2025
14 checks passed
@lawnjelly
Copy link
Member Author

Thanks!

@lawnjelly lawnjelly deleted the spat_children_linear_list branch June 18, 2025 13:03
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