From 6bba4ba27e0e0fa33fef25130cd62b70aa74710e Mon Sep 17 00:00:00 2001 From: Jacinta Ferrant Date: Thu, 18 Jun 2026 21:17:35 -0700 Subject: [PATCH] fix(rust): measure worst-case pre-filter overhead in the benchmark Signed-off-by: Jacinta Ferrant --- .../bench_pattern_prefilter.py | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/tests/rust_core_unit_tests/bench_pattern_prefilter.py b/tests/rust_core_unit_tests/bench_pattern_prefilter.py index 36e126e3b7e..2384731b1ca 100644 --- a/tests/rust_core_unit_tests/bench_pattern_prefilter.py +++ b/tests/rust_core_unit_tests/bench_pattern_prefilter.py @@ -89,6 +89,14 @@ def _rust_prefilter(text: str) -> bool: return PREFILTER.any_match(text) +def _with_prefilter_check(text: str) -> bool: + if PREFILTER.any_match(text): + return _python_loop(text) + return False + + +MATCHING_TEXT = "My SSN is 123-45-6789, can you confirm it looks valid?" + CLEAN_TEXTS = { "short (~10 words)": "Can you help me write a short poem about the ocean at sunset?", "medium (~80 words)": ( @@ -115,14 +123,14 @@ CLEAN_TEXTS = { ), } -WARMUP = 200 +ITERATIONS_PER_BATCH = 200 REPEATS = 5 def _measure_us(fn, arg) -> float: - fn(arg) - times = timeit.repeat(lambda: fn(arg), number=WARMUP, repeat=REPEATS) - return min(times) / WARMUP * 1_000_000 + fn(arg) # untimed warm-up call, separate from the timed batches below + times = timeit.repeat(lambda: fn(arg), number=ITERATIONS_PER_BATCH, repeat=REPEATS) + return min(times) / ITERATIONS_PER_BATCH * 1_000_000 def main() -> None: @@ -163,6 +171,19 @@ def main() -> None: print("python: each pattern runs its own re.search call over the same text.") print() + print("Worst case: text that actually matches, where the pre-filter can't") + print("skip anything and the full pattern loop runs either way. This is the") + print("added cost of the pre-filter check itself, not a speedup.") + print() + assert _python_loop(MATCHING_TEXT) is True + assert _with_prefilter_check(MATCHING_TEXT) is True + old_us = _measure_us(_python_loop, MATCHING_TEXT) + new_us = _measure_us(_with_prefilter_check, MATCHING_TEXT) + print(f"without pre-filter check: {old_us:.2f}us") + print(f"with pre-filter check: {new_us:.2f}us") + print(f"added overhead: {new_us - old_us:+.2f}us") + print() + if __name__ == "__main__": main()