Files
vectorcraft/crates/doc/src/node/changed.rs
T
dotCooCoo aaa54f1307 Performance: an edit skips unchanged objects without reading them
After every edit, the pixel grid, color mask, Objects on Path, Shaper and
symbol instance passes, and the inline graphics refresh, pair each object
with its counterpart from before the edit. Each pass read both objects to
compare their ids before it found that they were the same node, so moving
one object in a layer of 20,000 objects read all of them in four passes.
The color mask pass is new since v0.24.1, and object.move became slower
than in that release.

The new vectorcraft_doc::changed_nodes does the pairing for all six. An
object that is the same node at the same position is skipped by a pointer
comparison; only changed and new objects are looked up by id.

object.move of one rectangle in a document of 20,000 objects, release
build, median CPU time of 2,000 moves: main 1.25 ms, this change 1.08 ms,
v0.24.1 1.06 ms. The passes' share of the move went from about 30% to 5%
in a profile.
2026-11-29 13:03:07 -08:00

34 lines
1.5 KiB
Rust

use std::collections::HashMap;
use super::{Arc, Node, NodeId};
#[cfg(test)]
thread_local! {
/// Tests: how many nodes [`changed_nodes`] looked up a counterpart for on this thread.
pub(crate) static COUNTERPART_LOOKUPS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// The nodes of `after` that are not the same nodes as their counterparts in `before` (the same
/// list before an edit), with their positions in `after`. A node's counterpart is the node at the
/// same position when it has the same id, else the node in `before` with its id, else `None` (the
/// node is new). A node that is the same node as the one at its position in `before` is skipped
/// without reading it, so a list of many unchanged nodes costs one pointer comparison per node.
pub fn changed_nodes<'a>(before: &'a [Arc<Node>], after: &'a [Arc<Node>]) -> impl Iterator<Item = (usize, &'a Arc<Node>, Option<&'a Arc<Node>>)> {
let mut by_id: Option<HashMap<NodeId, &'a Arc<Node>>> = None;
after.iter().enumerate().filter_map(move |(i, a)| {
if before.get(i).is_some_and(|b| Arc::ptr_eq(b, a)) {
return None;
}
#[cfg(test)]
COUNTERPART_LOOKUPS.with(|c| c.set(c.get() + 1));
let b = match before.get(i) {
Some(b) if b.id == a.id => Some(b),
_ => by_id.get_or_insert_with(|| before.iter().map(|b| (b.id, b)).collect()).get(&a.id).copied(),
};
if b.is_some_and(|b| Arc::ptr_eq(b, a)) {
return None;
}
Some((i, a, b))
})
}