From 52ae980326cfcdd2824cea8224681448befb155c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 1 Dec 2026 20:40:58 -0800 Subject: [PATCH] Scripts: a command no longer copies the empty-path closed settings Each command a script ran copied the set of empty paths the script had closed into the command's style trail, so a script that closed N empty paths copied up to N entries per command. The trail now shares the set through an `Arc`, and drops its share when the command's steps end, so a later change to the set does not copy it either. Settings that end after a kept command are dropped only when one actually ends. --- crates/engine/src/script/dom.rs | 5 +++-- crates/engine/src/script/dom/paths.rs | 15 ++++++++++++--- crates/engine/src/script/mod.rs | 13 +++++++++---- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index 8db00ef254..c825945e80 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -205,8 +205,9 @@ pub(super) struct State { /// ([`flush`]). pending: RefCell)>>, /// The paths with no points, by document uid, revert count and id, that a script set `closed` - /// on: the points `setEntirePath` gives them later make a closed path. - pub(super) closed_empty: RefCell>, + /// on: the points `setEntirePath` gives them later make a closed path. A command's steps share + /// the set without copying it ([`super::StyleTrail`]). + pub(super) closed_empty: RefCell>>, /// The index of the first point of each subpath of the path whose points the run read last, /// and what they were counted from ([`paths`]). point_starts: RefCell>)>>, diff --git a/crates/engine/src/script/dom/paths.rs b/crates/engine/src/script/dom/paths.rs index d1cc9d9be0..882bc4ff11 100644 --- a/crates/engine/src/script/dom/paths.rs +++ b/crates/engine/src/script/dom/paths.rs @@ -256,7 +256,10 @@ fn edit_points(h: &super::Host, item: Item, f: impl FnOnce(&mut Vec) let gained = |gained: bool| { if gained { let key = closed_key(h, item); - h.dom.closed_empty.borrow_mut().remove(&key); + let mut held = h.dom.closed_empty.borrow_mut(); + if held.contains(&key) { + Arc::make_mut(&mut held).remove(&key); + } } }; let mut f = Some(f); @@ -305,7 +308,7 @@ fn closed_key(h: &super::Host, item: Item) -> (u64, u64, NodeId) { /// Drops empty-path closed settings after a kept command gives a path points or removes it. pub(super) fn forget_closed_empty(h: &super::Host) { let s = h.session.borrow(); - h.dom.closed_empty.borrow_mut().retain(|(uid, reverts, id)| { + let keep = |(uid, reverts, id): &(u64, u64, NodeId)| { !s.script_style_trail.paths_with_points.contains(&(*uid, *reverts, *id)) && s.documents() .iter() @@ -313,7 +316,12 @@ pub(super) fn forget_closed_empty(h: &super::Host) { .and_then(|st| st.doc.node(*id)) .and_then(|n| n.path_data()) .is_some_and(|pd| pd.subpaths.is_empty()) - }); + }; + let mut held = h.dom.closed_empty.borrow_mut(); + // The set is copied only when another holder shares it and a setting ends. + if !held.iter().all(keep) { + Arc::make_mut(&mut held).retain(keep); + } } /// Drops the [`starts`] that [`super::State`] keeps of waiting point changes, which a change can @@ -786,6 +794,7 @@ fn path_set_closed(h: &super::Host, a: &[Data]) -> Answer { with_doc(h, item.doc, |_, st| refuse_path_edit(h, st, item.id))?; let key = closed_key(h, item); let mut held = h.dom.closed_empty.borrow_mut(); + let held = Arc::make_mut(&mut held); if closed { held.insert(key); } else { diff --git a/crates/engine/src/script/mod.rs b/crates/engine/src/script/mod.rs index 05d0cf1e9c..6b5c14b2f9 100644 --- a/crates/engine/src/script/mod.rs +++ b/crates/engine/src/script/mod.rs @@ -1432,7 +1432,11 @@ fn command_crossing(host: &Host, id: &str, params: &Value, cross: bool) -> std:: let before = dom::before_command(host, id); let r = { let mut s = session.borrow_mut(); - step(&mut s, host, id, params) + let r = step(&mut s, host, id, params); + // The steps are done with the empty-path closed settings, so the object model can change + // them again without copying them. + s.script_style_trail.closed_empty = Arc::default(); + r }; // The resource names a failed command's steps compared are still charged, before its error is // given. @@ -1511,14 +1515,15 @@ pub(crate) struct StyleTrail { pub(in crate::script) resources_gone: std::collections::HashSet<(u64, dom::ResourceKind, String)>, /// The resource names the steps compared, charged to the run with the check that follows. pub(crate) resources_scanned: u64, - closed_empty: std::collections::HashSet<(u64, u64, NodeId)>, + /// The object model's empty-path closed settings, shared with it while the steps run. + closed_empty: Arc>, pub(in crate::script) paths_with_points: std::collections::HashSet<(u64, u64, NodeId)>, } impl StyleTrail { /// A trail that starts from the documents' style lists now, and from the documents themselves /// when `watch_resources`. - fn start(s: &Session, watch_resources: bool, closed_empty: std::collections::HashSet<(u64, u64, NodeId)>) -> Self { + fn start(s: &Session, watch_resources: bool, closed_empty: Arc>) -> Self { let lists = s.docs.iter().map(|st| (st.uid, st.reverts, st.doc.char_styles.clone(), st.doc.para_styles.clone())).collect(); let docs = if watch_resources { s.docs.iter().map(|st| (st.uid, st.doc.clone())).collect() } else { Vec::new() }; StyleTrail { lists, docs, watch_resources, closed_empty, ..Default::default() } @@ -1531,7 +1536,7 @@ impl StyleTrail { /// document's lists, however many steps change them. pub(crate) fn note_style_lists(s: &mut Session) { let trail = &mut s.script_style_trail; - for &(uid, reverts, id) in &trail.closed_empty { + for &(uid, reverts, id) in trail.closed_empty.iter() { if s.docs .iter() .find(|st| st.uid == uid && st.reverts == reverts)