mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 09:36:45 +00:00
Merge pull request #8161 from dotCooCoo/perf/cut-selection-one-pass
Cut: take the cut objects out of the selection in one pass
This commit is contained in:
@@ -45,6 +45,32 @@ fn remove_all_does_what_removing_each_does() {
|
||||
assert_eq!(none, s);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_all_does_what_removing_each_does_to_a_key_anchor_and_symbol_part() {
|
||||
for kept in [false, true] {
|
||||
let mut s = Selection::default();
|
||||
for i in 1..=4 {
|
||||
s.add(NodeId(i));
|
||||
}
|
||||
s.anchors.insert(NodeId(2), BTreeSet::from([(0, 0), (0, 1)]));
|
||||
s.anchors.insert(NodeId(3), BTreeSet::from([(0, 2)]));
|
||||
s.key_anchor = Some((NodeId(2), (0, 0)));
|
||||
s.symbol_part = Some(SymbolPart { instance: NodeId(1), path: vec![0] });
|
||||
s.guides = vec![NodeId(10)];
|
||||
s.key_guide = Some(NodeId(10));
|
||||
let ids = if kept { [NodeId(3), NodeId(8), NodeId(3)] } else { [NodeId(2), NodeId(8), NodeId(2)] };
|
||||
let mut one_by_one = s.clone();
|
||||
for id in ids {
|
||||
one_by_one.remove(id);
|
||||
}
|
||||
let mut all = s.clone();
|
||||
all.remove_all(ids);
|
||||
assert_eq!(all, one_by_one);
|
||||
assert_eq!(all.key_anchor.is_some(), kept);
|
||||
assert_eq!((all.symbol_part.clone(), all.guides.clone(), all.key_guide), (None, vec![NodeId(10)], Some(NodeId(10))));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guides_are_selected_on_their_own() {
|
||||
let mut s = Selection::default();
|
||||
|
||||
@@ -1189,9 +1189,7 @@ fn clear(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
d.retain_guides(sel, |g| !g.id.is_some_and(|id| gone.contains(&id)));
|
||||
}
|
||||
if is_explicit {
|
||||
for id in &ids {
|
||||
sel.remove(*id);
|
||||
}
|
||||
sel.remove_all(ids.iter().copied());
|
||||
sel.prune(d);
|
||||
} else {
|
||||
sel.clear();
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
//! Clear deletes its exact targets, including compound members (#786).
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use serde_json::json;
|
||||
use vectorcraft_doc::NodeId;
|
||||
use vectorcraft_doc::{NodeId, Selection};
|
||||
use vectorcraft_engine::{EngineError, Session};
|
||||
|
||||
fn compound() -> (Session, NodeId, NodeId, NodeId) {
|
||||
@@ -225,3 +228,168 @@ fn clear_explicit_ids_preserves_unrelated_selection() {
|
||||
assert!(s.active().unwrap().doc.node(r2).is_none());
|
||||
assert!(s.active().unwrap().selection.is_empty());
|
||||
}
|
||||
|
||||
fn rectangles(s: &mut Session, n: i64) -> Vec<NodeId> {
|
||||
(0..n)
|
||||
.map(|i| {
|
||||
NodeId(s.execute("shape.rectangle", &json!({"x": 10 + 40 * i, "y": 10, "width": 30, "height": 30})).unwrap()["id"].as_u64().unwrap())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Clear with `ids` leaves the selection that removing each id on its own leaves: the other objects
|
||||
/// in their order, their anchor and segment picks, a key object or key anchor that is not removed,
|
||||
/// and the selected guides. Undo brings back the document and the selection, and Redo the result.
|
||||
#[test]
|
||||
fn clear_explicit_ids_leaves_what_removing_each_id_leaves() {
|
||||
for key_anchor in [false, true] {
|
||||
for removed in [[1, 4, 1, 6], [0, 2, 2, 6]] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = rectangles(&mut s, 7);
|
||||
s.execute("guide.add", &json!({"vertical": true, "pos": 10})).unwrap();
|
||||
let guide = s.active().unwrap().doc.ruler_guides()[0].id.unwrap();
|
||||
let sel = &mut s.active_mut().unwrap().selection;
|
||||
// The seventh rectangle is not selected: Clear removes it from the document only.
|
||||
sel.set(r[..6].iter().copied());
|
||||
sel.guides = vec![guide];
|
||||
for (id, anchor) in [(r[1], (0, 1)), (r[2], (0, 2)), (r[4], (0, 0)), (r[5], (0, 2))] {
|
||||
sel.anchors.insert(id, BTreeSet::from([anchor, (0, 1)]));
|
||||
}
|
||||
for id in [r[2], r[5]] {
|
||||
sel.segments.insert(id, BTreeSet::from([(0, 1)]));
|
||||
}
|
||||
if key_anchor {
|
||||
sel.key_anchor = Some((r[4], (0, 0)));
|
||||
} else {
|
||||
sel.key = Some(r[4]);
|
||||
}
|
||||
let before = sel.clone();
|
||||
// The selection is one that `prune` keeps as it is, so Undo can restore it unchanged.
|
||||
let mut pruned = before.clone();
|
||||
pruned.prune(&s.active().unwrap().doc);
|
||||
assert_eq!(pruned, before);
|
||||
let doc_before = s.active().unwrap().doc.clone();
|
||||
let ids: Vec<NodeId> = removed.iter().map(|i| r[*i]).collect();
|
||||
s.execute("edit.clear", &json!({"ids": ids.iter().map(|id| id.0).collect::<Vec<_>>()})).unwrap();
|
||||
let st = s.active().unwrap();
|
||||
let mut expected = before.clone();
|
||||
for id in &ids {
|
||||
expected.remove(*id);
|
||||
}
|
||||
expected.prune(&st.doc);
|
||||
assert_eq!(st.selection, expected, "key anchor {key_anchor}, removed {removed:?}");
|
||||
assert_eq!(st.selection.guides, vec![guide]);
|
||||
assert!(ids.iter().all(|id| st.doc.node(*id).is_none()));
|
||||
let doc_after = st.doc.clone();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.active().unwrap().doc, doc_before);
|
||||
assert_eq!(s.active().unwrap().selection, before);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(s.active().unwrap().doc, doc_after);
|
||||
assert_eq!(s.active().unwrap().selection, expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Cut of many selected objects puts each of them on the clipboard in paint order, removes them and
|
||||
/// the selected guide, and leaves nothing selected. One Undo brings all of it back.
|
||||
#[test]
|
||||
fn cut_of_many_objects_takes_them_all_out_of_the_selection() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = rectangles(&mut s, 40);
|
||||
s.execute("guide.add", &json!({"vertical": true, "pos": 10})).unwrap();
|
||||
let guide = s.active().unwrap().doc.ruler_guides()[0].id.unwrap();
|
||||
let ids: Vec<u64> = r.iter().rev().map(|id| id.0).collect();
|
||||
s.execute("select.set", &json!({"ids": ids})).unwrap();
|
||||
let sel = &mut s.active_mut().unwrap().selection;
|
||||
sel.guides = vec![guide];
|
||||
sel.key = Some(r[7]);
|
||||
let before = sel.clone();
|
||||
let doc_before = s.active().unwrap().doc.clone();
|
||||
s.execute("edit.cut", &json!({})).unwrap();
|
||||
assert_eq!(s.clipboard.nodes.iter().map(|n| n.id).collect::<Vec<_>>(), r);
|
||||
let st = s.active().unwrap();
|
||||
assert!(r.iter().all(|id| st.doc.node(*id).is_none()));
|
||||
assert!(st.doc.ruler_guides().is_empty());
|
||||
assert_eq!(st.selection, Selection::default());
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.active().unwrap().doc, doc_before);
|
||||
assert_eq!(s.active().unwrap().selection, before);
|
||||
}
|
||||
|
||||
/// Timings of taking the cut objects out of a selection of 2,000, 20,000 and 50,000 objects: one
|
||||
/// [`Selection::remove`] per id, as Clear with `ids` did before, against one
|
||||
/// [`Selection::remove_all`], for all of the objects and for every other one. The two run in turn
|
||||
/// in one process, 7 times each, and the two results are compared field by field. It also times
|
||||
/// Cut of every object, with Undo between the runs. Ignored by default; it prints its timings and
|
||||
/// sets no bound on them:
|
||||
/// `cargo test --release -p vectorcraft-engine --test it clear_selection_perf -- --ignored --nocapture`.
|
||||
#[test]
|
||||
#[ignore = "a benchmark: run with --ignored --nocapture"]
|
||||
fn clear_selection_perf() {
|
||||
fn ms(d: &[Duration]) -> String {
|
||||
d.iter().map(|d| format!("{:.3}", d.as_secs_f64() * 1000.0)).collect::<Vec<_>>().join(" ")
|
||||
}
|
||||
fn median(d: &mut [Duration]) -> f64 {
|
||||
d.sort();
|
||||
d[d.len() / 2].as_secs_f64() * 1000.0
|
||||
}
|
||||
for n in [2_000_i64, 20_000, 50_000] {
|
||||
let mut b = vectorcraft_testkit::fixtures::DocBuilder::new(4000.0, 4000.0);
|
||||
let r: Vec<NodeId> = (0..n)
|
||||
.map(|i| {
|
||||
let (x, y) = ((i % 400) as f64 * 10.0, (i / 400) as f64 * 10.0);
|
||||
b.rect(vectorcraft_geom::Rect::new(x, y, x + 8.0, y + 8.0), vectorcraft_color::Color::rgb(0.2, 0.4, 0.6), |_| {})
|
||||
})
|
||||
.collect();
|
||||
let doc = b.build();
|
||||
let mut sel = Selection::default();
|
||||
sel.set(r.iter().copied());
|
||||
for id in r.iter().step_by(10) {
|
||||
sel.anchors.insert(*id, BTreeSet::from([(0, 0)]));
|
||||
}
|
||||
sel.key = Some(r[r.len() / 2]);
|
||||
for (what, ids) in [("all", r.clone()), ("every other", r.iter().copied().step_by(2).collect::<Vec<_>>())] {
|
||||
let (mut each, mut all) = (Vec::new(), Vec::new());
|
||||
for _ in 0..7 {
|
||||
let mut one_by_one = sel.clone();
|
||||
let start = Instant::now();
|
||||
for id in &ids {
|
||||
one_by_one.remove(*id);
|
||||
}
|
||||
one_by_one.prune(&doc);
|
||||
each.push(start.elapsed());
|
||||
let mut at_once = sel.clone();
|
||||
let start = Instant::now();
|
||||
at_once.remove_all(ids.iter().copied());
|
||||
at_once.prune(&doc);
|
||||
all.push(start.elapsed());
|
||||
assert_eq!(at_once, one_by_one);
|
||||
}
|
||||
eprintln!("clear_selection_perf: {n} selected, {what} ({} ids), remove each ms: {}", ids.len(), ms(&each));
|
||||
eprintln!("clear_selection_perf: {n} selected, {what} ({} ids), remove_all ms: {}", ids.len(), ms(&all));
|
||||
eprintln!(
|
||||
"clear_selection_perf: {n} selected, {what}: median remove each {:.3} ms, remove_all {:.3} ms",
|
||||
median(&mut each),
|
||||
median(&mut all)
|
||||
);
|
||||
}
|
||||
let mut s = Session::new();
|
||||
let data = vectorcraft_format::base64_encode(&vectorcraft_format::save(&doc, false));
|
||||
s.execute("document.open", &json!({"name": "perf.vectorcraft", "dataBase64": data})).unwrap();
|
||||
s.execute("select.all", &json!({})).unwrap();
|
||||
let mut cut = Vec::new();
|
||||
for _ in 0..7 {
|
||||
let start = Instant::now();
|
||||
s.execute("edit.cut", &json!({})).unwrap();
|
||||
cut.push(start.elapsed());
|
||||
assert!(s.active().unwrap().selection.objects.is_empty());
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.active().unwrap().selection.objects.len(), r.len());
|
||||
}
|
||||
eprintln!("clear_selection_perf: {n} selected, edit.cut ms: {}", ms(&cut));
|
||||
eprintln!("clear_selection_perf: {n} selected, edit.cut median {:.3} ms", median(&mut cut));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2119,6 +2119,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
A selection with more than 100,000 points to outline shows its bounds instead of each object's outline and anchors. Paths count their anchors. Area type and type on a path count the anchors of their frame or path, and point type counts six: its box's corners and the ends of its baseline marker. Images, symbol instances, placed documents, and area type or type on a path in perspective count the four corners of their box, and gradient meshes and envelopes count their points and the ends of their mesh lines. The hover highlight leaves out an object past that count, and such an object shows its bounds as the key object. With 32,768 type objects, images, symbol instances, gradient meshes or envelopes selected, the selection's outlines and anchors add fewer than 100 vertices to the canvas's mesh (#2460).
|
||||
The canvas keeps the selected objects' outlines, anchors and handles from one frame to the next as one mesh. It builds an object's marks again when the object, its layer color or its selected anchors change. A pan moves the kept outlines and handles and places the anchor squares again. Each subpath and handle gets its triangles the first time it is in view. A frame with a new zoom or rotation draws the marks one shape at a time and keeps nothing, and the next frame at that view builds them. The pixels are the same, apart from two small differences: a kept outline's corner of exactly 90 degrees in a rotated view can keep its miter or cut corner after a pan, and kept vertices can move by rounding errors. On Windows at 1440 × 900, in a release build, with 2,000 selected circles of 50 anchors each in 10 fill colors, an idle frame takes 12.8 ms instead of 59.9 ms with the Selection tool and 29.8 ms instead of 192.1 ms with Direct Selection (medians of five runs). A frame during a zoom takes about what it did: 86.7 ms instead of 80.0 ms with the Selection tool.
|
||||
Delete, Cut, Group, Ungroup, Make and Release Compound Path, Make Clipping Mask, Send to Current Layer, Duplicate, Copy in the Move, Rotate, Scale, Reflect and Shear dialogs, Transform Each with Copy and each move of an Alt-drag take time in proportion to the size of the document (#1429).
|
||||
Cut, and Clear with `ids`, take the removed objects out of the selection in one pass over the selected objects. With 50,000 objects selected and all of them cut, that step takes 5.07 to 5.91 ms instead of 544 to 630 ms, with 20,000 it takes 1.75 to 1.87 ms instead of 91.7 to 97.2 ms, and with 2,000 it takes 0.120 to 0.148 ms instead of 0.940 to 0.955 ms (release build, 7 wall-clock runs of each, measured while other builds were running).
|
||||
In the web build the status bar's render and UI times are measured with the browser's `performance.now()`, and the Layers panel stops rendering row thumbnails in a frame once they have taken 8 ms.
|
||||
On the desktop, the Layers panel renders a row's thumbnail on its thumbnail thread when the row's paths, and the paths of its opacity masks that are not disabled, hold more than 2,000 anchors in all. It already did this for the first thumbnail of a row of more than 200 objects. Until its first thumbnail is ready, the row shows boxes at the bounds of up to 41 of its paths, each in its path's solid fill or stroke color or in gray, when its own paths, without its opacity masks, hold at most 2,000 anchors in all, and an empty white box otherwise. With one path of 200,000 anchors, the first frame after the panel opens takes 2.4 ms instead of 393 ms (release build, median of three runs).
|
||||
The same 2,000-anchor limit counts each glyph of type as 4 anchors, each point of a gradient mesh as 1 and every 100 pixels of an image as 1, and a symbol instance or a placed document counts the objects and anchors of the art it shows. A placed document whose file has not been read yet, or a symbol nested more than 8 deep (one that holds an instance of itself, for example), gets its first thumbnail on the thumbnail thread, and so does a row whose art takes more than 20,000 steps to count (each object, subpath, text run and symbol looked at is one step). The first frame after the panel opens takes 2.4 ms instead of 19.8 ms with 5,000 glyphs of point type, 1.8 ms instead of 343 ms with one instance of a symbol of a 200,000-anchor path, 2.0 ms instead of 11.0 ms with a 100 × 100 mesh, and 2.1 ms instead of 274 ms with a placed document of a 200,000-anchor path (release build, median of three runs).
|
||||
|
||||
Reference in New Issue
Block a user