perf(engine): optimize selection prune and sanity checks for large selections (#4502)

Fixes #4502.

- In `crates/doc/src/selection.rs`:
  - Simplifies dead condition in empty selection branch of `Selection::prune`.
  - Simplifies second branch to avoid redundant `key.is_some()` check.
  - Added unit test comparing `prune` against full-document unoptimized baseline across 300 random iterations for mixed objects, anchors, segments, guides, and key guides.
This commit is contained in:
el-j
2026-11-02 16:50:30 +01:00
parent f1ab585170
commit 07912347f4
3 changed files with 125 additions and 2 deletions
+20 -2
View File
@@ -55,6 +55,24 @@ fn rect_in_range(r: vectorcraft_geom::Rect) -> bool {
/// Do the objects `ids` have finite geometry within [`MAX_COORD`] (the ones not there pass)?
pub(crate) fn objects_in_range(d: &Document, ids: &[NodeId]) -> bool {
let paths = d.index_paths(ids.iter().copied());
ids.iter().all(|id| paths.get(id).and_then(|p| d.node_at(p)).and_then(|n| n.geometric_bounds()).is_none_or(rect_in_range))
if ids.is_empty() {
return true;
}
if d.has_lookup_cache() || ids.len() <= 16 {
return ids.iter().all(|id| d.node(*id).and_then(|n| n.geometric_bounds()).is_none_or(rect_in_range));
}
let want: std::collections::HashSet<NodeId> = ids.iter().copied().collect();
let mut ok = true;
d.walk(|n| {
if !ok {
return;
}
if want.contains(&n.id)
&& let Some(b) = n.geometric_bounds()
&& !rect_in_range(b)
{
ok = false;
}
});
ok
}