fix: prevent regexp cache charsCurrent inflation on duplicate Put#68
Merged
fix: prevent regexp cache charsCurrent inflation on duplicate Put#68
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #68 +/- ##
==========================================
+ Coverage 89.67% 90.81% +1.14%
==========================================
Files 11 11
Lines 2984 3257 +273
==========================================
+ Hits 2676 2958 +282
+ Misses 210 204 -6
+ Partials 98 95 -3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
* remove excessive test * add fast path Signed-off-by: f41gh7 <nik@victoriametrics.com>
Contributor
|
CompileRegexp performs a non-atomic Get/compile/Put sequence. When multiple goroutines miss on Get for the same regexp concurrently, each calls Put, which blindly incremented charsCurrent without checking if the key already existed. This inflated the size counter while len(rc.m) stayed at 1, causing premature cache eviction and unnecessary regexp recompiles. This commit adds a check, if parsed regex was already added by concurrent goroutine. |
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
regexpCache.Putto only incrementcharsCurrentwhen the key is new, preventing size counter inflation from concurrent duplicate insertsPutto verify correct accountingProblem
CompileRegexpperforms a non-atomicGet/compile/Putsequence. When multiple goroutines miss onGetfor the same regexp concurrently, each callsPut, which blindly incrementedcharsCurrentwithout checking if the key already existed. This inflated the size counter whilelen(rc.m)stayed at 1, causing premature cache eviction and unnecessary regexp recompiles.Fix
Added an existence check before incrementing
charsCurrent:Test plan
TestRegexpCachewith duplicate-key inputsTestRegexpCacheDuplicatePutverifying triple-Put only counts chars once-racedetectorgo vetclean🤖 Generated with Claude Code
Summary by cubic
Fix
regexpCache.Putto count chars only for new keys and add a fast path for existing entries. Prevents counter inflation under concurrency and premature evictions; tests cover duplicates.Written for commit 958928e. Summary will update on new commits.