perf: index morph name candidates by content class (#2467)

The strict name and geometry passes now key on text, kind and picture media, so candidates that differ in those fields never share a queue. A 20,000 same-name distinct-text slide looks up one key per shape instead of scanning 20,000 candidates. Remove a front match with pop_front and use a positional removal only for a later match.

Signed-off-by: MaheshDesu <[email protected]>
This commit is contained in:
MaheshDesu
2026-12-04 16:33:15 +05:30
parent b6c8adbaff
commit 29f90a373f
2 changed files with 58 additions and 9 deletions
+26 -9
View File
@@ -12,6 +12,13 @@ fn text_of(s: &Shape) -> String {
s.text.as_ref().map(|t| t.text()).unwrap_or_default()
}
fn media_of(s: &Shape) -> Option<u32> {
match &s.kind {
ShapeKind::Picture { fill } => Some(fill.media.0),
_ => None,
}
}
/// Pairs of (shape on `a`, shape on `b`) that Morph animates from one to the other: first by
/// `!!name` (forced match), then by exact name when the two are the same object, then by the same
/// kind and text, then by the same kind and preset geometry if neither carries text and they are
@@ -22,8 +29,9 @@ fn text_of(s: &Shape) -> String {
/// once; top-level shapes only (groups move whole).
///
/// Each pass compares a key of the two shapes, so the shapes of `b` are indexed by key once per
/// pass: matching costs the sum of the two shape lists, plus a scan of the shapes sharing a key
/// on a strict pass, never the product of the two lists.
/// pass. A strict pass's key includes the text, kind and picture media, so only shapes that can
/// still match through a geometry or crop change share a queue; matching is linear in the shape
/// lists plus the candidates that share such a key.
pub fn morph_pairs(a: &Slide, b: &Slide) -> Vec<(ShapeId, ShapeId)> {
let mut pairs: Vec<(ShapeId, ShapeId)> = Vec::new();
let mut used_a = vec![false; a.shapes.len()];
@@ -32,10 +40,12 @@ pub fn morph_pairs(a: &Slide, b: &Slide) -> Vec<(ShapeId, ShapeId)> {
let text_a: Vec<String> = a.shapes.iter().map(text_of).collect();
let text_b: Vec<String> = b.shapes.iter().map(text_of).collect();
let forced = |s: &Shape, _: &str| (s.name.starts_with("!!")).then(|| MorphKey::Name(s.name.clone()));
let named = |s: &Shape, _: &str| (!s.name.is_empty()).then(|| MorphKey::Name(s.name.clone()));
let named = |s: &Shape, t: &str| {
(!s.name.is_empty()).then(|| MorphKey::Named { name: s.name.clone(), text: t.to_string(), kind: s.kind_name(), media: media_of(s) })
};
let same_text = |s: &Shape, t: &str| (!t.trim().is_empty()).then(|| MorphKey::Text(s.kind_name(), t.to_string()));
let same_geom = |s: &Shape, t: &str| match s.geom.preset_name() {
Some(g) if t.trim().is_empty() => Some(MorphKey::Geom(s.kind_name(), g.to_string())),
Some(g) if t.trim().is_empty() => Some(MorphKey::Geom(s.kind_name(), g.to_string(), media_of(s))),
_ => None,
};
// Forced names pair anything; ordinary names and the geometry fallback pair only the same
@@ -61,13 +71,19 @@ pub fn morph_pairs(a: &Slide, b: &Slide) -> Vec<(ShapeId, ShapeId)> {
continue;
}
let Some(k) = key(sa, text_a.get(i).map_or("", String::as_str)) else { continue };
// A strict pass takes the first candidate that is the same object, looking past those
// that are not; a rejected candidate stays in the queue for another shape of `a` and
// for a later pass, and only the one paired is removed.
// A strict pass takes the first candidate that is the same object. Its key already
// separates text, kind and picture differences that `morph_match` cannot bridge, so
// the scan only looks at candidates that may differ in geometry or crop.
let Some(q) = index.get_mut(&k) else { continue };
let pick = if strict { q.iter().copied().find(|&j| b.shapes.get(j).is_some_and(|sb| morph_match(sa, sb))) } else { q.front().copied() };
let Some(j) = pick else { continue };
q.retain(|&x| x != j);
// A match at the front is the common case; only a later match needs a positional
// removal, so a long queue of compatible shapes stays linear.
if q.front() == Some(&j) {
q.pop_front();
} else {
q.retain(|&x| x != j);
}
let Some(sb) = b.shapes.get(j) else { continue };
if let Some(u) = used_a.get_mut(i) {
*u = true;
@@ -85,8 +101,9 @@ pub fn morph_pairs(a: &Slide, b: &Slide) -> Vec<(ShapeId, ShapeId)> {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum MorphKey {
Name(String),
Named { name: String, text: String, kind: &'static str, media: Option<u32> },
Text(&'static str, String),
Geom(&'static str, String),
Geom(&'static str, String, Option<u32>),
}
fn lerp(a: f64, b: f64, t: f64) -> f64 {
+32
View File
@@ -627,6 +627,26 @@ fn same_name_skips_an_incompatible_candidate() {
assert_eq!(f.paths[0].from, deckcraft_model::Geom::preset("rect"));
}
#[test]
fn same_name_pairs_a_picture_after_an_incompatible_same_media_candidate() {
let mut a = Slide::default();
let mut b = Slide::default();
let pic = |id, geom: &str, crop, x| Shape {
xfrm: Some(Xfrm::new(x, 100.0, 200.0, 100.0)),
geom: deckcraft_model::Geom::preset(geom),
kind: deckcraft_model::ShapeKind::Picture {
fill: deckcraft_model::style::PictureFill { media: deckcraft_model::MediaId(10), crop, ..Default::default() },
},
..shape(id, "Picture 1")
};
a.shapes = vec![pic(1, "rect", [0.0; 4], 0.0)];
b.shapes = vec![
pic(2, "ellipse", [0.5, 0.0, 0.0, 0.0], 200.0), // same name and media, but geometry and crop both changed
pic(3, "rect", [0.0; 4], 400.0), // the same object
];
assert_eq!(morph_pairs(&a, &b), [(ShapeId(1), ShapeId(3))], "the later compatible picture pairs");
}
#[test]
fn same_name_pictures_with_different_images_fade_in_place() {
// #2467: the same name is not enough when the picture changed; the geometry fallback must not
@@ -1317,6 +1337,18 @@ fn morph_of_large_slides_stays_linear() {
assert_eq!(f.shapes.len(), 2 * n as usize - 1);
}
#[test]
fn many_same_name_shapes_with_distinct_text_stay_linear() {
let n = 20_000u32;
let mut a = Slide::default();
let mut b = Slide::default();
// Every shape shares the name but no text: the strict name pass must look up one text key
// per shape, not scan all 20,000 same-name candidates.
a.shapes = (0..n).map(|i| Shape { text: Some(TextBody::from_text(&format!("a {i}"))), ..shape(i + 1, "Box") }).collect();
b.shapes = (0..n).map(|i| Shape { text: Some(TextBody::from_text(&format!("b {i}"))), ..shape(n + i + 1, "Box") }).collect();
assert!(morph_pairs(&a, &b).is_empty());
}
#[test]
fn paragraph_states_of_a_large_build_stay_linear() {
let n = 20_000usize;