diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index 8123566196..a3d49ddd26 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -94,9 +94,9 @@ pub(super) struct State { coords: Cell, items: RefCell>, saving: Cell, - /// The last list of art one of [`layers`]' collections gave, kept while its document is - /// unchanged. - art: RefCell>, + /// The last [`layers::ART_LISTS`] lists of art [`layers`]' collections gave, least recently + /// used first, each kept while its document is unchanged. + art: RefCell>, /// The uids of the documents whose active artboard `setActiveArtboardIndex` changed. pub(super) active_artboards: RefCell>, presets: RefCell>>, diff --git a/crates/engine/src/script/dom/layers.rs b/crates/engine/src/script/dom/layers.rs index bd0046a4b4..b6c82ab1f1 100644 --- a/crates/engine/src/script/dom/layers.rs +++ b/crates/engine/src/script/dom/layers.rs @@ -572,17 +572,23 @@ fn art_scope(h: &Host, d: &Data, class: &str) -> Result<(Doc, Option)> { /// and the ids it listed then. pub(super) type ArtList = (u64, Arc, u8, Option, Rc>); +/// How many lists of art [`super::State`] keeps at once. +pub(super) const ART_LISTS: usize = 4; + /// The objects of `kind` in `container` (directly), or in the whole document at any depth, in -/// paint order. The last list is kept while the document is unchanged, and each new one is charged -/// by the objects it looks at. +/// paint order. The last [`ART_LISTS`] lists are kept while their documents are unchanged, so a +/// loop over groups that reads each group's `pageItems` builds each list once. Each new list is +/// charged by the objects it looks at. fn art_list(h: &Host, st: &DocState, kind: Kind, container: Option) -> Result>> { - if let Some((uid, doc, k, c, ids)) = &*h.dom.art.borrow() - && *uid == st.uid - && Arc::ptr_eq(doc, &st.doc) - && *k == kind as u8 - && *c == container { - return Ok(ids.clone()); + let mut lists = h.dom.art.borrow_mut(); + let at = lists.iter().position(|(uid, doc, k, c, _)| *uid == st.uid && Arc::ptr_eq(doc, &st.doc) && *k == kind as u8 && *c == container); + if let Some(at) = at { + let list = lists.remove(at); + let ids = list.4.clone(); + lists.push(list); + return Ok(ids); + } } let ids: Vec = match container { None => { @@ -597,7 +603,12 @@ fn art_list(h: &Host, st: &DocState, kind: Kind, container: Option) -> R } }; let ids = Rc::new(ids); - *h.dom.art.borrow_mut() = Some((st.uid, st.doc.clone(), kind as u8, container, ids.clone())); + let mut lists = h.dom.art.borrow_mut(); + lists.retain(|(uid, doc, ..)| *uid != st.uid || Arc::ptr_eq(doc, &st.doc)); + if lists.len() >= ART_LISTS { + lists.remove(0); + } + lists.push((st.uid, st.doc.clone(), kind as u8, container, ids.clone())); Ok(ids) } diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index c44ecb4887..7597da4da3 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -1250,6 +1250,30 @@ fn art_collections_follow_edits_between_reads() { assert_eq!(out, [json!(2), json!(0), json!(3), json!("GroupItem"), json!(1), json!(2), json!("PathItem")]); } +#[test] +fn a_loop_over_groups_and_their_items_lists_each_collection_once() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + for i in 0..5 { + let x = f64::from(i) * 20.0; + let (a, b) = (rect(&mut s, x, 0.0, 10.0, 10.0), rect(&mut s, x, 20.0, 10.0, 10.0)); + s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); + s.execute("object.group", &json!({})).unwrap(); + } + // Both scripts make the same calls. The first reads the groups and each group's items in + // turn; the second reads every group before any group's items, so it builds each list once. + let nested = "var gs = app.activeDocument.groupItems, n = 0;\ + for (var i = 0; i < gs.length; i++) { var p = gs[i].pageItems; for (var j = 0; j < p.length; j++) { p[j]; n++; } }\ + vc.log(n);"; + let in_order = "var gs = app.activeDocument.groupItems, all = [], n = 0;\ + for (var i = 0; i < gs.length; i++) { all.push(gs[i]); }\ + for (var k = 0; k < all.length; k++) { var p = all[k].pageItems; for (var j = 0; j < p.length; j++) { p[j]; n++; } }\ + vc.log(n);"; + let (a, b) = (run(&mut s, nested).unwrap(), run(&mut s, in_order).unwrap()); + assert_eq!((a["log"].clone(), b["log"].clone()), (json!([10]), json!([10]))); + assert_eq!(a["hostCalls"], b["hostCalls"], "{a}\n{b}"); +} + #[test] fn group_items_remove_all_refuses_locked_groups() { let (mut s, a, b) = doc();