perf: lazy memory architecture — reduce per-pattern overhead 5-7x (#158)#160
Merged
Conversation
…158) Adopt Rust regex Cache separation model to reduce memory overhead from 16x to ~3x vs stdlib when compiling many patterns (WAF workloads). Changes: - PikeVM: add NewPikeVMLazy() with deferred internal state allocation - DFA: share Engine PikeVM via SetPikeVM(), eliminate per-DFA clones - SearchState: defer allocation to first search (not compile time) - SearchState: strategy-aware cache allocation (skip unused engines) - DFA cache: reduce initCap from 64 to 16 entries - CI: skip benchmark workflow for docs-only PRs For 900 OWASP CRS patterns, estimated savings: - PikeVM lazy init: ~10 KB per unused PikeVM (CharClass, Teddy, AC) - Shared DFA PikeVM: ~15-20 KB per DFA pattern - Deferred SearchState: ~15-50 KB per pattern at compile time - Strategy-aware caches: 30-70% fewer allocations per SearchState - DFA initCap 64->16: ~3 MB across 900 patterns Fixes #158
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Fixes maintidx lint: Cyclomatic Complexity 25 -> lower by extracting PikeVM sharing logic into a helper function.
Benchmark ComparisonComparing Summary:
|
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
Adopts Rust regex Cache separation model to dramatically reduce memory per compiled pattern. Addresses #158 where Coraza WAF (900 OWASP CRS patterns) saw 16x memory overhead vs stdlib.
Changes:
NewPikeVMLazy()defers thread queues/sparse set allocation to first search (~10 KB saved per unused PikeVM)SetPikeVM()eliminates duplicate PikeVM per DFA (~15-20 KB per DFA pattern)newSearchState()only allocates caches needed by active strategy (30-70% fewer allocations)Architecture (Rust model):
Test plan
go test ./...— all packages passgolangci-lint run— no new issuesgofmt— all modified files cleanBenchmarkFindFixes #158