mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 18:25:34 +01:00
Performance: a hover reads each plain path's effects once
The culling box of every path a click or hover passes asked whether the path draws geometry effects by reading its effects twice: once on the path itself, and again in the walk of the path and what it holds, which starts at the path. The walk alone now answers, so a hover over many plain paths reads half as much. The walk also stops at the first object with an effect, for drawn art and for a clipping path's region, instead of visiting every member of a large compound path or group after the answer is known.
This commit is contained in:
1 parent
52de8120d3
commit
65ebc72ec4
2 files changed
+57
-11
No files matched your search
+15
-11
@@ -2,6 +2,7 @@
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::ops::ControlFlow;
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
@@ -403,6 +404,8 @@ thread_local! {
|
||||
thread_local! {
|
||||
/// How many objects' drawn art [`drawn`] evaluated on this thread.
|
||||
static DRAWN_EVALUATED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
/// How many times [`has_own_effects`] read an object's effects on this thread.
|
||||
static OWN_EFFECTS_READ: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// While a guard lives, [`drawn`] gives no art: the art it gave already has every geometry effect
|
||||
@@ -467,7 +470,7 @@ fn plain<T>(mask: bool, f: impl FnOnce() -> T) -> T {
|
||||
/// kept alone.
|
||||
fn drawn(symbols: &SymbolBoxes, n: &Node) -> Option<(Rc<Vec<Node>>, Option<Rect>)> {
|
||||
let f = DRAWN_ART.get()?;
|
||||
if DRAWN_OFF.get() || !evaluates(n) || !(has_own_effects(n) || draws_effects(symbols, n)) {
|
||||
if DRAWN_OFF.get() || !evaluates(n) || !draws_effects(symbols, n) {
|
||||
return None;
|
||||
}
|
||||
let doc = symbols.document();
|
||||
@@ -508,7 +511,7 @@ fn evaluates(n: &Node) -> bool {
|
||||
match &n.kind {
|
||||
NodeKind::Path { guide, .. } => !guide,
|
||||
NodeKind::Compound { .. } => true,
|
||||
NodeKind::Group { .. } | NodeKind::Layer { .. } => has_own_effects(n) || !n.appearance.items.is_empty(),
|
||||
NodeKind::Group { .. } | NodeKind::Layer { .. } => !n.appearance.items.is_empty() || has_own_effects(n),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -1120,23 +1123,24 @@ pub(crate) fn clip_region_inputs() -> (u64, bool) {
|
||||
/// Does `clip`, or anything it holds, have a visible effect of its own or on a fill or stroke? Only
|
||||
/// then can the registered [`ClipRegion`] find a geometry effect in it.
|
||||
fn has_effects(clip: &Node) -> bool {
|
||||
let mut found = false;
|
||||
clip.walk(&mut |n| found |= has_own_effects(n));
|
||||
found
|
||||
clip.try_walk(&mut |n| if has_own_effects(n) { ControlFlow::Break(()) } else { ControlFlow::Continue(()) }).is_break()
|
||||
}
|
||||
|
||||
/// Does `n`, anything it holds, or the art of a symbol an instance in it shows have a visible
|
||||
/// effect? Only then can the registered [`DrawnArt`] find a geometry effect in it.
|
||||
/// effect? Only then can the registered [`DrawnArt`] find a geometry effect in it. The walk starts
|
||||
/// at `n`, so its own effects are read once, here, and it ends at the first object with an effect.
|
||||
fn draws_effects(symbols: &SymbolBoxes, n: &Node) -> bool {
|
||||
let mut found = false;
|
||||
n.walk(&mut |c| {
|
||||
found = found || has_own_effects(c) || matches!(&c.kind, NodeKind::SymbolInstance { symbol, .. } if symbols.has_effects(symbol));
|
||||
});
|
||||
found
|
||||
n.try_walk(&mut |c| {
|
||||
let has = has_own_effects(c) || matches!(&c.kind, NodeKind::SymbolInstance { symbol, .. } if symbols.has_effects(symbol));
|
||||
if has { ControlFlow::Break(()) } else { ControlFlow::Continue(()) }
|
||||
})
|
||||
.is_break()
|
||||
}
|
||||
|
||||
/// Does `n` itself have a visible effect, of its own or on one of its own fills or strokes?
|
||||
fn has_own_effects(n: &Node) -> bool {
|
||||
#[cfg(test)]
|
||||
OWN_EFFECTS_READ.with(|c| c.set(c.get() + 1));
|
||||
let visible = |e: &crate::Effect| e.visible;
|
||||
n.appearance.effects.iter().any(visible) || n.appearance.items.iter().any(|i| i.effects().iter().any(visible))
|
||||
}
|
||||
|
||||
@@ -3569,3 +3569,45 @@ fn a_shaper_groups_original_shapes_in_symbol_art_are_not_picked() {
|
||||
assert_eq!(symbol_part_at(&d, n, Point::new(130.0, 50.0), opt), None, "over the erased original shape");
|
||||
assert_eq!(symbol_part_at(&d, n, Point::new(50.0, 50.0), opt), Some(vec![0, 1]), "over the shown face");
|
||||
}
|
||||
|
||||
/// A hover over empty canvas past many plain paths reads each path's effects once, and the layer's
|
||||
/// once.
|
||||
#[test]
|
||||
fn hovers_past_plain_paths_read_each_paths_effects_once() {
|
||||
set_drawn_art(shifted_art);
|
||||
let mut d = Document::new(1000.0, 1000.0);
|
||||
let l = d.layers[0].id;
|
||||
let paths = 100;
|
||||
for i in 0..paths {
|
||||
let n = square(&mut d, (i % 10) as f64 * 60.0, (i / 10) as f64 * 60.0);
|
||||
d.insert(Some(l), 0, n).unwrap();
|
||||
}
|
||||
let reads = || OWN_EFFECTS_READ.with(std::cell::Cell::get);
|
||||
let before = reads();
|
||||
assert!(hit_test(&d, Point::new(900.0, 900.0), HitOptions::default()).is_none());
|
||||
let read = reads() - before;
|
||||
assert_eq!(read, paths + 1, "each path's effects and the layer's are read once");
|
||||
assert!(hit_at(&d, 25.0, 25.0, HitOptions::default()).is_some(), "a square is still hit");
|
||||
}
|
||||
|
||||
/// Whether a compound clipping path, or a compound path, has a live effect is settled by the first
|
||||
/// object found with one: the members after it are not read.
|
||||
#[test]
|
||||
fn an_effect_on_a_compound_path_is_found_without_reading_its_members() {
|
||||
set_clip_region(moved_clip_region);
|
||||
set_drawn_art(shifted_art);
|
||||
let mut d = Document::new(1000.0, 1000.0);
|
||||
let members: Vec<_> = (0..100)
|
||||
.map(|i| Arc::new(Node::path(d.alloc_id(), shapes::rectangle(Rect::new(i as f64, 0.0, i as f64 + 5.0, 5.0)), Default::default())))
|
||||
.collect();
|
||||
let mut mask = Node::new(d.alloc_id(), NodeKind::Compound { children: members, rule: FillRule::NonZero });
|
||||
mask.appearance.effects.push(test_move());
|
||||
let reads = || OWN_EFFECTS_READ.with(std::cell::Cell::get);
|
||||
let before = reads();
|
||||
assert!(has_effects(&mask));
|
||||
assert_eq!(reads() - before, 1, "the clipping path's own effect ends the search");
|
||||
let symbols = d.symbol_boxes();
|
||||
let before = reads();
|
||||
assert!(draws_effects(&symbols, &mask));
|
||||
assert_eq!(reads() - before, 1, "the compound path's own effect ends the search");
|
||||
}
|
||||
Reference in new issue
Block a user