Performance: the check of a moved selection reads the objects at their places

After a move, the check that a view can move the selection's marks
instead of building them again looked each moved object up by id three
times: the object, its parent's id and the parent. With 10,000 objects
selected that was 30,000 lookups after the edit. The check now keeps
each object's place from the transform and reads the object and its
parent there, so the move looks each object up once, as before.
This commit is contained in:
dotCooCoo committed 2026-11-30 03:33:26 -08:00
1 parent 18271017b7
commit 04ced9bee7
1 file changed
+14 -12
+14 -12
View File
@@ -200,7 +200,7 @@ fn apply_transform_moving_guides(
// A move of whole objects, not of copies (which `reindex` covers): the document before it,
// and the moved objects as the transform left them, for [`moved_by`] to check after the edit.
let from = (objects && !areas && !reindex && c[..4] == [1.0, 0.0, 0.0, 1.0]).then(|| s.doc().map(|st| st.doc.clone())).transpose()?;
let mut held: Vec<(NodeId, Arc<Node>)> = vec![];
let mut held: Vec<(Vec<usize>, Arc<Node>)> = vec![];
let targets = s.edit(label, |d, sel| {
let mut targets = match &kept {
// The document becomes the snapshot with the kept copies, untransformed, and the
@@ -240,7 +240,7 @@ fn apply_transform_moving_guides(
}
// Holding them makes any later change to them copy them first, which `moved_by` sees.
if from.is_some() {
held = targets.iter().filter_map(|id| Some((*id, node_arc(d, paths.get(id)?)?))).collect();
held = targets.iter().filter_map(|id| paths.get(id)).filter_map(|p| Some((p.clone(), node_arc(d, p)?))).collect();
}
if objects {
// The transforms above leave every object at its place, so `paths` still holds them.
@@ -294,20 +294,22 @@ fn node_arc(d: &Document, path: &[usize]) -> Option<Arc<Node>> {
}
/// The [`crate::Moved`] of a move by `by` that took `from` to the document of `st`, given the moved
/// objects as the transform left them (`held`). `None` unless every moved object is still that
/// node, none is a member of a blend (whose spine follows its members), and every object the
/// selection shows, the key object and the key anchor's object included, is a moved object or
/// inside one. A pass after the transform that changes a moved object, such as the one that snaps
/// moved pixel-aligned objects to the pixel grid again ([`super::pixelgrid::refresh`]), copies it
/// first, so it is no longer that node.
fn moved_by(from: &Arc<Document>, st: &crate::DocState, held: &[(NodeId, Arc<Node>)], by: Vec2) -> Option<crate::Moved> {
/// objects as the transform left them (`held`, each with its index path). `None` unless every moved
/// object is still that node at its path, none is a member of a blend (whose spine follows its
/// members), and every object the selection shows, the key object and the key anchor's object
/// included, is a moved object or inside one. A pass after the transform that changes a moved
/// object, such as the one that snaps moved pixel-aligned objects to the pixel grid again
/// ([`super::pixelgrid::refresh`]), copies it first, so it is no longer that node. The checks read
/// the nodes at their paths and look nothing up by id.
fn moved_by(from: &Arc<Document>, st: &crate::DocState, held: &[(Vec<usize>, Arc<Node>)], by: Vec2) -> Option<crate::Moved> {
let d = &st.doc;
let unchanged = |(id, n): &(NodeId, Arc<Node>)| d.node(*id).is_some_and(|now| std::ptr::eq(now, Arc::as_ptr(n)));
let unchanged = |(p, n): &(Vec<usize>, Arc<Node>)| d.node_at(p).is_some_and(|now| std::ptr::eq(now, Arc::as_ptr(n)));
if held.is_empty() || !held.iter().all(unchanged) {
return None;
}
let in_blend = |id: NodeId| d.parent_of(id).and_then(|p| d.node(p)).is_some_and(|p| matches!(p.kind, NodeKind::Blend { .. }));
if held.iter().any(|(id, _)| in_blend(*id)) {
// The node at each path is the moved object, so its parent is the node one level up.
let in_blend = |p: &[usize]| p.split_last().and_then(|(_, up)| d.node_at(up)).is_some_and(|parent| matches!(parent.kind, NodeKind::Blend { .. }));
if held.iter().any(|(p, _)| in_blend(p)) {
return None;
}
let mut moved = std::collections::HashSet::new();