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 <[email protected]>
This commit is contained in:
PanosandClaude Opus 5 committed 2026-09-05 09:10:48 +03:00
1 parent 13afc1479f
commit 2d2fdacdfd
1 file changed
+6 -1
+6 -1
View File
@@ -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);