Files
vectorcraft/crates/engine/src/cmd/docmenu/tests.rs
T
dotCooCoo 30d5552795 Select: Object > Direction Handles selects the paths with one set call
Direction Handles cleared the selection and then added each path on its
own, and each add searched the paths already selected, so building the
selection took time that grows with the square of the path count. The
command now selects the paths with one Selection::set call and adds the
anchor sets in one extend. The selection is the same: paths keep their
order, a repeated path keeps its first place and its last anchor set,
and the side fields are cleared as before.

crates/engine/examples/direction_handles_bench.rs times both ways on the
same paths in one process. With 20,000 four-anchor paths, building the
selection takes 1.9 ms of thread CPU time against 69 ms one path at a
time.
2026-11-16 12:39:16 -08:00

69 lines
2.6 KiB
Rust

//! The selection that Select ▸ Object ▸ Direction Handles builds.
use std::collections::{BTreeSet, HashSet};
use std::sync::Arc;
use vectorcraft_doc::{NodeId, Selection, SymbolPart};
use super::select_anchors;
type Items = Vec<(NodeId, BTreeSet<(usize, usize)>)>;
/// Selects `items` one path at a time: a clear, then an add and an anchor insert per path.
fn one_by_one(sel: &mut Selection, items: Items) {
sel.clear();
for (id, refs) in items {
sel.add(id);
sel.anchors.insert(id, refs);
}
}
/// A selection with every field set, as one left by earlier selecting.
fn busy() -> Selection {
let mut sel = Selection::default();
sel.set([NodeId(90), NodeId(91)]);
sel.anchors.insert(NodeId(90), BTreeSet::from([(0, 0)]));
sel.segments.insert(NodeId(91), BTreeSet::from([(0, 1)]));
sel.key = Some(NodeId(91));
sel.target = Some(NodeId(92));
sel.slices = vec![NodeId(93)];
sel.guides = vec![NodeId(94)];
sel.key_guide = Some(NodeId(94));
sel.key_anchor = Some((NodeId(90), (0, 0)));
sel.symbol_part = Some(SymbolPart { instance: NodeId(95), path: vec![0, 1] });
sel.on_path_member = Some(NodeId(96));
sel.new_art = Arc::new(HashSet::from([NodeId(97)]));
sel
}
fn refs(n: usize) -> BTreeSet<(usize, usize)> {
(0..n).map(|i| (0, i)).collect()
}
#[test]
fn select_anchors_gives_the_selection_one_by_one_selecting_gives() {
let cases: Vec<Items> = vec![
vec![],
vec![(NodeId(1), refs(4))],
(1..=500).map(|i| (NodeId(i), refs(4))).collect(),
// A path listed again keeps its first place and its last anchors.
vec![(NodeId(3), refs(1)), (NodeId(1), refs(4)), (NodeId(3), refs(2)), (NodeId(2), BTreeSet::new()), (NodeId(1), refs(3))],
];
for items in cases {
for start in [Selection::default(), busy()] {
let (mut want, mut got) = (start.clone(), start);
one_by_one(&mut want, items.clone());
select_anchors(&mut got, items.clone());
assert_eq!(got, want, "{items:?}");
}
}
let mut got = busy();
select_anchors(&mut got, vec![(NodeId(3), refs(1)), (NodeId(1), refs(4)), (NodeId(3), refs(2))]);
assert_eq!(got.objects, vec![NodeId(3), NodeId(1)]);
assert_eq!(got.anchors[&NodeId(3)], refs(2));
assert!(got.segments.is_empty() && got.slices.is_empty() && got.guides.is_empty());
assert_eq!((got.key, got.target, got.key_guide, got.key_anchor, got.on_path_member), (None, None, None, None, None));
assert!(got.symbol_part.is_none());
assert_eq!(*got.new_art, HashSet::from([NodeId(97)]), "selecting leaves the new-art list alone");
}