From 25acf6f375d3772e0dbd9e73eeb0bf10d0331298 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 22 Sep 2026 07:48:59 +0000 Subject: [PATCH] refactor: optimize property fetching in canvas rings rendering Replaces the O(N * M) multiple `find` calls with a single O(N) pass through the `properties` vector when rendering `rings` DSL blocks in the canvas standard library. Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- stdlib/src/canvas.rs | 61 ++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/stdlib/src/canvas.rs b/stdlib/src/canvas.rs index 3c4b3302..ac460182 100644 --- a/stdlib/src/canvas.rs +++ b/stdlib/src/canvas.rs @@ -110,34 +110,39 @@ fn render_dsl_to_svg(svg: &mut String, val: &RuntimeValue, is_dragon: bool) { ); } "rings" => { - let count = dsl - .properties - .iter() - .find(|p| p.name == "count") - .and_then(|p| p.value.as_ref()) - .and_then(|v| v.try_into_int().ok()) - .unwrap_or(3); - let color = dsl - .properties - .iter() - .find(|p| p.name == "color") - .and_then(|p| p.value.as_ref()) - .map(|v| v.to_string()) - .unwrap_or_else(|| "#00d4ff".to_string()); - let size = dsl - .properties - .iter() - .find(|p| p.name == "size") - .and_then(|p| p.value.as_ref()) - .and_then(|v| v.try_into_int().ok()) - .unwrap_or(40); - let thickness = dsl - .properties - .iter() - .find(|p| p.name == "thickness") - .and_then(|p| p.value.as_ref()) - .and_then(|v| v.try_into_int().ok()) - .unwrap_or(3); + let mut count = 3; + let mut color = None; + let mut size = 40; + let mut thickness = 3; + + for p in &dsl.properties { + if let Some(v) = &p.value { + match p.name.as_str() { + "count" => { + if let Ok(c) = v.try_into_int() { + count = c; + } + } + "color" => { + color = Some(v.to_string()); + } + "size" => { + if let Ok(s) = v.try_into_int() { + size = s; + } + } + "thickness" => { + if let Ok(t) = v.try_into_int() { + thickness = t; + } + } + _ => {} + } + } + } + + let color = color.unwrap_or_else(|| "#00d4ff".to_string()); + for i in 0..count { let r = 80 + i as i64 * (size / 2); let opacity = 0.4 - (i as f32 * 0.08);