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`.
This commit is contained in:
UkoeHB 2024-09-02 12:01:59 -05:00 committed by GitHub
parent 2b94a108ae
commit 3227c3de36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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 /// 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) /// 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. /// method should be called when only changing the `Text`'s colors.
#[allow(clippy::too_many_arguments)]
pub fn measure_text_system( pub fn measure_text_system(
mut scale_factors_buffer: Local<EntityHashMap<f32>>,
mut last_scale_factors: Local<EntityHashMap<f32>>, mut last_scale_factors: Local<EntityHashMap<f32>>,
fonts: Res<Assets<Font>>, fonts: Res<Assets<Font>>,
camera_query: Query<(Entity, &Camera)>, camera_query: Query<(Entity, &Camera)>,
@ -171,14 +173,14 @@ pub fn measure_text_system(
>, >,
mut text_pipeline: ResMut<TextPipeline>, mut text_pipeline: ResMut<TextPipeline>,
) { ) {
let mut scale_factors: EntityHashMap<f32> = EntityHashMap::default(); scale_factors_buffer.clear();
for (entity, text, content_size, text_flags, camera, mut buffer) in &mut text_query { 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()) let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_ui_camera.get())
else { else {
continue; 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::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => *entry.insert( Entry::Vacant(entry) => *entry.insert(
camera_query 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)] #[allow(clippy::too_many_arguments)]
@ -284,6 +286,7 @@ fn queue_text(
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn text_system( pub fn text_system(
mut textures: ResMut<Assets<Image>>, mut textures: ResMut<Assets<Image>>,
mut scale_factors_buffer: Local<EntityHashMap<f32>>,
mut last_scale_factors: Local<EntityHashMap<f32>>, mut last_scale_factors: Local<EntityHashMap<f32>>,
fonts: Res<Assets<Font>>, fonts: Res<Assets<Font>>,
camera_query: Query<(Entity, &Camera)>, camera_query: Query<(Entity, &Camera)>,
@ -301,14 +304,14 @@ pub fn text_system(
&mut CosmicBuffer, &mut CosmicBuffer,
)>, )>,
) { ) {
let mut scale_factors: EntityHashMap<f32> = EntityHashMap::default(); scale_factors_buffer.clear();
for (node, text, text_layout_info, text_flags, camera, mut buffer) in &mut text_query { 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()) let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_ui_camera.get())
else { else {
continue; 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::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => *entry.insert( Entry::Vacant(entry) => *entry.insert(
camera_query 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);
} }