diff --git a/crates/engine/src/cmd/browser.rs b/crates/engine/src/cmd/browser.rs index c1a64c2dc..685f961d5 100644 --- a/crates/engine/src/cmd/browser.rs +++ b/crates/engine/src/cmd/browser.rs @@ -1424,6 +1424,51 @@ mod tests { assert_eq!(s.selection.len(), 2); } + /// The viewport asks whether each body is shown several times a frame: a lookup reads the + /// set of hidden bodies, not the list, and follows every change of the list, one written in + /// place included. + #[test] + fn hidden_body_lookups_do_not_walk_the_hidden_body_list() { + use crate::occurrences::visibility_work; + + let mut s = Session::default(); + let hide_many = |s: &mut Session, n: usize| { + s.visibility.hidden_bodies.clear(); + s.visibility.hidden_bodies.extend((0..n).map(|i| format!("Hidden{i}"))); + }; + let walked_by = |s: &Session, name: &str| { + visibility_work::take(); + let visible = s.body_visible(name); + (visible, visibility_work::take()) + }; + hide_many(&mut s, 10); + let (_, few) = walked_by(&s, "Shown"); + hide_many(&mut s, 10_000); + visibility_work::built_take(); + let (visible, many) = walked_by(&s, "Shown"); + assert!(visible); + assert!(many <= few, "a lookup walked {many} entries of 10,000 hidden bodies and {few} of 10"); + assert_eq!(visibility_work::built_take(), 10_000, "the hidden bodies are read into the set once"); + for i in 0..300 { + assert!(walked_by(&s, &format!("Body{i}")) == (true, 0)); + } + assert_eq!(visibility_work::built_take(), 0, "and not again for the next lookups"); + assert!(!s.body_visible("Hidden5000") && !s.body_visible("Hidden9999")); + s.visibility.hidden_bodies.retain(|name| name != "Hidden5000"); + assert!(s.body_visible("Hidden5000"), "shown again in place"); + s.visibility.hidden_bodies.push("Shown".into()); + assert!(!s.body_visible("Shown"), "hidden in place"); + s.visibility.hidden_bodies.clear(); + assert!(s.body_visible("Shown") && s.body_visible("Hidden0"), "all shown"); + run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Peg"})); + run(&mut s, "browser.visibility", json!({"items": ["Peg"], "visible": false})); + assert!(!s.body_visible("Peg")); + run(&mut s, "edit.undo", json!({})); + assert!(s.body_visible("Peg"), "undo shows it again"); + run(&mut s, "edit.redo", json!({})); + assert!(!s.body_visible("Peg"), "redo hides it again"); + } + #[test] fn show_all_bodies_clears_stale_hidden_names() { for folder in ["bodies", "all"] { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index fabb4e0fa..56b27f959 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -496,9 +496,9 @@ pub struct Session { /// (see [`Session::solve_joints`]). joints_settled: Option<(Arc, std::sync::Weak)>, occurrence_visibility_cache: std::sync::Mutex>, - /// The hidden joints and folders of the view, as sets: the browser asks about every joint - /// of the design on every frame (#3892), and an answer that walked the hidden lists made a - /// frame cost grow with the size of the design. + /// The hidden joints, folders and bodies of the view, as sets: the browser asks about every + /// joint of the design on every frame (#3892), the viewport about every body, and an answer + /// that walked the hidden lists made a frame cost grow with the size of the design. visibility_index_cache: std::sync::Mutex>, /// The sketches a feature uses, with the design they were read from /// ([`view::sketch_consumed`]): the viewport asks about every sketch several times a frame, diff --git a/crates/engine/src/occurrences.rs b/crates/engine/src/occurrences.rs index 816c567c4..f3978b919 100644 --- a/crates/engine/src/occurrences.rs +++ b/crates/engine/src/occurrences.rs @@ -32,10 +32,11 @@ pub(crate) struct VisibilityCache { bodies: std::collections::BTreeSet, } -/// The hidden joints and folders of the view, as sets: the browser asks about every joint -/// and every folder of the design on every frame, and an answer that walked the hidden -/// lists made a frame cost grow with the size of the design (#3892). The sets are built -/// when the view changes and are kept until it changes again. +/// The hidden joints, folders and bodies of the view, as sets: the browser asks about every +/// joint and every folder of the design on every frame, the viewport about every body several +/// times a frame, and an answer that walked the hidden lists made a frame cost grow with the +/// size of the design (#3892). The sets are built when the view changes and are kept until it +/// changes again. pub(crate) struct VisibilityIndex { /// The revision of the design the sets were built for. revision: u64, @@ -49,11 +50,16 @@ pub(crate) struct VisibilityIndex { folders_at: ListId, /// The hidden folder keys. folder_keys: std::collections::HashSet, + /// Where the hidden bodies were when the sets were built. + bodies_at: ListId, + /// The names of the bodies hidden themselves. + body_names: std::collections::HashSet, /// The hidden joints and folders, as they were when the sets were built: they are /// compared with these to tell a change of the view from a change of the design alone /// (the design changes far more often than the view does). joints: Vec, folders: Vec, + bodies: Vec, } /// Where a list of hidden items was: the address of its buffer and how many items it held. @@ -77,15 +83,19 @@ impl VisibilityIndex { fn new(revision: u64, v: &Visibility) -> VisibilityIndex { let joint_ids: std::collections::HashSet = v.hidden_joints.iter().copied().collect(); let folder_keys: std::collections::HashSet = v.hidden_folders.iter().cloned().collect(); - visibility_work::built(joint_ids.len() as u64 + folder_keys.len() as u64); + let body_names: std::collections::HashSet = v.hidden_bodies.iter().cloned().collect(); + visibility_work::built(joint_ids.len() as u64 + folder_keys.len() as u64 + body_names.len() as u64); VisibilityIndex { revision, joints_at: ListId::of(&v.hidden_joints), joint_ids, folders_at: ListId::of(&v.hidden_folders), folder_keys, + bodies_at: ListId::of(&v.hidden_bodies), + body_names, joints: v.hidden_joints.clone(), folders: v.hidden_folders.clone(), + bodies: v.hidden_bodies.clone(), } } @@ -103,12 +113,15 @@ impl VisibilityIndex { && self.joints.last() == v.hidden_joints.last() && self.folders.first() == v.hidden_folders.first() && self.folders.last() == v.hidden_folders.last() + && self.bodies_at == ListId::of(&v.hidden_bodies) + && self.bodies.first() == v.hidden_bodies.first() + && self.bodies.last() == v.hidden_bodies.last() } /// Are the hidden joints and folders the ones the sets were built from? A design that /// changed without touching the view leaves the sets alone. fn same_lists(&self, v: &Visibility) -> bool { - self.joints == v.hidden_joints && self.folders == v.hidden_folders + self.joints == v.hidden_joints && self.folders == v.hidden_folders && self.bodies == v.hidden_bodies } /// The lists are the ones the sets were built from, at another revision. @@ -116,6 +129,7 @@ impl VisibilityIndex { self.revision = revision; self.joints_at = ListId::of(&v.hidden_joints); self.folders_at = ListId::of(&v.hidden_folders); + self.bodies_at = ListId::of(&v.hidden_bodies); } } @@ -705,10 +719,10 @@ impl Session { Ok(count) } - /// The hidden joints and folders of the view, as sets: the browser asks about every joint - /// of the design on every frame, and an answer that walked the hidden lists made a frame - /// cost grow with the size of the design (#3892). The sets are built when the view changes - /// and are kept until it changes again. + /// The hidden joints, folders and bodies of the view, as sets: the browser asks about every + /// joint of the design on every frame, and an answer that walked the hidden lists made a + /// frame cost grow with the size of the design (#3892). The sets are built when the view + /// changes and are kept until it changes again. fn visibility_index(&self) -> Option>> { let mut cache = self.visibility_index_cache.lock().ok()?; let v = &self.visibility; @@ -819,7 +833,7 @@ impl Session { } pub fn body_visible(&self, name: &str) -> bool { - if self.visibility.hidden_bodies.iter().any(|n| n == name) || self.body_removed_later(name) { + if self.body_hidden_itself(name) || self.body_removed_later(name) { return false; } let v = &self.visibility; @@ -840,6 +854,16 @@ impl Session { !self.bodies_hidden_by_parents().contains(name) } + /// Is the body hidden itself (its own eye; a placement, folder or group may still hide it)? + fn body_hidden_itself(&self, name: &str) -> bool { + if let Some(cache) = self.visibility_index() { + return cache.as_ref().is_some_and(|c| c.body_names.contains(name)); + } + // The lock is poisoned: answer from the list itself. + visibility_work::add(self.visibility.hidden_bodies.len() as u64); + self.visibility.hidden_bodies.iter().any(|n| n == name) + } + /// World names of the bodies a hidden placement, folder or group above them hides (their /// own state aside). fn bodies_hidden_by_parents(&self) -> std::collections::BTreeSet {