Repository navigation
perf: cache compiled path regexes and cut request validation allocations - #340
Open
emilien-puget wants to merge 1 commit into
Open
emilien-puget wants to merge 1 commit into
emilien-puget wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #340 +/- ##
==========================================
+ Coverage 98.74% 98.82% +0.07%
==========================================
Files 94 95 +1
Lines 12310 12462 +152
==========================================
+ Hits 12156 12316 +160
+ Misses 129 125 -4
+ Partials 25 21 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Request validation called helpers.GetRegexForPath for every path template on every request and recompiled the same regex each time, and the rest of the request hot path rebuilt per-request state it could reuse. This closes the gap with libopenapi, which caches compiled JSONPath expressions the same way. Changes: - Cache compiled path regexes by template in GetRegexForPath (package-level sync.Map, LoadOrStore, invalid templates uncached), with ClearRegexCache() following the libopenapi cache convention of ClearJSONPathCache(). The brace indices are cached with the compiled regex; GetBraceIndicesForPath reads them from the same entry. - ExtractParamsForOperation returns the model slices untouched when one side is empty and merges into an exact-size slice otherwise: appending to item.Parameters can write into the model backing array, and the model stays shared across validators and requests. - ValidateRequestSchema, ValidateResponseSchema and ValidateResponseHeaders accept pre-built options through an unexported core; internal callers stop building config.NewValidationOptions per request. Exported signatures are unchanged. - nonEmptyPathSegments walks the path once with strings.SplitSeq. - ExtractRawQueryValues returns nil for an empty query. - GetRenderedSchema returns the cached ReferenceSchema string instead of copying bytes per call. - compilePathRegexEntry assembles patterns with strings.Builder: bytes.Buffer writes cannot fail, so the unreachable error branch and the fmt call with its interface boxing are gone. Evidence: paired runs, 10 samples per side, benchstat (i7-1265U, Go 1.26), all measured back-to-back per commit, main vs branch across 12 gateway scenarios including query-heavy, invalid-request, concurrent, format-assertions, strict-server, urlencoded, JSON-response-body and 200-route shapes: - plain GET: 97 -> 35 allocs/op (-64%), 11.1 -> 5.7 us (-48%) - param-heavy GET: 324 -> 94 allocs/op (-71%), 37.7 -> 14.6 us (-61%) - invalid request: 326 -> 96 allocs/op (-71%), 42.5 -> 16.7 us (-61%) - concurrent GET, 12 goroutines: 97 -> 35 allocs/op, 6.7 -> 2.4 us - large route table (200 paths): 114 -> 35 allocs/op (-69%), -61% - format assertions: 98 -> 36 allocs/op; strict server: 99 -> 37 - urlencoded body: 97 -> 85; JSON response body: 53 -> 44 - geomean: -42% sec/op, -54% B/op, -51% allocs/op, p<0.001 Memory: an entry holds a few KB (measured 4.3 KB: key string plus compiled regex), so a 1,000-route API costs about 4 MB. ClearRegexCache bounds it for long-running processes, matching the ClearJSONPathCache contract in libopenapi. One candidate was measured and rejected, recorded here: hoisting the per-segment regex out of the parameter loop cost +9% sec/op on the single-parameter shape (three precompute slices, nothing to dedupe). Verification: go test -race -count=1 ./... (21/21 packages), make lint (0 issues), new tests for cache identity, errors staying uncached, ClearRegexCache, cached brace indices, the merge and model-slice safety, and the unbraced-segment skip; 12 benchmark scenarios in validator_request_bench_test.go and validator_scenario_bench_test.go.
emilien-puget
force-pushed
the
path-regex-cache
branch
from
November 12, 2026 17:23
5371269 to
d2edbce
Compare
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.
Goal
Request validation called
helpers.GetRegexForPathfor every path template on every request and recompiled the same regex each time, although the template is constant per route. This PR caches compiled regexes by template and then applies the same cache discipline to the rest of the request and response hot path: stop rebuilding per-request state, hoist per-call allocations out of loops, and reuse what the validator already built.It closes the gap with libopenapi, which already caches its compiled JSONPath expressions the same way; the validator's request path was still recompiling on every request.
What changed
01df353helpers.GetRegexForPath(package-levelsync.Map,LoadOrStore, invalid templates uncached), plusClearRegexCache()5501815GetBraceIndicesForPath);ExtractParamsForOperationno longer appends onto the model's own slice5f5262eValidateRequestSchema/ValidateResponseSchemaaccept pre-builtValidationOptionsinstead of building defaults per calla6a047dnonEmptyPathSegmentswalks the path once (strings.SplitSeq) instead ofSplit+ copy71259a3ExtractRawQueryValuesreturns nil for an empty queryff19efcGetRenderedSchemareturns the cachedReferenceSchemastring instead of copying bytes per callb941576ValidateResponseHeadersaccepts pre-built options through an unexported core; exported signature unchangedce3ba8e,46c57ee,3150fb0Evidence
Paired runs, 10 samples per side, compared with
benchstat(i7-1265U, Go 1.26). Allbenchandperfcommits were measured individually with back-to-back before/after runs; the table below comparesmainagainst the full branch on all 12 scenarios. Allocation counts are exact (±0%), time is p<0.001 for the shapes this PR touches.Reproduce:
Mechanism and memory
GetRegexForPathchecks a package-levelsync.Mapkeyed by the template string, then compiles and stores withLoadOrStore(concurrent callers race once; the loser's regex is discarded). The original body moved unchanged to an internal compile function. Invalid templates are not cached. The existingconfig.RegexCacheoption keeps working; this PR makes the default path fast with no configuration.For bounding, this follows the cache convention libopenapi introduced for content-keyed caches:
utils.ClearJSONPathCache()documents "Call this between document lifecycles in long-running processes to bound memory" and is wired intoClearAllCaches(). This PR adds the same contract ashelpers.ClearRegexCache(). An entry holds a few KB (measured 4.3 KB: key string plus compiled regex), so a 1,000-route API costs about 4 MB.One candidate was measured and rejected, and the negative result is recorded in the commit message: hoisting the per-segment regex out of the parameter loop cost +9% sec/op on the single-parameter shape (three precompute slices, nothing to dedupe), so it was reverted.
Verification
go test -race -count=1 ./...— 21/21 packages PASS (run after every change).make lint(golangci-lint, repo config) — 0 issues.ClearRegexCache, cached brace indices (identity + invalid template).extractPathParamsmaps,FindStringSubmatchIndexvariant, integermath/bigfast path); they touch the exportedRouteshape or vendored-schema behavior and are deliberately out of this diff.