Merge pull request #8379 from dotCooCoo/perf/ai-texts-move-stories

PDF/.ai import: the text document reader moves its stories out instead of copying them
This commit is contained in:
BFlat
2026-11-18 12:56:39 +07:00
committed by GitHub
+129 -7
View File
@@ -65,6 +65,14 @@ impl Val {
}
}
/// Removes the entry `key` of a dictionary and returns it, without copying it.
fn take(&mut self, key: &str) -> Option<Val> {
match self {
Val::Dict(d) => d.remove(key),
_ => None,
}
}
fn num(&self) -> Option<f64> {
match self {
Val::Num(n) if n.is_finite() => Some(*n),
@@ -86,6 +94,14 @@ impl Val {
}
}
/// [`Self::list`] as an owned vector, without copying the items: empty when this isn't a list.
fn into_list(self) -> Vec<Val> {
match self {
Val::List(l) => l,
_ => Vec::new(),
}
}
fn nums(&self) -> Option<Vec<f64>> {
self.list().iter().map(Val::num).collect()
}
@@ -661,20 +677,30 @@ impl Texts {
})
.collect();
let main = doc.get("1")?;
main.get("1")?;
main.get("2")?;
doc.get("0")?.get("8")?.get("0")?;
let sheets = |key: &str| -> Vec<Val> {
let list = doc.get("0").and_then(|d| d.get(key)).and_then(|s| s.get("0")).map(Val::list).unwrap_or_default();
list.iter().take(MAX_SHEETS).map(|s| s.get("0").cloned().unwrap_or(Val::Dict(BTreeMap::new()))).collect()
};
let char_sheets = sheets("5");
let para_sheets = sheets("6");
let kinsoku_sets = kinsoku_sets(&doc);
// Every path is there (looked up above), so the stories, defaults and frames move out of the
// document without a copy.
let mut doc = doc;
let mut main = doc.take("1")?;
Some(Texts {
stories: main.get("1")?.list().to_vec(),
stories: main.take("1")?.into_list(),
fonts,
defaults: main.get("2")?.clone(),
para_defaults: main.get("3").cloned().unwrap_or(Val::Dict(BTreeMap::new())),
char_sheets: sheets("5"),
para_sheets: sheets("6"),
frames: doc.get("0")?.get("8")?.get("0")?.list().to_vec(),
defaults: main.take("2")?,
para_defaults: main.take("3").unwrap_or(Val::Dict(BTreeMap::new())),
char_sheets,
para_sheets,
frames: doc.take("0")?.take("8")?.take("0")?.into_list(),
patterns: HashMap::new(),
kinsoku_sets: kinsoku_sets(&doc),
kinsoku_sets,
})
}
@@ -3086,4 +3112,100 @@ mod tests {
let (placed, at, _) = first(&s, true);
assert!(placed && at.is_some_and(|x| (x - (own + 3.0)).abs() < 1e-6), "{at:?} for {}", own + 3.0);
}
/// [`Val::take`] and [`Val::into_list`] hand over the value they find without copying it.
#[test]
fn taking_a_value_keeps_its_storage() {
let mut v = read(b"/1 [ (a story) << /0 (its frame) >> ] /2 (defaults)").unwrap();
let (Some(Val::List(l)), Some(Val::Str(s))) = (v.get("1"), v.get("2")) else { panic!("a list and a string") };
let (list, string) = (l.as_ptr(), s.as_ptr());
let taken = v.take("1").unwrap().into_list();
assert_eq!(taken.as_ptr(), list);
assert_eq!(taken.len(), 2);
let Some(Val::Str(s)) = v.take("2") else { panic!("a string") };
assert_eq!(s.as_ptr(), string);
assert_eq!(v, Val::Dict(BTreeMap::new()));
assert_eq!(v.take("1"), None);
assert_eq!(Val::Num(1.0).take("1"), None);
assert_eq!(Val::Str("x".into()).into_list(), Vec::new());
}
/// [`Texts::read`] gives the same stories, defaults, frames, styles, Kinsoku sets and fonts as
/// copying each of them out of the parsed document does, and reads nothing where that finds
/// nothing.
#[test]
fn the_text_document_reads_the_same_as_copying_its_parts() {
type Parts = (Vec<Val>, Val, Val, Vec<Val>, Vec<Val>, Vec<Val>, Vec<Option<Kinsoku>>, Vec<(String, Option<String>)>);
let copied = |document: &str| -> Option<Parts> {
let doc = names::numbered(read(document.as_bytes())?);
let fonts = doc.get("0")?.get("1")?.get("0")?.list();
let fonts = fonts
.iter()
.map(|f| {
let record = f.get("0").and_then(|f| f.get("0"));
let text = |k: &str| record.and_then(|r| r.get(k)).and_then(Val::str);
(text("0").unwrap_or_default().to_string(), text("5").and_then(record_version).map(str::to_string))
})
.collect();
let main = doc.get("1")?;
let sheets = |key: &str| -> Vec<Val> {
let list = doc.get("0").and_then(|d| d.get(key)).and_then(|s| s.get("0")).map(Val::list).unwrap_or_default();
list.iter().take(MAX_SHEETS).map(|s| s.get("0").cloned().unwrap_or(Val::Dict(BTreeMap::new()))).collect()
};
Some((
main.get("1")?.list().to_vec(),
main.get("2")?.clone(),
main.get("3").cloned().unwrap_or(Val::Dict(BTreeMap::new())),
doc.get("0")?.get("8")?.get("0")?.list().to_vec(),
sheets("5"),
sheets("6"),
kinsoku_sets(&doc),
fonts,
))
};
let read_parts = |document: &str| -> Option<Parts> {
// Padded so that the data's end, `~>`, stays on one line.
let mut document = document.to_string();
while crate::import::tests_native::native_with_text("", &document).contains("\n%>\n") {
document.push('\n');
}
let t = Texts::read(crate::import::tests_native::native_with_text("", &document).as_bytes())?;
let fonts = t.fonts.iter().map(|f| (f.name.clone(), f.version.clone())).collect();
Some((t.stories, t.defaults, t.para_defaults, t.frames, t.char_sheets, t.para_sheets, t.kinsoku_sets, fonts))
};
let plain = crate::import::tests_native::text_document(&[
("First story", 0, (10.0, 20.0), (1.0, 2.0), 0.0),
("Second, longer story", 2, (30.0, 40.0), (3.0, 4.0), 5.0),
("Third", 1, (50.0, 60.0), (5.0, 6.0), 0.0),
]);
let full = plain
.replacen(
"/8 << /0 [",
"/4 << /0 [ << /0 << /0 (PhotoshopKinsokuHard) >> >> << /0 << /0 (Own) >> >> ] >> \
/5 << /0 [ << /0 << /0 (Normal) /6 << /1 9.0 >> >> >> << /1 0 >> ] >> \
/6 << /0 [ << /0 << /0 (Body) /5 << /0 2 >> >> >> ] >> /8 << /0 [",
1,
)
.replacen("/2 << /1 12.0 >>", "/2 << /1 12.0 /0 1 >> /3 << /0 2 /33 1 >>", 1);
let cases = [
("every part", full.clone(), true),
("no paragraph defaults", plain.clone(), true),
("stories that aren't a list", full.replacen("\n/1 << /1 [", "\n/1 << /1 5 /7 [", 1), true),
("frames that aren't a list", full.replacen("/8 << /0 [", "/8 << /0 5 /7 [", 1), true),
("no defaults", full.replacen("/2 << /1 12.0 /0 1 >>", "", 1), false),
("no frames", full.replacen("/8 << /0 [", "/9 << /0 [", 1), false),
("no stories", full.replacen("\n/1 << /1 [", "\n/1 << /7 [", 1), false),
("no fonts", full.replacen("/0 << /1 << /0 [", "/0 << /1 << /7 [", 1), false),
];
for (case, document, reads) in cases {
assert!(case == "every part" || document != full, "{case} changes the document");
let want = copied(&document);
assert_eq!(want.is_some(), reads, "{case}");
assert_eq!(read_parts(&document), want, "{case}");
}
let Some((stories, defaults, para_defaults, frames, char_sheets, para_sheets, kinsoku, fonts)) = read_parts(&full) else { panic!("read") };
assert_eq!((stories.len(), frames.len(), char_sheets.len(), para_sheets.len(), fonts.len()), (3, 3, 2, 1, 1));
assert_eq!(kinsoku, [Some(Kinsoku::Hard), None]);
assert!(defaults.get("0").is_some() && para_defaults.get("33").is_some());
}
}