From 7a176ae0a88bf69d6343970da20f4961dea7ecc3 Mon Sep 17 00:00:00 2001 From: James Liu Date: Sun, 22 Jan 2023 06:51:31 +0000 Subject: [PATCH] Optimize color computation in prepare_uinodes (#7311) # Objective Speed up `prepare_uinodes`. The color `[f32; 4]` is being computed separately for every vertex in the UI, even though the color is the same for all 6 verticies. ## Solution Avoid recomputing the color and cache it for all 6 verticies. ## Performance On `many_buttons`, this shaved off 33% of the time in `prepare_uinodes` (7.67ms -> 5.09ms) on my local machine. ![image](https://user-images.githubusercontent.com/3137680/213862448-236ac6e4-040a-4c86-a801-b947d99cc581.png) --- crates/bevy_ui/src/render/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 17d6719b4a083..002c4655a4163 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -523,11 +523,12 @@ pub fn prepare_uinodes( uvs = [uvs[3], uvs[2], uvs[1], uvs[0]]; } + let color = extracted_uinode.background_color.as_linear_rgba_f32(); for i in QUAD_INDICES { ui_meta.vertices.push(UiVertex { position: positions_clipped[i].into(), uv: uvs[i].into(), - color: extracted_uinode.background_color.as_linear_rgba_f32(), + color, }); }