Merge pull request #8176 from dotCooCoo/perf/task-bar-has-colors-early-exit

Task bar: the Recolor check stops at the first color
This commit is contained in:
BFlat
2026-11-17 02:51:01 +07:00
committed by GitHub
5 changed files with 302 additions and 10 deletions
+12
View File
@@ -608,6 +608,18 @@ impl Node {
}
}
/// [`Self::walk`] that ends at the first node for which `f` returns [`std::ops::ControlFlow::Break`]: no
/// node after it is visited, and the break is returned.
pub fn try_walk<'a, B>(&'a self, f: &mut impl FnMut(&'a Node) -> std::ops::ControlFlow<B>) -> std::ops::ControlFlow<B> {
f(self)?;
if let Some(ch) = self.children() {
for c in ch {
c.try_walk(f)?;
}
}
std::ops::ControlFlow::Continue(())
}
/// Count of nodes in this subtree, ruler guides left out.
pub fn count(&self) -> usize {
let mut n = 0;
+28
View File
@@ -805,3 +805,31 @@ fn mapping_ruler_guide_positions_looks_at_each_child_of_nested_layers_once() {
assert!(Arc::ptr_eq(&group.children().unwrap()[0], &twice));
assert_eq!(calls, 0);
}
/// `try_walk` visits in `walk`'s order and visits nothing after the node that breaks it: not its
/// children, its later siblings or its ancestors' later siblings.
#[test]
fn try_walk_ends_at_the_break() {
use std::ops::ControlFlow;
let leaf = |id| Arc::new(Node::group(NodeId(id), vec![]));
let inner = Node::group(NodeId(2), vec![leaf(3), leaf(4), leaf(5)]);
let root = Node::group(NodeId(1), vec![Arc::new(inner), leaf(6), leaf(7)]);
let mut all = vec![];
root.walk(&mut |n| all.push(n.id));
let mut seen = vec![];
assert_eq!(
root.try_walk(&mut |n| {
seen.push(n.id);
ControlFlow::<()>::Continue(())
}),
ControlFlow::Continue(())
);
assert_eq!(seen, all, "without a break it visits what walk visits, in the same order");
seen.clear();
let flow = root.try_walk(&mut |n| {
seen.push(n.id);
if n.id == NodeId(3) { ControlFlow::Break(n.id) } else { ControlFlow::Continue(()) }
});
assert_eq!(flow, ControlFlow::Break(NodeId(3)));
assert_eq!(seen, [NodeId(1), NodeId(2), NodeId(3)]);
}
+239 -5
View File
@@ -8,6 +8,7 @@
//! change; [`Document::map_paints`] replaces whole paints of any kind in the same places.
use std::collections::{HashMap, HashSet};
use std::ops::ControlFlow;
use std::sync::Arc;
use vectorcraft_color::swatch::REGISTRATION;
@@ -340,18 +341,39 @@ fn map_style_paint(attrs: &mut serde_json::Map<String, serde_json::Value>, key:
/// and gradient stops (with their link), a dynamic symbol instance's own paints
/// ([`override_paints`]), then mesh points (unlinked), in paint order.
pub fn node_colors(n: &Node, f: &mut dyn FnMut(&Color, Option<&str>)) {
n.walk(&mut |m| {
let _ = try_node_colors::<()>(n, &mut |c, s| {
f(c, s);
ControlFlow::Continue(())
});
}
/// Whether the subtree of `n` holds any color [`node_colors`] visits. It stops at the first one.
pub fn node_has_colors(n: &Node) -> bool {
try_node_colors(n, &mut |_, _| ControlFlow::Break(())).is_break()
}
/// [`node_colors`] that ends at the first color for which `f` returns [`ControlFlow::Break`]: no
/// paint or node after it is visited.
fn try_node_colors<B>(n: &Node, f: &mut dyn FnMut(&Color, Option<&str>) -> ControlFlow<B>) -> ControlFlow<B> {
n.try_walk(&mut |m| {
for p in node_paints(m).chain(override_paints(m)) {
match p {
Paint::Solid { color, swatch, .. } => f(color, swatch.as_deref()),
Paint::Gradient(g) => g.gradient.stops.iter().for_each(|s| f(&s.color, s.swatch.as_deref())),
Paint::Solid { color, swatch, .. } => f(color, swatch.as_deref())?,
Paint::Gradient(g) => {
for s in &g.gradient.stops {
f(&s.color, s.swatch.as_deref())?;
}
}
_ => {}
}
}
if let NodeKind::Mesh(mesh) = &m.kind {
mesh.points.iter().for_each(|p| f(&p.color, None));
for p in &mesh.points {
f(&p.color, None)?;
}
}
});
ControlFlow::Continue(())
})
}
fn item_paint(it: &AppearanceItem) -> &Paint {
@@ -821,4 +843,216 @@ mod tests {
node_colors(d.node(b).unwrap(), &mut |c, s| colors.push((*c, s.map(str::to_string))));
assert_eq!(colors, [(Color::rgb(1.0, 0.0, 0.0), None)]);
}
/// The colors [`node_colors`] visits, in its order, by a [`Node::walk`] that reads the whole subtree.
fn full_walk(n: &Node, f: &mut dyn FnMut(&Color, Option<&str>)) {
n.walk(&mut |m| {
for p in node_paints(m).chain(override_paints(m)) {
match p {
Paint::Solid { color, swatch, .. } => f(color, swatch.as_deref()),
Paint::Gradient(g) => g.gradient.stops.iter().for_each(|s| f(&s.color, s.swatch.as_deref())),
_ => {}
}
}
if let NodeKind::Mesh(mesh) = &m.kind {
mesh.points.iter().for_each(|p| f(&p.color, None));
}
});
}
/// Whether [`full_walk`] finds a color in `n`.
fn full_walk_has_colors(n: &Node) -> bool {
let mut found = false;
full_walk(n, &mut |_, _| found = true);
found
}
/// The colors of `n` by [`full_walk`], in order.
fn full_walk_colors(n: &Node) -> Vec<(Color, Option<String>)> {
let mut out = vec![];
full_walk(n, &mut |c, s| out.push((*c, s.map(str::to_string))));
out
}
fn square(id: u64, fill: Paint, stroke: Paint) -> Node {
Node::path(NodeId(id), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::basic(fill, stroke, 1.0))
}
fn holder(id: u64, kind: fn(Vec<Arc<Node>>) -> NodeKind, children: Vec<Node>) -> Node {
Node::new(NodeId(id), kind(children.into_iter().map(Arc::new).collect()))
}
/// One node of each kind of paint [`node_colors`] reads or skips, with whether it shows a color.
fn color_cases() -> Vec<(&'static str, Node, bool)> {
let red = Paint::solid(Color::rgb(1.0, 0.0, 0.0));
let gradient = |stops: usize| {
let mut g = vectorcraft_color::Gradient::default();
g.stops.truncate(stops);
Paint::Gradient(Box::new(vectorcraft_color::GradientPaint::new(g)))
};
let pattern = Paint::Pattern { pattern: "Dots".into(), xf: vectorcraft_geom::Affine::IDENTITY };
let area = shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0));
let mesh = crate::GradientMesh::for_path_with(&area, 2, 2, &|_| (Color::BLACK, 1.0)).unwrap();
let mut empty_mesh = mesh.clone();
empty_mesh.points.clear();
let text = |fill: Paint| {
let style = crate::CharStyle { fill, stroke: Paint::None, ..Default::default() };
let mut n =
Node::new(NodeId(20), NodeKind::Text(Box::new(crate::TextObject::point(vectorcraft_geom::Point::new(0.0, 0.0), "Hi", style))));
n.appearance = Appearance::basic(Paint::None, Paint::None, 0.0).into();
n
};
let image = |colorize: Option<Paint>| {
let im = crate::ImageObject {
key: "k".into(),
width: 2,
height: 2,
xf: vectorcraft_geom::Affine::IDENTITY,
link: None,
placement: Default::default(),
pixels: None,
};
let mut n = Node::new(NodeId(21), NodeKind::Image(Box::new(im)));
n.appearance = Appearance::basic(Paint::None, Paint::None, 0.0).into();
n.extras_mut().colorize = colorize.map(Box::new);
n
};
let overridden = |fill: Option<Paint>| {
let mut n = square(22, Paint::None, Paint::None);
n.extras_mut().symbol_overrides = vec![crate::SymbolOverride { path: vec![0], fill, ..Default::default() }];
n
};
let masked = {
let mut n = square(23, Paint::None, Paint::None);
n.extras_mut().mask = Some(Box::new(crate::OpacityMask::new(square(24, red.clone(), Paint::None), true)));
n
};
let group = |children| NodeKind::Group { children, clip: false };
let clip = |children| NodeKind::Group { children, clip: true };
let compound = |children| NodeKind::Compound { children, rule: Default::default() };
let blend = |children| NodeKind::Blend { children, spec: Default::default() };
let blank = |id| square(id, Paint::None, Paint::None);
vec![
("no paint", blank(1), false),
("solid fill", square(2, red.clone(), Paint::None), true),
("solid stroke", square(3, Paint::None, red.clone()), true),
("pattern only", square(4, pattern.clone(), pattern.clone()), false),
("gradient without stops", square(5, gradient(0), Paint::None), false),
("gradient with stops", square(6, gradient(2), Paint::None), true),
("mesh with points", Node::new(NodeId(7), NodeKind::Mesh(mesh)), true),
("mesh without points", Node::new(NodeId(8), NodeKind::Mesh(empty_mesh)), false),
("text run fill", text(red.clone()), true),
("text without paint", text(Paint::None), false),
("image colorize", image(Some(red.clone())), true),
("image without colorize", image(None), false),
("image with a pattern colorize", image(Some(pattern.clone())), false),
("symbol override fill", overridden(Some(red.clone())), true),
("symbol override without paint", overridden(None), false),
("color only in the opacity mask", masked, false),
("group, color first", holder(30, group, vec![square(31, red.clone(), Paint::None), blank(32), blank(33)]), true),
("group, color last", holder(34, group, vec![blank(35), blank(36), square(37, red.clone(), Paint::None)]), true),
("group without color", holder(38, group, vec![blank(39), blank(40)]), false),
("clip group", holder(41, clip, vec![blank(42), square(43, red.clone(), Paint::None)]), true),
("compound shape", holder(44, compound, vec![blank(45), square(46, gradient(3), Paint::None)]), true),
("blend", holder(47, blend, vec![blank(48), blank(49)]), false),
(
"nested groups, color deepest",
holder(50, group, vec![blank(51), holder(52, group, vec![holder(53, group, vec![blank(54), square(55, red.clone(), Paint::None)])])]),
true,
),
]
}
#[test]
fn has_colors_matches_the_full_walk() {
for (label, n, expected) in color_cases() {
let all = full_walk_colors(&n);
assert_eq!(node_has_colors(&n), full_walk_has_colors(&n), "{label}");
assert_eq!(full_walk_has_colors(&n), !all.is_empty(), "{label}");
assert_eq!(node_has_colors(&n), expected, "{label}");
let mut listed = vec![];
node_colors(&n, &mut |c, s| listed.push((*c, s.map(str::to_string))));
assert_eq!(listed, all, "{label}: node_colors lists the same colors in the same order");
}
}
#[test]
fn has_colors_stops_at_the_first_color() {
let red = Paint::solid(Color::rgb(1.0, 0.0, 0.0));
let mut stops = vectorcraft_color::Gradient::default();
stops.stops.push(stops.stops[0].clone());
let gradient = Paint::Gradient(Box::new(vectorcraft_color::GradientPaint::new(stops)));
let mut later = vec![square(2, gradient, red.clone())];
later.extend((3..100).map(|id| square(id, red.clone(), red.clone())));
let n = holder(1, |children| NodeKind::Group { children, clip: false }, later);
let (mut colors, mut nodes) = (0, 0);
let flow = try_node_colors(&n, &mut |_, _| {
colors += 1;
ControlFlow::Break(())
});
assert!(flow.is_break());
assert_eq!(colors, 1, "the first gradient stop ends the walk: no later stop, stroke or object is read");
let flow = n.try_walk(&mut |m| {
nodes += 1;
if node_paints(m).next().is_some() { ControlFlow::Break(()) } else { ControlFlow::Continue(()) }
});
assert!(flow.is_break());
assert_eq!(nodes, 2, "the group, then its first member: none of the 97 after it");
}
/// Times [`node_has_colors`] against the full walk it replaces on one group of 100,000 paths, alternating the two in
/// one process: `cargo test --release -p vectorcraft-doc --lib -- --ignored --nocapture has_colors_timing`.
#[test]
#[ignore = "a timing, not a check"]
fn has_colors_timing() {
/// This thread's CPU time in nanoseconds on Linux; the elapsed time since `start` elsewhere.
fn clock(start: std::time::Instant) -> u128 {
let cpu = std::fs::read_to_string("/proc/thread-self/schedstat").ok();
cpu.and_then(|s| s.split_whitespace().next()?.parse().ok()).unwrap_or_else(|| start.elapsed().as_nanos())
}
let red = Paint::solid(Color::rgb(1.0, 0.0, 0.0));
let count = 100_000u64;
let group = |colored: &dyn Fn(u64) -> bool| {
let children =
(0..count).map(|i| if colored(i) { square(i + 1, red.clone(), Paint::None) } else { square(i + 1, Paint::None, Paint::None) });
holder(0, |children| NodeKind::Group { children, clip: false }, children.collect())
};
let cases = [
("every path filled", group(&|_| true)),
("only the last path filled", group(&|i| i == count - 1)),
("no path filled", group(&|_| false)),
];
let start = std::time::Instant::now();
println!("clock: {}", if std::fs::metadata("/proc/thread-self/schedstat").is_ok() { "thread CPU time" } else { "wall clock" });
// The time per call of `f` on `n`: batches of calls double until one takes at least 100 ms.
let per_call = |f: fn(&Node) -> bool, n: &Node| {
let mut calls = 1u32;
loop {
let t = clock(start);
for _ in 0..calls {
std::hint::black_box(f(std::hint::black_box(n)));
}
let spent = clock(start) - t;
if spent >= 100_000_000 || calls >= 1 << 30 {
return (spent as f64 / f64::from(calls), calls);
}
calls *= 2;
}
};
for (label, n) in &cases {
assert_eq!(full_walk_has_colors(n), node_has_colors(n), "{label}");
let (mut old, mut new) = (vec![], vec![]);
for _ in 0..7 {
old.push(per_call(full_walk_has_colors, n));
new.push(per_call(node_has_colors, n));
}
old.sort_by(|a, b| a.0.total_cmp(&b.0));
new.sort_by(|a, b| a.0.total_cmp(&b.0));
let (o, nw) = (old[3], new[3]);
println!(
"{label}: full walk {:.1} ns per call ({} calls), node_has_colors {:.1} ns per call ({} calls), median of 7 runs",
o.0, o.1, nw.0, nw.1
);
}
}
}
+1 -5
View File
@@ -22,11 +22,7 @@ pub(crate) fn task_bar_shows_fonts(ctx: &egui::Context) -> bool {
/// Whether the objects `ids` (or anything inside them) are painted with a colour: a solid colour,
/// a gradient's stops or a mesh's points, which Recolor Artwork can change.
pub(crate) fn has_colors(doc: &vectorcraft_doc::Document, ids: &[vectorcraft_doc::NodeId]) -> bool {
ids.iter().filter_map(|id| doc.node(*id)).any(|n| {
let mut found = false;
vectorcraft_doc::swatches::node_colors(n, &mut |_, _| found = true);
found
})
ids.iter().filter_map(|id| doc.node(*id)).any(vectorcraft_doc::swatches::node_has_colors)
}
/// The bounds of the artboards the Artboard tool has selected ([`crate::panels::artboards::picked`]).
+22
View File
@@ -11228,6 +11228,28 @@ fn the_task_bar_recolors_coloured_art() {
assert!(app.ui.dialog.is_none(), "no Recolor button without colours");
}
/// Recolor Artwork shows in the bars when any selected object has a color, whichever of them it is,
/// and not for an empty selection or ids that are gone.
#[test]
fn has_colors_looks_at_every_selected_object() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
let mut rect = |x: i32| {
vectorcraft_doc::NodeId(s.execute("shape.rectangle", &json!({"x": x, "y": 10, "width": 40, "height": 40})).unwrap()["id"].as_u64().unwrap())
};
let (blank, colored) = (rect(10), rect(80));
s.execute("paint.setFill", &json!({"ids": [blank.0], "none": true})).unwrap();
s.execute("paint.setStroke", &json!({"ids": [blank.0], "none": true})).unwrap();
s.execute("paint.setFill", &json!({"ids": [colored.0], "color": "#cc3322"})).unwrap();
let doc = &s.active().unwrap().doc;
let gone = vectorcraft_doc::NodeId(u64::MAX);
assert!(!has_colors(doc, &[]), "nothing selected");
assert!(!has_colors(doc, &[gone]), "an id that is gone");
assert!(!has_colors(doc, &[blank, gone]));
assert!(has_colors(doc, &[blank, colored]), "the color is on the second object");
assert!(has_colors(doc, &[colored, blank, gone]), "the color is on the first object");
}
/// The Contextual Task Bar's Lock button locks the selected member in isolation mode and the
/// selected group outside isolation mode.
#[test]