Skip to content

Replace ChildBufferCache with a simpler stack allocator. - #25593

Merged
alice-i-cecile merged 3 commits into
bevyengine:mainfrom
ickshonpe:uistack-stack
Sep 8, 2026
Merged

Replace ChildBufferCache with a simpler stack allocator.#25593
alice-i-cecile merged 3 commits into
bevyengine:mainfrom
ickshonpe:uistack-stack

Conversation

@ickshonpe

Copy link
Copy Markdown
Contributor

Objective

Replace the ChildBufferCache used in ui_stack_system with a simpler stack allocator.

Solution

Instead of the vec of vecs, use a single vec stack allocator.

Testing

The stack module's tests all still pass.


Showcase

cargo run --example many_buttons --release --features="trace_tracy"
uistack

Instead of the vec of vecs, use a single vec stack allocator.
@ickshonpe ickshonpe added C-Performance A change motivated by improving speed, memory usage or compile times A-UI Graphical user interfaces, styles, layouts, and widgets D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 28, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in UI Aug 28, 2026
@ickshonpe ickshonpe added the C-Code-Quality A section of code that is hard to understand or change label Aug 28, 2026

@PPakalns PPakalns left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Using cnt, pop(), sort_by_key Reverse in some special cases could use even less memory, but probably in average case is just more complex code.

LGTM.

@PPakalns

PPakalns commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Using cnt, pop(), sort_by_key Reverse in some special cases could use even less memory, but probably in average case is just more complex code.

LGTM.

Just an optional idea:

One more optimization to avoid storing start, end on stack.

This function can be implemented as one while loop which at the start stores initial node_entity in child_buffer and then uses while let Some((entity, _)) = child_buffer.pop() to process nodes. When childrens are added, we use sort_by_key with Reverse wrapper helper type.

This would reduce amount of data stored on stack during recursion. But additional profiling is needed, maybe compiler already does some optimizations.

Recursive implementation of course is easier to understand than loop one.

@ickshonpe

Copy link
Copy Markdown
Contributor Author

Using cnt, pop(), sort_by_key Reverse in some special cases could use even less memory, but probably in average case is just more complex code.
LGTM.

Just an optional idea:

One more optimization to avoid storing start, end on stack.

This function can be implemented as one while loop which at the start stores initial node_entity in child_buffer and then uses while let Some((entity, _)) = child_buffer.pop() to process nodes. When childrens are added, we use sort_by_key with Reverse wrapper helper type.

This would reduce amount of data stored on stack during recursion. But additional profiling is needed, maybe compiler already does some optimizations.

Recursive implementation of course is easier to understand than loop one.

Not sure, the ordering needs to be stable and sorting in reverse might mess it up? Eliminating the recursion might be good though, I guess the stack could overflow if someone had a crazy deep tree for some reason.

@PPakalns

PPakalns commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Not sure, the ordering needs to be stable and sorting in reverse might mess it up?

Yes, it will probably mess it up. In that case additional reverse call would be needed.

@stuartparmenter stuartparmenter added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 8, 2026
@alice-i-cecile
alice-i-cecile added this pull request to the merge queue Sep 8, 2026
Merged via the queue into bevyengine:main with commit 793c91c Sep 8, 2026
40 checks passed
@github-project-automation github-project-automation Bot moved this from Needs SME Triage to Done in UI Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-UI Graphical user interfaces, styles, layouts, and widgets C-Code-Quality A section of code that is hard to understand or change C-Performance A change motivated by improving speed, memory usage or compile times D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants