mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 21:27:00 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user