From 7495d68b415149ec845b1c4fc1faaf7ab1397e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Wed, 16 Oct 2024 19:59:30 +0200 Subject: [PATCH] UI materials: don't reserve in loop when enough capacity (#15919) # Objective - UI materials reserve too much capacity in a vec: for every node in the transparent phase, it reserves enough memory to store all the nodes - Update #10437 ## Solution - Only reserve extra memory if there's not enough - Only reserve the needed memory, not more --- crates/bevy_ui/src/render/ui_material_pipeline.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/bevy_ui/src/render/ui_material_pipeline.rs b/crates/bevy_ui/src/render/ui_material_pipeline.rs index e9a3803ebb..8ca8d390f8 100644 --- a/crates/bevy_ui/src/render/ui_material_pipeline.rs +++ b/crates/bevy_ui/src/render/ui_material_pipeline.rs @@ -642,9 +642,11 @@ pub fn queue_ui_material_nodes( bind_group_data: material.key.clone(), }, ); - transparent_phase - .items - .reserve(extracted_uinodes.uinodes.len()); + if transparent_phase.items.capacity() < extracted_uinodes.uinodes.len() { + transparent_phase.items.reserve_exact( + extracted_uinodes.uinodes.len() - transparent_phase.items.capacity(), + ); + } transparent_phase.add(TransparentUi { draw_function, pipeline,