Skip to content

Regex layer: lock-free pattern cache, single matching, no per-call allocation - #4

Merged
MasonLyons merged 1 commit into
mainfrom
regex-layer
Dec 4, 2026
Merged

MasonLyons merged 1 commit into
mainfrom
regex-layer

Conversation

@MasonLyons

Copy link
Copy Markdown
Owner

Summary

The regex layer's per-call overhead was the main remaining cost in parse, validate, format and the matcher. This PR removes most of it, without changing any matching semantics.

  • Pattern cache: each thread caches compiled regexes in front of a shared compile-once map (RwLock). Once warm, a lookup takes no lock, allocates nothing, and hashes with FxHash. Before, every call took one global mutex and allocated a String key, so Python threads running GIL-released batch calls all queued on that mutex.
  • fullmatch: the ^(?:…)$ form is compiled once instead of being built with format! on every call.
  • Single matching: match_number_against_pattern runs one regex call instead of two (anchored match, then full match). The new engine::is_match_here replaces match_here(..).is_some() and skips capture groups.
  • Constant patterns: the 14 zero-argument pattern builders (matcher candidate pattern, extension patterns, and others) are now LazyLock statics.
  • Matcher: the text is stored as UTF-8 once instead of being rebuilt on every find(). For ASCII text, byte and char offsets map directly instead of rescanning from the start.
  • Benchmarks: a new 8-thread validation benchmark, and tools/bench_compare.py now honours CRITERION_HOME.

Benchmarks

Two alternating before/after rounds, run against a main worktree with the same bench file:

benchmark before after change
parse international ×12 107 µs 30–31 µs 3.5–3.6×
parse national ×12 119–121 µs 39 µs 3.0–3.1×
is_valid_number ×23 75 µs 30 µs 2.5×
format_number ×23 52–53 µs 34–36 µs 1.45–1.53×
matcher, 10 KB text 1.14–1.20 ms 426–447 µs 2.7×
as-you-type, 10 digits 16–17 µs 11.7–11.9 µs 1.4×
is_valid_number, 8 threads 148–149 ms 9.5–10.5 ms 14–16×

normalize_digits_only is untouched; its results moved by layout and noise only.

Python package vs upstream phonenumbers (benchmarks/run_bench.py): single call 3.2× → 7.5×, matcher 2.6× → 7.6×, as-you-type 6.0× → 8.1×, 100k batch 3.0× → 7.8×.

Verification

  • cargo test --workspace --exclude pn-py, including the vector tests and LUT soundness. New engine unit tests cover:
    • invalid patterns cached as misses
    • is_match_here agreeing with match_here
    • plain and full forms of the same pattern cached separately
    • concurrent first use from 8 threads
  • pytest, with the differential fuzz against python-phonenumbers run at PHONES_FUZZ_EXAMPLES=5000: 58 passed.
  • cargo clippy -D warnings, cargo fmt, and an MSRV 1.88 check of phonenumbers-rs.

🤖 Generated with Claude Code

…location

- engine: per-thread FxHash cache in front of a shared compile-once map
  (RwLock); fullmatch forms compiled once as ^(?:p)$; new is_match_here
  (anchored, no captures).
- matching: one regex call per number-pattern check instead of two.
- Constant pattern builders memoised in LazyLock statics.
- matcher: keep the UTF-8 text once, ASCII fast path for byte/char offsets.
- Bench: 8-thread validation benchmark; bench_compare honours CRITERION_HOME.
- README: before/after results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MasonLyons
MasonLyons merged commit 32d37b5 into main Dec 4, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant