Merge pull request #4001 from dotCooCoo/fix/affinity-run-match-count

.af reader: the paragraph matching test counts lookups instead of timing
This commit is contained in:
BFlat authored and GitHub committed 2026-11-29 03:01:18 +07:00
commit be1061eea1
3 files changed
+62 -21

No files matched your search

+8
View File
@@ -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<T>(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))
}
+28 -2
View File
@@ -240,12 +240,26 @@ pub(crate) fn story_runs(r: &mut Reader, story: ObjId, world: Affine) -> (Vec<Te
// Sorted by end (they are in order in every file), so both lookups below are binary
// searches: a block can have as many paragraphs and runs as characters.
paragraphs.sort_by_key(|(end, _)| *end);
let align_at = |i: usize| paragraphs.get(paragraphs.partition_point(|(end, _)| *end <= i)).map(|(_, a)| *a).or(open).unwrap_or_default();
let align_at = |i: usize| {
let at = paragraphs.partition_point(|(end, _)| {
count_match_read();
*end <= i
});
paragraphs.get(at).map(|(_, a)| *a).or(open).unwrap_or_default()
};
let mut cuts: Vec<usize> = paragraphs.iter().map(|(end, _)| *end).filter(|e| *e < chars.len()).collect();
cuts.dedup();
let push = |r: &mut Reader, runs: &mut Vec<TextRun>, attrs: Option<ObjId>, from: usize, to: usize, budget: &mut Option<usize>| {
// 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<Te
(runs, first_align.unwrap_or(Align::Left))
}
// Each paragraph end `story_runs` reads to match character runs to paragraphs, on this thread.
#[cfg(any(test, feature = "synth"))]
std::thread_local! {
pub(crate) static MATCH_READS: std::cell::Cell<usize> = 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()
}
+26 -19
View File
@@ -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.