Repository navigation
Regex layer: lock-free pattern cache, single matching, no per-call allocation - #4
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
RwLock). Once warm, a lookup takes no lock, allocates nothing, and hashes with FxHash. Before, every call took one global mutex and allocated aStringkey, so Python threads running GIL-released batch calls all queued on that mutex.fullmatch: the^(?:…)$form is compiled once instead of being built withformat!on every call.match_number_against_patternruns one regex call instead of two (anchored match, then full match). The newengine::is_match_herereplacesmatch_here(..).is_some()and skips capture groups.LazyLockstatics.find(). For ASCII text, byte and char offsets map directly instead of rescanning from the start.tools/bench_compare.pynow honoursCRITERION_HOME.Benchmarks
Two alternating before/after rounds, run against a
mainworktree with the same bench file:is_valid_number×23format_number×23is_valid_number, 8 threadsnormalize_digits_onlyis 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:is_match_hereagreeing withmatch_herepytest, with the differential fuzz against python-phonenumbers run atPHONES_FUZZ_EXAMPLES=5000: 58 passed.cargo clippy -D warnings,cargo fmt, and an MSRV 1.88 check ofphonenumbers-rs.🤖 Generated with Claude Code