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

UI materials: don't reserve in loop when enough capacity #15919

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions crates/bevy_ui/src/render/ui_material_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,11 @@ pub fn queue_ui_material_nodes<M: UiMaterial>(
bind_group_data: material.key.clone(),
},
);
transparent_phase
.items
.reserve(extracted_uinodes.uinodes.len());
if transparent_phase.items.capacity() < extracted_uinodes.uinodes.len() {
Copy link
Contributor

Choose a reason for hiding this comment

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

The transparent phase items vec is shared between all the extracted UI items, not just uimaterial nodes, so this isn't quite right either. Maybe the current length of items needs to be stored before the start of the loop, then this condition could become transparent_phase.items.capacity() - initial_len < extracted.uinodes.len()?

Copy link
Member Author

@mockersf mockersf Oct 15, 2024

Choose a reason for hiding this comment

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

so it seems we have no way to know the exact size required on each transparent phase, as not all nodes may go to the same if there are several cameras

Current behaviour on main is that for each entity, it's reserving "number of entities" capacity, so this PR is still an improvement
instead of:

  • for each entity
    • ensure phase can store "number of entities" more items <- allocate for every pass through the loop
    • push to the phase

it does:

  • for each entity
    • ensure once that phase can store "number of entities" more items <- allocate once
    • push to the phase <- allocate once capacity has been reached for every pass through the loop

Copy link
Contributor

@ickshonpe ickshonpe Oct 16, 2024

Choose a reason for hiding this comment

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

Yeah that makes sense. We can merge this and look for further improvements later.

transparent_phase.items.reserve_exact(
extracted_uinodes.uinodes.len() - transparent_phase.items.capacity(),
);
}
transparent_phase.add(TransparentUi {
draw_function,
pipeline,
Expand Down