View: look hidden bodies up in a set (#10496)

* claim: View: work out consumed sketches and hidden bodies once per revision

* View: work out the sketches features use once per design

Each sketch_shown call walked every feature of the design to tell whether
the sketch is used, and the viewport asks about every sketch several
times a frame. The used sketches are now gathered into a set once per
design (keyed on the design itself, so any edit reads them again).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* View: look hidden bodies up in a set

body_visible walked the hidden-bodies list for every body asked about, and
the viewport asks about every body several times a frame. The hidden
bodies now join the hidden joints and folders in the view's visibility
index, built when the list changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Markus Kaltenbrunner <kaltenbrunnermarkus@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kaltenbrunnermarkus
2026-11-01 09:59:42 +00:00
committed by GitHub
parent a93d7b207c
commit ab6e6941c7
3 changed files with 83 additions and 14 deletions
+45
View File
@@ -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"] {
+3 -3
View File
@@ -496,9 +496,9 @@ pub struct Session {
/// (see [`Session::solve_joints`]).
joints_settled: Option<(Arc<Document>, std::sync::Weak<solidcraft_doc::ModelState>)>,
occurrence_visibility_cache: std::sync::Mutex<Option<occurrences::VisibilityCache>>,
/// 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<Option<occurrences::VisibilityIndex>>,
/// 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,
+35 -11
View File
@@ -32,10 +32,11 @@ pub(crate) struct VisibilityCache {
bodies: std::collections::BTreeSet<String>,
}
/// 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<String>,
/// 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<String>,
/// 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<u64>,
folders: Vec<String>,
bodies: Vec<String>,
}
/// 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<u64> = v.hidden_joints.iter().copied().collect();
let folder_keys: std::collections::HashSet<String> = 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<String> = 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<std::sync::MutexGuard<'_, Option<VisibilityIndex>>> {
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<String> {