Cut Path: the cut ends are selected with one set call

Cut Path at Selected Anchor Points cleared the selection and then added
each cut path in turn, and each add searched the paths already added, so
selecting the cut ends took time that grows with the square of the number
of cut paths. The command now selects them with one Selection::set call,
which skips repeats through a hash set and keeps the order in which each
path first appears, and then adds each cut end's anchor. The selection is
the same in every field; a new test compares it with the one-by-one build
on open, closed and multi-subpath cuts, long lists and an empty list.

The gain shows on large documents. With the 8 middle anchors of each of
5,000 open paths of 10 anchors selected, building the selection of 40,000
cut ends takes 9.7 ms instead of 232 ms of CPU time, and with 2,000 paths
3.2 ms instead of 39 ms (medians of 7 runs of the new
cut_path_select_bench example, both ways in one process).
This commit is contained in:
dotCooCoo
2026-11-16 10:02:01 -08:00
parent f94e4c55ce
commit 58f1129123
3 changed files with 238 additions and 5 deletions
+70 -5
View File
@@ -1874,20 +1874,85 @@ fn cut_at_anchors(s: &mut Session, _: &Value) -> Result<Value> {
}
cut.finish(d)?;
// Each cut leaves one of its two coincident anchors selected, so a drag pulls the ends apart.
sel.clear();
for (id, a) in picked {
sel.add(id);
sel.anchors.entry(id).or_default().insert(a);
}
select_cut_ends(sel, &picked);
Ok(all)
})?;
Ok(super::draw2::ids_json(&ids))
}
/// Replaces the selection with the anchors in `picked`: each id once, in the order it first
/// appears, with every anchor listed for it. An empty `picked` clears the selection.
fn select_cut_ends(sel: &mut Selection, picked: &[(NodeId, (usize, usize))]) {
sel.set(picked.iter().map(|(id, _)| *id));
for (id, a) in picked {
sel.anchors.entry(*id).or_default().insert(*a);
}
}
#[cfg(test)]
mod tests {
use super::*;
/// `select_cut_ends` by clearing the selection and adding each id in turn, kept as the
/// reference the one-call version must match.
fn select_cut_ends_one_by_one(sel: &mut Selection, picked: &[(NodeId, (usize, usize))]) {
sel.clear();
for (id, a) in picked {
sel.add(*id);
sel.anchors.entry(*id).or_default().insert(*a);
}
}
/// A selection with every field set, the art made since the last deselect included.
fn busy_selection() -> Selection {
Selection {
objects: vec![NodeId(90), NodeId(91)],
anchors: [(NodeId(90), [(0, 3)].into())].into(),
segments: [(NodeId(91), [(0, 1)].into())].into(),
key: Some(NodeId(90)),
target: Some(NodeId(91)),
slices: vec![NodeId(92)],
guides: vec![NodeId(93)],
key_guide: Some(NodeId(93)),
key_anchor: Some((NodeId(90), (0, 3))),
symbol_part: Some(vectorcraft_doc::SymbolPart { instance: NodeId(94), path: vec![0, 2] }),
on_path_member: Some(NodeId(95)),
new_art: Arc::new([NodeId(90), NodeId(97)].into()),
}
}
/// The cut ends of open paths, of an opened closed path, of a path of several subpaths (one id
/// with several anchors, first seen before other ids), long lists and an empty one give the
/// same selection, every field, as clearing it and adding each id in turn.
#[test]
fn selecting_cut_ends_matches_adding_them_one_by_one() {
let n = NodeId;
let mut cases: Vec<Vec<(NodeId, (usize, usize))>> = vec![
vec![],
vec![(n(1), (0, 0)), (n(2), (0, 0))],
vec![(n(5), (0, 0))],
vec![(n(7), (0, 0)), (n(3), (1, 0)), (n(8), (0, 0)), (n(3), (2, 0)), (n(7), (0, 0)), (n(3), (0, 0))],
];
let mut rng = Lcg(11);
for len in [50, 2_000] {
cases.push((0..len).map(|_| (n(rng.below(300) + 1), (rng.below(4) as usize, 0))).collect());
}
for picked in &cases {
for start in [Selection::default(), busy_selection()] {
let (mut ours, mut theirs) = (start.clone(), start.clone());
select_cut_ends(&mut ours, picked);
select_cut_ends_one_by_one(&mut theirs, picked);
assert_eq!(ours, theirs, "{picked:?}");
assert_eq!(ours.new_art, start.new_art);
}
}
let mut sel = busy_selection();
select_cut_ends(&mut sel, &cases[3]);
assert_eq!(sel.objects, [n(7), n(3), n(8)]);
assert_eq!(sel.anchors[&n(3)].iter().collect::<Vec<_>>(), [&(0, 0), &(1, 0), &(2, 0)]);
assert_eq!((sel.anchors.len(), sel.key, sel.target, sel.symbol_part.is_some()), (3, None, None, false));
}
/// `merge_coincident` removing each merged anchor from the vector straight away, kept as the
/// reference the one-pass version must match.
fn merge_coincident_by_removal(sp: &mut SubPath, picked: impl Fn(usize) -> bool, join_kind: Option<bool>) -> usize {