From a2eab0d9f3f851fde6b1d764181f0ee05b1a8d03 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 19:14:34 -0800 Subject: [PATCH] .af reader: the paragraph matching test counts lookups instead of timing many_paragraphs_and_runs_are_matched_in_about_linear_time passed only when a story of 200,000 paragraphs and runs opened within 10 s. A counter, built with the synth feature that the test already needs, now records each paragraph end story_runs looks up to give a run its paragraph's format, and synth::count_paragraph_lookups returns it for one call. The test opens the story at 50,000 and at 200,000 paragraphs and checks that the larger one takes at most five times the lookups. With the paragraph found by a scan instead of a binary search, the lookups grow from 1,251,725,000 to 20,007,700,000 and the test fails. --- crates/affinity/src/synth.rs | 8 ++++ crates/affinity/src/text.rs | 30 ++++++++++++++- crates/affinity/tests/it/import_limits.rs | 45 +++++++++++++---------- 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/crates/affinity/src/synth.rs b/crates/affinity/src/synth.rs index 5b75ca0bff..a25cba792b 100644 --- a/crates/affinity/src/synth.rs +++ b/crates/affinity/src/synth.rs @@ -257,3 +257,11 @@ pub fn container(entries: &[(&str, &[u8], Method)], thumbnail_png: Option<&[u8]> } out } + +/// What `f` gives, and how many paragraph ends the reader looked up on this thread while it ran +/// to give each character run its paragraph's format. +pub fn count_paragraph_lookups(f: impl FnOnce() -> T) -> (T, usize) { + crate::text::MATCH_READS.with(|reads| reads.set(0)); + let value = f(); + (value, crate::text::MATCH_READS.with(std::cell::Cell::get)) +} diff --git a/crates/affinity/src/text.rs b/crates/affinity/src/text.rs index c74dc287f6..f645d66857 100644 --- a/crates/affinity/src/text.rs +++ b/crates/affinity/src/text.rs @@ -240,12 +240,26 @@ pub(crate) fn story_runs(r: &mut Reader, story: ObjId, world: Affine) -> (Vec = paragraphs.iter().map(|(end, _)| *end).filter(|e| *e < chars.len()).collect(); cuts.dedup(); let push = |r: &mut Reader, runs: &mut Vec, attrs: Option, from: usize, to: usize, budget: &mut Option| { // Split at paragraph ends so every run has one paragraph format. - let inside = cuts.get(cuts.partition_point(|c| *c <= from)..cuts.partition_point(|c| *c < to)).unwrap_or_default(); + let first = cuts.partition_point(|c| { + count_match_read(); + *c <= from + }); + let past = cuts.partition_point(|c| { + count_match_read(); + *c < to + }); + let inside = cuts.get(first..past).unwrap_or_default(); let mut a = from; for &c in inside.iter().chain(std::iter::once(&to)) { push_run(r, runs, attrs, characters(&chars, a, c), world, budget, align_at(a)); @@ -282,6 +296,18 @@ pub(crate) fn story_runs(r: &mut Reader, story: ObjId, world: Affine) -> (Vec = const { std::cell::Cell::new(0) }; +} + +#[inline] +fn count_match_read() { + #[cfg(any(test, feature = "synth"))] + MATCH_READS.with(|reads| reads.set(reads.get().saturating_add(1))); +} + fn characters(chars: &[char], start: usize, end: usize) -> String { chars.get(start..end).unwrap_or_default().iter().filter(|c| **c != '\0' && **c != '\u{FFFC}').collect() } diff --git a/crates/affinity/tests/it/import_limits.rs b/crates/affinity/tests/it/import_limits.rs index f08c044298..9924125d9d 100644 --- a/crates/affinity/tests/it/import_limits.rs +++ b/crates/affinity/tests/it/import_limits.rs @@ -111,25 +111,32 @@ fn a_shared_story_is_read_within_the_characters_of_one_document() { #[test] fn many_paragraphs_and_runs_are_matched_in_about_linear_time() { - // 200 000 paragraphs, each with its own run and format. - let n = 200_000; - let text = "a\u{2029}".repeat(n); - let fmt = - |end: usize| vec![(tag(b"Indx"), F::I32(end as i32)), (tag(b"Item"), F::Obj(tag(b"PAtt"), vec![(tag(b"Ints"), array_i32(b"Ints", &[1]))]))]; - let blk = F::Obj( - tag(b"StBl"), - vec![ - (tag(b"Glyp"), utf8(&text)), - (tag(b"GAtt"), F::Obj(tag(b"GlAS"), vec![(tag(b"Runs"), F::Objs(tag(b"GlAR"), (1..=n).map(|i| run(2 * i as i32, None)).collect()))])), - (tag(b"PAtt"), F::Obj(tag(b"PaAS"), vec![(tag(b"Runs"), F::Objs(tag(b"PaAR"), (1..=n).map(|i| fmt(2 * i)).collect()))])), - ], - ); - let started = std::time::Instant::now(); - let d = open(vec![artistic(F::Obj(tag(b"Stry"), vec![(tag(b"Blok"), F::Shared(vec![blk]))]))]); - let Kind::Text(t) = &d.spreads[0].nodes[0].kind else { panic!("text") }; - assert_eq!(t.runs.len(), n); - // (About half a second; quadratic matching took over half a minute on the same machine.) - assert!(started.elapsed().as_secs() < 10, "{:?}", started.elapsed()); + // `n` paragraphs, each with its own run and format, and how many paragraph ends matching + // them looks up. + let lookups = |n: usize| { + let text = "a\u{2029}".repeat(n); + let fmt = |end: usize| { + vec![(tag(b"Indx"), F::I32(end as i32)), (tag(b"Item"), F::Obj(tag(b"PAtt"), vec![(tag(b"Ints"), array_i32(b"Ints", &[1]))]))] + }; + let blk = F::Obj( + tag(b"StBl"), + vec![ + (tag(b"Glyp"), utf8(&text)), + (tag(b"GAtt"), F::Obj(tag(b"GlAS"), vec![(tag(b"Runs"), F::Objs(tag(b"GlAR"), (1..=n).map(|i| run(2 * i as i32, None)).collect()))])), + (tag(b"PAtt"), F::Obj(tag(b"PaAS"), vec![(tag(b"Runs"), F::Objs(tag(b"PaAR"), (1..=n).map(|i| fmt(2 * i)).collect()))])), + ], + ); + let story = artistic(F::Obj(tag(b"Stry"), vec![(tag(b"Blok"), F::Shared(vec![blk]))])); + let (d, lookups) = synth::count_paragraph_lookups(|| open(vec![story])); + let Kind::Text(t) = &d.spreads[0].nodes[0].kind else { panic!("text") }; + assert_eq!(t.runs.len(), n); + lookups + }; + // Four times the paragraphs and runs: a few binary searches each, so about four times the + // lookups (a little more for the longer searches), where matching each run against every + // paragraph is sixteen times. + let (quarter, full) = (lookups(50_000), lookups(200_000)); + assert!(full <= 5 * quarter, "{quarter} lookups for 50,000 paragraphs, {full} for 200,000"); } /// A shape node of `size` px.