perf: store transformationValue by value in cache map#1528
perf: store transformationValue by value in cache map#1528
Conversation
Store transformationValue structs directly in the transformation cache map instead of as pointers. This eliminates one heap allocation per cache entry and improves data locality for cache lookups. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1528 +/- ##
==========================================
- Coverage 85.95% 85.95% -0.01%
==========================================
Files 175 175
Lines 8567 8566 -1
==========================================
- Hits 7364 7363 -1
Misses 957 957
Partials 246 246
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR optimizes Coraza’s per-transaction transformation cache by storing transformationValue entries by value in the cache map (instead of pointers), reducing heap allocations and improving cache lookup locality during rule evaluation.
Changes:
- Change transformation cache map value type from
*transformationValuetotransformationValueacross transaction, WAF initialization, and rule evaluation APIs. - Store
transformationValuedirectly into the cache inRule.transformArg. - Update unit tests to use the new cache map type.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/corazawaf/rule.go | Updates rule evaluation/transform cache plumbing and stores cached transformation results by value. |
| internal/corazawaf/transaction.go | Updates Transaction.transformationCache field type to store values. |
| internal/corazawaf/waf.go | Initializes the transaction’s transformation cache with the new map value type. |
| internal/corazawaf/rule_test.go | Updates tests to use the new transformation cache map value type. |
| return cached.arg, cached.errs | ||
| } else { | ||
| ars, es := r.executeTransformations(arg.Value()) | ||
| errs := es |
There was a problem hiding this comment.
very curious about this, I think the idea is to avoid mutations of the errors but I am not sure why.
Summary
transformationValuestructs directly in the transformation cache map instead of as pointersgo build -gcflags='-m':&transformationValue{...} escapes to heapon main, gone on branch)Benchmark
BenchmarkRuleEvalWithTransformations— 1 rule withlowercasetransformation evaluatingARGS(5 query params):The 5-alloc reduction matches the 5 cached entries (one per ARGS key). Each additional cached transformation saves 1 alloc.
Test plan
go test ./internal/corazawaf/ -count=1passesBenchmarkRuleEvalWithTransformationsproves the alloc reductiongo build -gcflags='-m'confirms&transformationValue{...}no longer escapes to heap