From 3227c3de368ea3a82e14c28a058b7c979bd35bf4 Mon Sep 17 00:00:00 2001 From: UkoeHB <37489173+UkoeHB@users.noreply.github.com> Date: Mon, 2 Sep 2024 12:01:59 -0500 Subject: [PATCH] Don't reallocate scale factors in measure_text_system (#14999) # Objective - Reuse allocation of `scale_factors` in `measure_text_system`. ## Solution - Move it to a `Local`. --- crates/bevy_ui/src/widget/text.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index 3cb0dbfbe3979..515dae7f05bf6 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -152,7 +152,9 @@ fn create_text_measure( /// is only able to detect that a `Text` component has changed and will regenerate the `Measure` on /// color changes. This can be expensive, particularly for large blocks of text, and the [`bypass_change_detection`](bevy_ecs::change_detection::DetectChangesMut::bypass_change_detection) /// method should be called when only changing the `Text`'s colors. +#[allow(clippy::too_many_arguments)] pub fn measure_text_system( + mut scale_factors_buffer: Local>, mut last_scale_factors: Local>, fonts: Res>, camera_query: Query<(Entity, &Camera)>, @@ -171,14 +173,14 @@ pub fn measure_text_system( >, mut text_pipeline: ResMut, ) { - let mut scale_factors: EntityHashMap = EntityHashMap::default(); + scale_factors_buffer.clear(); for (entity, text, content_size, text_flags, camera, mut buffer) in &mut text_query { let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_ui_camera.get()) else { continue; }; - let scale_factor = match scale_factors.entry(camera_entity) { + let scale_factor = match scale_factors_buffer.entry(camera_entity) { Entry::Occupied(entry) => *entry.get(), Entry::Vacant(entry) => *entry.insert( camera_query @@ -208,7 +210,7 @@ pub fn measure_text_system( ); } } - *last_scale_factors = scale_factors; + std::mem::swap(&mut *last_scale_factors, &mut *scale_factors_buffer); } #[allow(clippy::too_many_arguments)] @@ -284,6 +286,7 @@ fn queue_text( #[allow(clippy::too_many_arguments)] pub fn text_system( mut textures: ResMut>, + mut scale_factors_buffer: Local>, mut last_scale_factors: Local>, fonts: Res>, camera_query: Query<(Entity, &Camera)>, @@ -301,14 +304,14 @@ pub fn text_system( &mut CosmicBuffer, )>, ) { - let mut scale_factors: EntityHashMap = EntityHashMap::default(); + scale_factors_buffer.clear(); for (node, text, text_layout_info, text_flags, camera, mut buffer) in &mut text_query { let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_ui_camera.get()) else { continue; }; - let scale_factor = match scale_factors.entry(camera_entity) { + let scale_factor = match scale_factors_buffer.entry(camera_entity) { Entry::Occupied(entry) => *entry.get(), Entry::Vacant(entry) => *entry.insert( camera_query @@ -341,5 +344,5 @@ pub fn text_system( ); } } - *last_scale_factors = scale_factors; + std::mem::swap(&mut *last_scale_factors, &mut *scale_factors_buffer); }