From cecb1d8b6689f6dfc750f702e08bdc2d707053fe Mon Sep 17 00:00:00 2001 From: PaddleStroke Date: Wed, 28 Oct 2026 14:43:42 +0000 Subject: [PATCH] Naming: a long history of one body no longer overflows the stack on reorder (#7587) (#8159) * claim: #7587 Co-Authored-By: Claude Opus 5.5 * Naming: name a long history of one body from the oldest up (#7587) A body's face names are worked out from the names of the body it was made from, recursively. After a few hundred features in a row whose names nothing asked for, reordering or moving an earlier feature re-evaluated them all and the first name lookup (a fillet at the end) recursed once per feature, overflowing the 1 MB window-thread stack on Windows. Name every 16th earlier version oldest first so no lookup recurses through more than 16 of them; the names are the same. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/doc/src/naming.rs | 62 +++++++++++++++++++++++++++++ crates/engine/src/cmd/edit_tests.rs | 31 +++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/crates/doc/src/naming.rs b/crates/doc/src/naming.rs index a6191ed19..4aa5e938d 100644 --- a/crates/doc/src/naming.rs +++ b/crates/doc/src/naming.rs @@ -306,9 +306,61 @@ pub(crate) fn warm_names<'a>(bodies: impl Iterator) { } } +/// Bodies whose names are worked out one from the other, oldest first, at most this many apart +/// (see [`warm_chain`]). +const WARM_STEP: usize = 16; + +/// Up to this many earlier versions of a body are walked to name it from the oldest up. +const MAX_CHAIN: usize = 100_000; + +/// The body `b`'s names are worked out from first: the body it is a moved copy of, else the +/// body of its name before the feature that made it (`None`: none, or past [`MAX_DEPTH`]). +fn predecessor(b: &ModelBody) -> Option<&ModelBody> { + if let Some(source) = &b.names.copy_of { + return Some(source); + } + let o = b.names.origin.as_ref().filter(|o| o.depth < MAX_DEPTH)?; + o.before.body(&b.name) +} + +/// Work out the names of the earlier versions of `b` that are not known yet, oldest first and +/// every [`WARM_STEP`]th, so that naming `b` never recurses through more than that many of them. +/// A body's names are found from the names of the body it was made from, which are found the +/// same way: a body moved or changed by a few hundred features in a row whose names nothing +/// asked for overflowed the window thread's stack the first time something did (a fillet after +/// them evaluated again when an earlier feature is reordered or moved: #7587, #7926). `ready` says whether a +/// body's names are known, `name` works them out. The names are the same either way. +fn warm_chain(b: &ModelBody, ready: fn(&ModelBody) -> bool, name: fn(&ModelBody)) { + let mut chain: Vec<&ModelBody> = Vec::new(); + let mut at = b; + while chain.len() < MAX_CHAIN + && let Some(p) = predecessor(at) + && !ready(p) + { + chain.push(p); + at = p; + } + if chain.len() <= WARM_STEP { + return; + } + for p in chain.iter().rev().step_by(WARM_STEP) { + name(p); + } +} + /// A body's names, with what finding them again needs: from the history of the feature that /// made it where it reports one, else from geometry (unverified). pub(crate) fn names(b: &ModelBody) -> Arc { + if let Some(known) = b.names.names.get() { + return known.clone(); + } + warm_chain( + b, + |x| x.names.names.get().is_some(), + |x| { + let _ = names(x); + }, + ); // A moved copy has its source's names (the source's cell keeps them for both). if let Some(source) = &b.names.copy_of { return b.names.names.get_or_init(|| names(source)).clone(); @@ -333,6 +385,16 @@ pub(crate) fn names(b: &ModelBody) -> Arc { /// as those builds gave them, pieces of a split face numbered along the longest axis of their /// spread (`#1`, `#2`, …). pub(crate) fn legacy_face_names(b: &ModelBody) -> Arc> { + if let Some(known) = b.names.legacy.get() { + return known.clone(); + } + warm_chain( + b, + |x| x.names.legacy.get().is_some(), + |x| { + let _ = legacy_face_names(x); + }, + ); if let Some(source) = &b.names.copy_of { return legacy_face_names(source); } diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rs index 140c4a552..c7aa2544f 100644 --- a/crates/engine/src/cmd/edit_tests.rs +++ b/crates/engine/src/cmd/edit_tests.rs @@ -1301,3 +1301,34 @@ fn select_same_look_uses_resolved_looks_without_document_history_edits_and_refus assert!(std::sync::Arc::ptr_eq(&doc, &s.doc)); assert_eq!(history, (s.undo.len(), s.redo.len(), s.evaluations)); } + +/// A body changed by a few hundred features in a row whose names nothing asks for, then +/// filleted: reordering or moving an earlier feature evaluates them all again, and the fillet +/// finds its edge by the names of the body before it, which are worked out from the body before +/// that, and so on back to the start. That walk recursed once per feature and overflowed the +/// window thread's stack (1 MB on Windows), ending the app (#7587, #7926). On a thread with that +/// stack, both moves are made. +#[test] +fn a_move_before_a_long_history_of_one_body_fits_a_window_threads_stack() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Peg"})); + run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [50, 0, 0], "body_name": "Other"})); + for i in 0..300 { + run(&mut s, "solid.move", json!({"bodies": ["Peg"], "translate": [0, 0, if i % 2 == 0 { 1 } else { -1 }]})); + } + run(&mut s, "solid.fillet", json!({"body": "Peg", "radius": 1, "edges": [[5, 0, 10]]})); + let other = s.doc.features.get(1).map(|f| f.id).unwrap(); + let fillet = s.doc.features.last().map(|f| f.id).unwrap(); + let moved = std::thread::Builder::new() + .stack_size(1024 * 1024) + .spawn(move || { + run(&mut s, "timeline.reorder", json!({"feature": other, "position": 0})); + assert!(s.model.result(fillet).is_some_and(|r| r.error.is_none()), "the fillet is made after the reorder"); + run(&mut s, "timeline.move_many", json!({"features": [other], "position": 2})); + assert_eq!(s.doc.features.get(1).map(|f| f.id), Some(other)); + assert!(s.model.result(fillet).is_some_and(|r| r.error.is_none()), "the fillet is made after the move"); + }) + .unwrap() + .join(); + assert!(moved.is_ok()); +}