From 61ba2a3d680110a4fb374a9f8d62fffbc115f3a7 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 18:15:05 -0800 Subject: [PATCH] craft-tiff: the missing byte count test counts offset reads instead of timing many_strips_without_byte_counts_are_fast passed only when chunks() measured a million strips without byte counts within 20 s. A test-only counter now records the offsets chunks() sorts and each offset it reads to find the next larger one. The test allows one read per strip for the sort and log2(n) + 2 reads per strip for the bisection, and the counter fails the test as soon as the reads pass that limit. With the next offset found by a scan of the sorted offsets, the test fails at 22,000,001 reads. --- crates/tiff/src/image.rs | 45 +++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/crates/tiff/src/image.rs b/crates/tiff/src/image.rs index d547279f84..ce3bc929bb 100644 --- a/crates/tiff/src/image.rs +++ b/crates/tiff/src/image.rs @@ -155,6 +155,7 @@ impl ImageInfo { let mut sorted: Vec = Vec::new(); if (0..n).any(|i| self.byte_counts.get(i).is_none_or(|&l| l == 0)) { sorted = self.offsets.clone(); + count_offset_reads(sorted.len()); sorted.sort_unstable(); sorted.dedup(); } @@ -173,7 +174,13 @@ impl ImageInfo { Some(&l) if l > 0 => l, _ => { // infer: up to the next larger offset, else end of data - let next = sorted.get(sorted.partition_point(|&o| o <= offset)).copied().unwrap_or(data_len); + let next = sorted + .get(sorted.partition_point(|&o| { + count_offset_reads(1); + o <= offset + })) + .copied() + .unwrap_or(data_len); next.saturating_sub(offset) } }; @@ -183,6 +190,30 @@ impl ImageInfo { } } +// The offsets `ImageInfo::chunks` sorts and then reads to infer missing byte counts, on this +// thread, and the most a test lets it read: past that the count fails the test at once rather +// than after the rest of the work. +#[cfg(test)] +std::thread_local! { + static OFFSET_READS: std::cell::Cell = const { std::cell::Cell::new(0) }; + static OFFSET_READ_LIMIT: std::cell::Cell = const { std::cell::Cell::new(usize::MAX) }; +} + +#[inline] +fn count_offset_reads(count: usize) { + #[cfg(test)] + { + let reads = OFFSET_READS.with(|reads| { + reads.set(reads.get().saturating_add(count)); + reads.get() + }); + let limit = OFFSET_READ_LIMIT.with(std::cell::Cell::get); + assert!(reads <= limit, "{reads} offsets read, more than the {limit} allowed"); + } + #[cfg(not(test))] + let _ = count; +} + /// The bytes of a chunk, clipped to the available data (truncated files yield a shorter slice). /// Returns `None` only when the chunk starts past the end of `data`. pub fn chunk_bytes<'a>(data: &'a [u8], c: &Chunk) -> Option<&'a [u8]> { @@ -229,12 +260,12 @@ mod tests { fn many_strips_without_byte_counts_are_fast() { let n = 1_000_000u64; let info = strips((0..n).rev().map(|i| 1000 + i * 2).collect(), Vec::new()); - let (tx, rx) = std::sync::mpsc::channel(); - std::thread::spawn(move || { - let chunks = info.chunks(1000 + n * 2); - let _ = tx.send(chunks.iter().map(|c| c.len).collect::>()); - }); - let lens = rx.recv_timeout(std::time::Duration::from_secs(20)).expect("chunks() took longer than 20 s"); + // One sort of the offsets, then a bisection of them per strip, not a scan or a sort per strip. + let per_strip = n.ilog2() as usize + 2; + OFFSET_READS.with(|reads| reads.set(0)); + OFFSET_READ_LIMIT.with(|limit| limit.set(n as usize * (per_strip + 1))); + let lens: Vec = info.chunks(1000 + n * 2).iter().map(|c| c.len).collect(); + OFFSET_READ_LIMIT.with(|limit| limit.set(usize::MAX)); assert!(lens.iter().all(|&l| l == 2)); } }