Skip to content

Commit 25acf6f

Browse files
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>
1 parent 43c970d commit 25acf6f

1 file changed

Lines changed: 33 additions & 28 deletions

File tree

‎stdlib/src/canvas.rs‎

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,34 +110,39 @@ fn render_dsl_to_svg(svg: &mut String, val: &RuntimeValue, is_dragon: bool) {
110110
);
111111
}
112112
"rings" => {
113-
let count = dsl
114-
.properties
115-
.iter()
116-
.find(|p| p.name == "count")
117-
.and_then(|p| p.value.as_ref())
118-
.and_then(|v| v.try_into_int().ok())
119-
.unwrap_or(3);
120-
let color = dsl
121-
.properties
122-
.iter()
123-
.find(|p| p.name == "color")
124-
.and_then(|p| p.value.as_ref())
125-
.map(|v| v.to_string())
126-
.unwrap_or_else(|| "#00d4ff".to_string());
127-
let size = dsl
128-
.properties
129-
.iter()
130-
.find(|p| p.name == "size")
131-
.and_then(|p| p.value.as_ref())
132-
.and_then(|v| v.try_into_int().ok())
133-
.unwrap_or(40);
134-
let thickness = dsl
135-
.properties
136-
.iter()
137-
.find(|p| p.name == "thickness")
138-
.and_then(|p| p.value.as_ref())
139-
.and_then(|v| v.try_into_int().ok())
140-
.unwrap_or(3);
113+
let mut count = 3;
114+
let mut color = None;
115+
let mut size = 40;
116+
let mut thickness = 3;
117+
118+
for p in &dsl.properties {
119+
if let Some(v) = &p.value {
120+
match p.name.as_str() {
121+
"count" => {
122+
if let Ok(c) = v.try_into_int() {
123+
count = c;
124+
}
125+
}
126+
"color" => {
127+
color = Some(v.to_string());
128+
}
129+
"size" => {
130+
if let Ok(s) = v.try_into_int() {
131+
size = s;
132+
}
133+
}
134+
"thickness" => {
135+
if let Ok(t) = v.try_into_int() {
136+
thickness = t;
137+
}
138+
}
139+
_ => {}
140+
}
141+
}
142+
}
143+
144+
let color = color.unwrap_or_else(|| "#00d4ff".to_string());
145+
141146
for i in 0..count {
142147
let r = 80 + i as i64 * (size / 2);
143148
let opacity = 0.4 - (i as f32 * 0.08);

0 commit comments

Comments
 (0)