From 9b0ef2a95e2554131cb3bf7692afa18a9fb8795f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 17:59:09 -0800 Subject: [PATCH] i18n: the turned-down occurrence test counts search steps instead of timing occurrences_turned_down_at_a_mark_are_not_searched_again passed only when the search finished within 5 s. A test-only counter now records each haystack byte Folded::matches reads and each failure link it follows, and the test checks that the search takes at most twice as many steps as the folded haystack has bytes. With the search started again after each turned-down occurrence, it takes 7,500,300,001 steps for the 300,001-byte haystack and the test fails. --- crates/i18n/src/fold.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/i18n/src/fold.rs b/crates/i18n/src/fold.rs index a25397ce36..9cb01c4b71 100644 --- a/crates/i18n/src/fold.rs +++ b/crates/i18n/src/fold.rs @@ -226,6 +226,19 @@ fn prefix_links(n: &[u8]) -> Vec { links } +// Each step of `Folded::matches` through the haystack on this thread: one per byte read and one +// per failure link followed. +#[cfg(test)] +std::thread_local! { + static SEARCH_STEPS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +#[inline] +fn count_search_step() { + #[cfg(test)] + SEARCH_STEPS.with(|steps| steps.set(steps.get().saturating_add(1))); +} + /// Folded text and where each part came from. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Folded { @@ -296,7 +309,9 @@ impl Folded { return Some(0..0); } while let Some(&b) = hay.get(at) { + count_search_step(); while k > 0 && n[k] != b { + count_search_step(); k = links[k - 1]; } if n[k] == b { @@ -330,10 +345,15 @@ mod tests { let haystack = format!("{}a", "a\u{301}".repeat(100_000)); let needle = format!("{}a", "a\u{301}".repeat(50_000)); let folded = f.fold(&haystack); - let start = std::time::Instant::now(); - let found = folded.find(&f.fold(&needle)); + let folded_needle = f.fold(&needle); + SEARCH_STEPS.with(|steps| steps.set(0)); + let found = folded.find(&folded_needle); assert_eq!(found, Some(haystack.len() - needle.len()..haystack.len())); - assert!(start.elapsed() < std::time::Duration::from_secs(5), "{:?}", start.elapsed()); + // One pass reads each haystack byte once and follows at most as many links as it read + // bytes; searching again from each turned-down occurrence rereads the needle each time. + let steps = SEARCH_STEPS.with(std::cell::Cell::get); + let bound = 2 * folded.as_str().len(); + assert!(steps <= bound, "{steps} search steps for a {}-byte haystack", folded.as_str().len()); } #[test] fn source_ranges_round_trip() {