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

Don't reallocate scale factors in measure_text_system #14999

Merged
merged 3 commits into from
Sep 2, 2024
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
15 changes: 9 additions & 6 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,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<EntityHashMap<f32>>,
mut last_scale_factors: Local<EntityHashMap<f32>>,
fonts: Res<Assets<Font>>,
camera_query: Query<(Entity, &Camera)>,
Expand All @@ -151,14 +153,14 @@ pub fn measure_text_system(
>,
mut text_pipeline: ResMut<TextPipeline>,
) {
let mut scale_factors: EntityHashMap<f32> = EntityHashMap::default();
scale_factors_buffer.clear();

for (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
Expand Down Expand Up @@ -187,7 +189,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)]
Expand Down Expand Up @@ -260,6 +262,7 @@ fn queue_text(
#[allow(clippy::too_many_arguments)]
pub fn text_system(
mut textures: ResMut<Assets<Image>>,
mut scale_factors_buffer: Local<EntityHashMap<f32>>,
mut last_scale_factors: Local<EntityHashMap<f32>>,
fonts: Res<Assets<Font>>,
camera_query: Query<(Entity, &Camera)>,
Expand All @@ -277,14 +280,14 @@ pub fn text_system(
&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 {
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
Expand Down Expand Up @@ -317,5 +320,5 @@ pub fn text_system(
);
}
}
*last_scale_factors = scale_factors;
std::mem::swap(&mut *last_scale_factors, &mut *scale_factors_buffer);
}