SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Open: check timeline folders for cycles in one pass (#12416)
DuBearte8505c77f5bd4
diff --git a/crates/doc/src/format.rs b/crates/doc/src/format.rsindex f8708c6298..d5998af841 100644--- a/crates/doc/src/format.rs+++ b/crates/doc/src/format.rs@@ -785,20 +785,24 @@ pub(crate) fn decode(mut v: Value, version: u32, applied: Option<&[String]>) ->return Err(DocError::Invalid("damaged design: timeline folder cannot contain itself".into()));}}+ // Each folder's chain of parents ends at the top, walked once: a chain stops at a folder+ // already known to end there. Walking every chain with a search per step took minutes on a+ // design with 10 000 nested folders.+ let parents: std::collections::BTreeMap<u64, Option<u64>> = d.timeline_groups.iter().map(|g| (g.id, g.parent)).collect();+ let mut ends = std::collections::BTreeSet::new();for group in &d.timeline_groups {- if let Some(parent) = group.parent {- if !group_ids.contains(&parent) {- return Err(DocError::Invalid("damaged design: timeline folder has an unknown parent".into()));- }- let mut seen = std::collections::BTreeSet::new();- let mut current = Some(parent);- while let Some(id) = current {- if !seen.insert(id) {- return Err(DocError::Invalid("damaged design: cyclic timeline folders".into()));- }- current = d.timeline_groups.iter().find(|candidate| candidate.id == id).and_then(|candidate| candidate.parent);+ if group.parent.is_some_and(|parent| !group_ids.contains(&parent)) {+ return Err(DocError::Invalid("damaged design: timeline folder has an unknown parent".into()));+ }+ let mut chain = std::collections::BTreeSet::new();+ let mut current = Some(group.id);+ while let Some(id) = current.filter(|id| !ends.contains(id)) {+ if !chain.insert(id) {+ return Err(DocError::Invalid("damaged design: cyclic timeline folders".into()));}+ current = parents.get(&id).copied().flatten();}+ ends.extend(chain);}if let Some(id) = crate::non_contiguous_timeline_group(&d.features, &d.timeline_groups) {return Err(DocError::Invalid(format!("damaged design: timeline folder {id} is not contiguous")));diff --git a/crates/doc/src/tests.rs b/crates/doc/src/tests.rsindex 2cd078d9b9..870489c807 100644--- a/crates/doc/src/tests.rs+++ b/crates/doc/src/tests.rs@@ -664,6 +664,26 @@ fn a_folder_naming_missing_or_repeated_features_still_opens() {assert_eq!(opened.timeline_groups.iter().map(|g| g.name.as_str()).collect::<Vec<_>>(), ["Repeats", "Stale"], "both folders are kept");}+/// Timeline folders nested 10 000 deep open at once: each folder's chain of parents is walked+/// once, not again for every folder inside it (a 588 KB design took over three minutes). A+/// cycle is still refused.+#[test]+fn deeply_nested_timeline_folders_open_and_a_cycle_does_not() {+ let mut doc = Document::new("nested folders");+ let n = 10_000u64;+ doc.timeline_groups = (0..n)+ .map(|i| TimelineGroup { id: 100 + i, name: "f".into(), parent: (i + 1 < n).then_some(101 + i), features: vec![], joints: vec![] })+ .collect();+ let started = std::time::Instant::now();+ let opened = Document::from_json(&doc.to_json()).expect("the design opens");+ assert_eq!(opened.timeline_groups.len(), n as usize);+ assert!(started.elapsed() < std::time::Duration::from_secs(20), "{:?}", started.elapsed());+ doc.timeline_groups =+ (0..3).map(|i| TimelineGroup { id: 100 + i, name: "f".into(), parent: Some(100 + (i + 1) % 3), features: vec![], joints: vec![] }).collect();+ let refused = Document::from_json(&doc.to_json()).unwrap_err().to_string();+ assert!(refused.contains("cyclic timeline folders"), "{refused}");+}+/// A marker after the last feature leaves every feature active, as no marker does, so a feature/// added then is active too, not added rolled back (#7741: editing the last feature from the/// History runs its command with the marker there).