From 3e57f8311c6885af155fb4fc9e385d0c65f33bc4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 03:04:09 -0800 Subject: [PATCH] Brushes: brush_targets drops repeated paths with a set instead of a linear search of its output brush_targets checked each brushable node against every target it had already collected, so P targets cost about P^2/2 comparisons. It now keeps a HashSet of the ids it has pushed. The output vector is unchanged: the same ids in the same walk order, each kept where it is first reached. brush.apply, brush.remove, Expand on brushed paths and the brush library's apply all collect their targets through it. A unit test checks the exact result for guides, clipping paths, compound members, nested compounds, repeated and overlapping roots and a missing id, and compares it with a copy of the old collector. --- crates/engine/src/cmd/brushsym/brushes.rs | 94 ++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/brushsym/brushes.rs b/crates/engine/src/cmd/brushsym/brushes.rs index 89f3893b24..7a92ff130a 100644 --- a/crates/engine/src/cmd/brushsym/brushes.rs +++ b/crates/engine/src/cmd/brushsym/brushes.rs @@ -5,15 +5,16 @@ use super::{ // ---------- brushes ---------- -/// Brushable nodes (paths and compound paths) under `ids`. +/// Brushable nodes (paths and compound paths) under `ids`, in walk order, each listed once where it is first reached. pub(crate) fn brush_targets(d: &Document, ids: &[NodeId]) -> Vec { let mut out = vec![]; + let mut seen = std::collections::HashSet::new(); for id in ids { let Some(n) = d.node(*id) else { continue }; n.walk(&mut |c| { let own = matches!(c.kind, NodeKind::Path { guide: false, clipping: false, .. } | NodeKind::Compound { .. }); let in_compound = d.parent_of(c.id).and_then(|p| d.node(p)).is_some_and(|p| matches!(p.kind, NodeKind::Compound { .. })); - if own && !in_compound && !out.contains(&c.id) { + if own && !in_compound && seen.insert(c.id) { out.push(c.id); } }); @@ -748,3 +749,92 @@ pub(super) fn brush_freehand(s: &mut Session, p: &Value) -> Result { let checked = crate::schemas::check(spec, p, s.replaying())?; crate::cmd::draw2::freehand_brushed(s, checked.as_ref().unwrap_or(p), name.as_deref()) } + +#[cfg(test)] +mod tests { + use super::*; + use vectorcraft_doc::Appearance; + use vectorcraft_geom::PathData; + + /// The collector with a linear search of its output, kept to compare results with. + fn listed_targets(d: &Document, ids: &[NodeId]) -> Vec { + let mut out = vec![]; + for id in ids { + let Some(n) = d.node(*id) else { continue }; + n.walk(&mut |c| { + let own = matches!(c.kind, NodeKind::Path { guide: false, clipping: false, .. } | NodeKind::Compound { .. }); + let in_compound = d.parent_of(c.id).and_then(|p| d.node(p)).is_some_and(|p| matches!(p.kind, NodeKind::Compound { .. })); + if own && !in_compound && !out.contains(&c.id) { + out.push(c.id); + } + }); + } + out + } + + fn path(d: &mut Document) -> Node { + Node::path(d.alloc_id(), PathData::default(), Appearance::default()) + } + + fn compound(d: &mut Document, children: Vec) -> Node { + Node::new(d.alloc_id(), NodeKind::Compound { children: children.into_iter().map(Arc::new).collect(), rule: Default::default() }) + } + + fn add(d: &mut Document, node: Node) -> NodeId { + let layer = d.layers.first().map(|l| l.id); + d.insert(layer, usize::MAX, node).unwrap() + } + + #[test] + fn brush_targets_list_each_target_once_in_first_reached_order() { + let mut d = Document::new(400.0, 300.0); + let a = path(&mut d); + let a = add(&mut d, a); + let mut guide = path(&mut d); + if let NodeKind::Path { guide: g, .. } = &mut guide.kind { + *g = true; + } + let guide = add(&mut d, guide); + let mut clip = path(&mut d); + if let NodeKind::Path { clipping, .. } = &mut clip.kind { + *clipping = true; + } + let clip = add(&mut d, clip); + // A group holding a path and a compound path of two members. + let p1 = path(&mut d); + let p1_id = p1.id; + let (m1, m2) = (path(&mut d), path(&mut d)); + let m1_id = m1.id; + let cp1 = compound(&mut d, vec![m1, m2]); + let cp1_id = cp1.id; + let group = Node::group(d.alloc_id(), vec![Arc::new(p1), Arc::new(cp1)]); + let group = add(&mut d, group); + // A compound path holding a compound path. + let m3 = path(&mut d); + let cp3 = compound(&mut d, vec![m3]); + let cp3_id = cp3.id; + let cp2 = compound(&mut d, vec![cp3]); + let cp2 = add(&mut d, cp2); + + let ids = [cp1_id, a, group, p1_id, a, guide, clip, m1_id, cp2, cp3_id, NodeId(u64::MAX)]; + let got = brush_targets(&d, &ids); + assert_eq!(got, [cp1_id, a, p1_id, cp2], "repeats keep their first place; guides, clipping paths and compound members are left out"); + assert_eq!(got, listed_targets(&d, &ids)); + assert!(brush_targets(&d, &[]).is_empty()); + + // Many paths in groups, each group listed with some of its members. + let mut ids = vec![]; + for _ in 0..50 { + let members: Vec = (0..20).map(|_| path(&mut d)).collect(); + let member_ids: Vec = members.iter().map(|m| m.id).collect(); + let g = Node::group(d.alloc_id(), members.into_iter().map(Arc::new).collect()); + let g = add(&mut d, g); + ids.push(member_ids[7]); + ids.push(g); + ids.push(member_ids[3]); + } + let got = brush_targets(&d, &ids); + assert_eq!(got.len(), 50 * 20); + assert_eq!(got, listed_targets(&d, &ids)); + } +}