From 52d72bc03ffe253be574aabdb4c6d3315a421c51 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 26 Mar 2026 12:32:00 +0000 Subject: [PATCH] Optimize update_circle_layout by moving font cloning and size calculation outside the loop. This commit improves the performance of the `update_circle_layout` system in Bevy: 1. Moves the `typography.space_grotesk.clone()` call outside the `for` loop to avoid redundant atomic operations. 2. Calculates the final `font_size` once outside the loop. 3. Adds checks to only update `text_font.font` and `text_font.font_size` if they have actually changed, preventing unnecessary component mutations and triggering of Bevy's change detection. Co-authored-by: dynamikdev <717692+dynamikdev@users.noreply.github.com> --- src/ui/systems.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ui/systems.rs b/src/ui/systems.rs index 576d4bd..7117c97 100644 --- a/src/ui/systems.rs +++ b/src/ui/systems.rs @@ -196,7 +196,8 @@ pub fn update_circle_layout( // Use minimum dimension for radius to ensure it fits comfortably with padding. let radius = available_width.min(available_height) / 2.0 * 0.8; // Scale font size linearly based on the radius to maintain visual proportions. - let dynamic_font_size = radius * 0.25; + let font_size = (radius * 0.25).max(10.0); + let font_handle = typography.space_grotesk.clone(); for (index, mut transform, mut text_font) in &mut text_query { let i = index.0; @@ -210,8 +211,16 @@ pub fn update_circle_layout( transform.translation.x = x - PANEL_WIDTH / 2.0; transform.translation.y = y; transform.translation.z = 1.0; - text_font.font = typography.space_grotesk.clone(); - text_font.font_size = dynamic_font_size.max(10.0); // Ensure readability on small windows. + + // Optimize: Only update font and size if they have actually changed. + // This avoids unnecessary atomic operations from Handle cloning and + // unnecessary component mutations. + if text_font.font_size != font_size { + text_font.font_size = font_size; + } + if text_font.font != font_handle { + text_font.font = font_handle.clone(); + } } }