From 2d2fdacdfd2aea4ee43345c9155fd782c799f530 Mon Sep 17 00:00:00 2001 From: Panos <25664832+mechpanos@users.noreply.github.com> Date: Fri, 4 Sep 2026 17:04:13 +0300 Subject: [PATCH] perf: parse each wire name once when applying draw order The resident wire sort keys on the entity handle parsed out of each wire's name `String`. `sort_by_key` evaluates that key on every comparison, so a 198 k-wire resident set parsed every name roughly eighteen times over. Measured at 295 ms on every rebuild of the resident set -- including rebuilds that re-tessellated nothing, where the memo hit on all 186 455 entities and the sort was most of what was left. `sort_by_cached_key` evaluates the key once per element. It is stable in the same way `sort_by_key` is, so draw order is unchanged. Co-Authored-By: Claude Opus 5 --- src/scene/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/scene/mod.rs b/src/scene/mod.rs index da511e24f..f012cd2e6 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -9066,7 +9066,12 @@ impl Scene { if let Some((_, ref idx)) = *cache { if let Some(sort_map) = idx.get(&block_handle) { sorted = true; - wires.sort_by_key(|w| { + // `sort_by_cached_key`, not `sort_by_key`: the key parses + // the wire's name out of a `String`, and `sort_by_key` calls + // it O(n log n) times. On the resident set that is ~198 k + // wires parsed eighteen times over — 295 ms on every rebuild, + // including rebuilds where nothing was re-tessellated. + wires.sort_by_cached_key(|w| { let key = Self::handle_from_wire_name(&w.name) .map(|h| h.value()) .unwrap_or(u64::MAX);