Skip to content

⚡ Optimize font handle cloning in update_circle_layout loop#6

Open
dynamikdev wants to merge 1 commit intomasterfrom
performance-font-cloning-fix-11230702228547829404
Open

⚡ Optimize font handle cloning in update_circle_layout loop#6
dynamikdev wants to merge 1 commit intomasterfrom
performance-font-cloning-fix-11230702228547829404

Conversation

@dynamikdev
Copy link
Copy Markdown
Owner

The optimization in src/ui/systems.rs addresses the issue of redundant font handle cloning and unnecessary component updates in the update_circle_layout system, which runs every frame.

What: Moved font handle cloning and size calculation outside the main loop, and added conditional checks before updating the TextFont component.
Why: Cloning a Bevy Handle (like Handle<Font>) involves atomic reference counting, which is costly when repeated inside a loop that runs every frame. Additionally, updating a component's fields unconditionally triggers Bevy's change detection system, potentially causing unnecessary downstream work (like re-layout or re-rendering).
Measured Improvement: Due to the inability to download dependencies (bevy, bevy_ui_widgets) in this environment, a direct benchmark was not possible. However, this is a well-established optimization in Bevy for systems that update every frame. The number of atomic operations was reduced from $N$ (where $N$ is the number of target entities, in this case 8) to 1 per frame for the font handle, and component mutations were eliminated for all cases where the layout remains static.


PR created automatically by Jules for task 11230702228547829404 started by @dynamikdev

…tion 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>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant