perf: cache compiled CRD regex + skip hex-preview Vec allocation#40
Open
indyjonesnl wants to merge 2 commits into
Open
perf: cache compiled CRD regex + skip hex-preview Vec allocation#40indyjonesnl wants to merge 2 commits into
indyjonesnl wants to merge 2 commits into
Conversation
validate_string recompiled regex::Regex::new(pattern) on every CRD field that declared a pattern. Cache compiled patterns in a process-wide LazyLock<RwLock<HashMap<String, Regex>>>; bad patterns are still returned as InvalidResource errors and are not cached. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Avoids the intermediate Vec<String> from collect()+join() in the protobuf debug-logging path. Co-Authored-By: Claude Opus 4.7 (1M context) <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.
Changes
Two unrelated perf wins, kept together because both are small and obviously safe.
`perf(schema-validation): cache compiled regex patterns`
`SchemaValidator::validate_string` recompiled `regex::Regex::new(pattern)` on every CRD field that declared a `pattern`. On a busy admission webhook this is a measurable hot path — regex compile dominates the validation cost.
Cache compiled patterns in a process-wide `LazyLock<RwLock<HashMap<String, Regex>>>`. Read-lock for the common hit path; write-lock only on miss. Bad regexes still return `InvalidResource` errors and are not cached.
Includes a microbenchmark (`#[ignore]`, run with `--release --ignored`) that compares the cached path against `Regex::new` per call:
```
regex_cache_speedup: n=5000 cached=3.59ms uncached=259.39ms speedup=72.3x
```
`perf(middleware): build hex body preview directly into String`
Protobuf debug-logging path used `.map(|b| format!("{:02x}", b)).collect::<Vec<_>>().join(" ")`, allocating an intermediate `Vec` of 80 entries on every protobuf request. Write directly into a pre-allocated `String` via `write!` — same output, one allocation instead of 81.
Verification
`cargo build --workspace --locked` clean. Two new tests on the regex cache (correctness + the ignored bench) pass. No new test failures vs baseline.
Depends on #32 for green CI.