diff --git a/crates/ui-egui/src/panels/links.rs b/crates/ui-egui/src/panels/links.rs index 3b45e450d4..cf68e1d5bd 100644 --- a/crates/ui-egui/src/panels/links.rs +++ b/crates/ui-egui/src/panels/links.rs @@ -54,7 +54,7 @@ impl Default for Options { /// `links.list` and the first selected image's `links.info`, with what they were fetched for. #[derive(Clone, Default)] -struct Cache { +pub(super) struct Cache { /// (document uid, revision, show, sort, Show DCS Transparency Interactions). key: Option<(u64, u64, &'static str, Option<&'static str>, bool)>, at: f64, @@ -83,23 +83,23 @@ fn set_options(ctx: &egui::Context, f: impl FnOnce(&mut Options)) { } /// The rows to show (cached per document revision, the files looked at again every [`RECHECK`] -/// seconds) and the Link Info of the first selected image when the section is open. -fn cached(app: &mut VectorcraftApp, ctx: &egui::Context, o: &Options) -> Option { +/// seconds) and the Link Info of the first selected image when the section is open. A frame that +/// fetches nothing returns the stored cache without copying it and stores nothing. +fn cached(app: &mut VectorcraftApp, ctx: &egui::Context, o: &Options) -> Option> { let st = app.session.active()?; let key = Some((st.uid, st.revision, o.show, o.sort, o.dcs)); let first = selected_links(app, ctx).first().copied(); let now = ctx.input(|i| i.time); - let mut c: Cache = pstate(ctx, "links-cache"); + let mut c: std::sync::Arc = pstate(ctx, "links-cache"); let stale = c.key != key || now - c.at > RECHECK; if stale { let same_doc = c.key == key; - c.list = app.session.execute("links.list", &json!({ "show": o.show, "sort": o.sort, "dcsTransparency": o.dcs })).ok()?; - (c.key, c.at) = (key, now); + let list = app.session.execute("links.list", &json!({ "show": o.show, "sort": o.sort, "dcsTransparency": o.dcs })).ok()?; // One pass over the stored art for the rows outside the layer tree and the objects whose // opacity masks hold them, not one per frame. An object in the layer tree is looked up by // its index when drawn. let doc = app.session.active().map(|st| &st.doc); - let want: std::collections::HashSet = c.list["links"] + let want: std::collections::HashSet = list["links"] .as_array() .into_iter() .flatten() @@ -110,18 +110,22 @@ fn cached(app: &mut VectorcraftApp, ctx: &egui::Context, o: &Options) -> Option< .collect(); // The same revision keeps the objects collected for it, so their thumbnails are not drawn // again each time the files are looked at. - if !(same_doc && want.iter().all(|id| c.nested_node(*id).is_some())) { - c.nested = match doc { + let nested = if same_doc && want.iter().all(|id| c.nested_node(*id).is_some()) { + c.nested.clone() + } else { + match doc { Some(d) if !want.is_empty() => stored_objects(d, want), _ => vec![], - }; - } + } + }; + c = std::sync::Arc::new(Cache { key, at: now, list, info: None, nested }); } let want = first.filter(|_| o.info); if stale || c.info.as_ref().map(|(id, _)| *id) != want { - c.info = want.and_then(|id| Some((id, app.session.execute("links.info", &json!({ "id": id })).ok()?))); + let info = want.and_then(|id| Some((id, app.session.execute("links.info", &json!({ "id": id })).ok()?))); + std::sync::Arc::make_mut(&mut c).info = info; + set_pstate(ctx, "links-cache", c.clone()); } - set_pstate(ctx, "links-cache", c.clone()); // Files change on disk without a frame of ours: look again later. ctx.request_repaint_after(std::time::Duration::from_secs_f64(RECHECK)); Some(c) @@ -819,9 +823,10 @@ fn link_info(ui: &mut Ui, info: Option<&Value>, units: vectorcraft_doc::Unit) { pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) { let o = options(ui.ctx()); // The rows the panel shows (the menu opens from it). - let list = pstate::(ui.ctx(), "links-cache").list; + let cache = pstate::>(ui.ctx(), "links-cache"); + let list = &cache.list; let selected = selected_links(app, ui.ctx()); - let sel = selected_rows(&list, &selected); + let sel = selected_rows(list, &selected); let linked = ids(&sel, |r| r["linked"] == true); let embedded: Vec<&Value> = sel.iter().copied().filter(|r| status_is(r, "embedded")).collect(); let any = !sel.is_empty(); diff --git a/crates/ui-egui/src/panels/tests_links.rs b/crates/ui-egui/src/panels/tests_links.rs index aaf0ed0be7..1d38e6eb9b 100644 --- a/crates/ui-egui/src/panels/tests_links.rs +++ b/crates/ui-egui/src/panels/tests_links.rs @@ -136,6 +136,36 @@ fn the_panel_lists_the_images_with_link_info_for_the_selected_one() { } } +/// A frame that fetches nothing keeps the stored rows and Link Info as they are, without a copy; the +/// two-second recheck, another first selected image and an edit each store a new cache. +#[test] +fn frames_that_fetch_nothing_reuse_the_cached_rows() { + let dir = Folder::new("cache"); + let (mut app, linked, _) = app(&dir); + let ctx = egui::Context::default(); + crate::theme::install_fonts(&ctx); + let frame = |app: &mut VectorcraftApp, time: f64| { + let raw = egui::RawInput { time: Some(time), ..Default::default() }; + let text: Vec = super::tests_appearance::frame_raw(&ctx, app, raw, super::links::show).into_iter().map(|(t, _)| t).collect(); + (super::pstate::>(&ctx, "links-cache"), text) + }; + let (first, text) = frame(&mut app, 1.0); + assert!(text.iter().any(|t| t == "linked.png") && text.iter().any(|t| t == "40 × 20 px"), "{text:?}"); + let (same, text) = frame(&mut app, 1.5); + assert!(std::sync::Arc::ptr_eq(&first, &same), "a frame that fetches nothing stores no new cache"); + assert!(text.iter().any(|t| t == "linked.png") && text.iter().any(|t| t == "40 × 20 px"), "{text:?}"); + let (rechecked, _) = frame(&mut app, 3.6); + assert!(!std::sync::Arc::ptr_eq(&same, &rechecked), "the files are looked at again after two seconds"); + app.run("select.set", json!({"ids": [linked]})).unwrap(); + let (picked, text) = frame(&mut app, 3.7); + assert!(!std::sync::Arc::ptr_eq(&rechecked, &picked), "Link Info follows the first selected image"); + assert!(text.iter().any(|t| t == "300 × 200 px"), "{text:?}"); + app.run("edit.clear", json!({"ids": [linked]})).unwrap(); + let (edited, text) = frame(&mut app, 3.8); + assert!(!std::sync::Arc::ptr_eq(&picked, &edited), "an edit lists the rows again"); + assert!(!text.iter().any(|t| t == "linked.png") && text.iter().any(|t| t == "embedded.png"), "{text:?}"); +} + /// Copy Info copies the file names of the selected links, or of `ids` in the order given, one per /// line; it is off with nothing selected, and a bad `ids` leaves the clipboard as it was. #[test] diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index e7c58c75f8..82309fa7c8 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2252,6 +2252,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur On the desktop, the Layers panel renders a row's thumbnail on its thumbnail thread when the row's paths, and the paths of its opacity masks that are not disabled, hold more than 2,000 anchors in all. It already did this for the first thumbnail of a row of more than 200 objects. Until its first thumbnail is ready, the row shows boxes at the bounds of up to 41 of its paths, each in its path's solid fill or stroke color or in gray, when its own paths, without its opacity masks, hold at most 2,000 anchors in all, and an empty white box otherwise. With one path of 200,000 anchors, the first frame after the panel opens takes 2.4 ms instead of 393 ms (release build, median of three runs). The same 2,000-anchor limit counts each glyph of type as 4 anchors, each point of a gradient mesh as 1 and every 100 pixels of an image as 1, and a symbol instance or a placed document counts the objects and anchors of the art it shows. A placed document whose file has not been read yet, or a symbol nested more than 8 deep (one that holds an instance of itself, for example), gets its first thumbnail on the thumbnail thread, and so does a row whose art takes more than 20,000 steps to count (each object, subpath, text run and symbol looked at is one step). The first frame after the panel opens takes 2.4 ms instead of 19.8 ms with 5,000 glyphs of point type, 1.8 ms instead of 343 ms with one instance of a symbol of a 200,000-anchor path, 2.0 ms instead of 11.0 ms with a 100 × 100 mesh, and 2.1 ms instead of 274 ms with a placed document of a 200,000-anchor path (release build, median of three runs). When the Layers or Links panel shows another document's rows, the thumbnail thread drops the row thumbnails it has not started for the documents not shown, and it drops all of them when the Layers or Links panel is open after the last document closes. Those rows ask for their thumbnails again when their document shows again, and the thumbnails already made are kept. With 150 rows of 20,000-anchor paths waiting for another document, the 8 rows of such paths of the document shown get their thumbnails in 153 ms instead of 3,265 ms (debug test build, median of three runs). + The Links panel keeps its `links.list` rows and Link Info in one shared cache, and a frame that fetches nothing new reads that cache without copying the rows. The cache is replaced when the document or its revision changes, the Show, Sort or Show DCS Transparency Interactions option changes, or the files are looked at again every 2 seconds, and, while an image is selected, when Link Info opens or closes or the first selected image changes. With 2,001 images a frame's cache step takes 0.2 µs instead of 1.79 ms, and with 10,001 images 0.2 µs instead of 12.2 ms (test build, median of 7 wall-clock runs of 1,000 frames, measured while other builds were running). In the web build, while a pan, zoom or rotation changes the view of a document whose last canvas render took more than 8 ms, the canvas draws the last frame moved with the view, and it renders the art again once the view has been still for 0.2 s. An Alt-drag with the Selection, Direct Selection, Group Selection, Rotate, Scale, Reflect or Shear tool copies the selection on its first move, and the moves after it transform those copies; the previews of the Move, Rotate, Scale, Reflect and Shear dialogs with Copy and Preview on copy the selection once in the same way (#1429). An Alt-drag of objects in perspective with the Perspective Selection tool, and an Alt-drag of a plane widget with that tool or the Perspective Grid tool, copy the objects on the first move, and the moves after it move those copies (#1429).