Skip to content

perf: store transformationValue by value in cache map#1528

Open
jptosso wants to merge 3 commits intomainfrom
perf/transformation-cache-by-value
Open

perf: store transformationValue by value in cache map#1528
jptosso wants to merge 3 commits intomainfrom
perf/transformation-cache-by-value

Conversation

@jptosso
Copy link
Copy Markdown
Member

@jptosso jptosso commented Mar 6, 2026

Summary

  • Store transformationValue structs directly in the transformation cache map instead of as pointers
  • Eliminates one heap allocation per cache entry (confirmed via go build -gcflags='-m': &transformationValue{...} escapes to heap on main, gone on branch)
  • Improves data locality for cache lookups during rule evaluation

Benchmark

BenchmarkRuleEvalWithTransformations — 1 rule with lowercase transformation evaluating ARGS (5 query params):

main (*transformationValue pointer):
BenchmarkRuleEvalWithTransformations-10    807051    1491 ns/op    2103 B/op    40 allocs/op

branch (transformationValue by value):
BenchmarkRuleEvalWithTransformations-10    862641    1416 ns/op    1857 B/op    35 allocs/op
Metric Change
Speed -5%
Allocs 40 → 35 (-12.5%)
Bytes 2103 → 1857 (-12%)

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=1 passes
  • BenchmarkRuleEvalWithTransformations proves the alloc reduction
  • go build -gcflags='-m' confirms &transformationValue{...} no longer escapes to heap

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
Copy link
Copy Markdown

codecov bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.95%. Comparing base (3347961) to head (23126c4).
⚠️ Report is 1 commits behind head on main.

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              
Flag Coverage Δ
coraza.rule.case_sensitive_args_keys 85.92% <100.00%> (-0.01%) ⬇️
coraza.rule.mandatory_rule_id_check 85.94% <100.00%> (-0.01%) ⬇️
coraza.rule.multiphase_evaluation 85.67% <100.00%> (-0.01%) ⬇️
coraza.rule.no_regex_multiline 85.94% <100.00%> (-0.01%) ⬇️
default 85.95% <100.00%> (-0.01%) ⬇️
examples+ 16.56% <66.66%> (+<0.01%) ⬆️
examples+coraza.rule.case_sensitive_args_keys 83.94% <100.00%> (-0.01%) ⬇️
examples+coraza.rule.mandatory_rule_id_check 84.05% <100.00%> (-0.01%) ⬇️
examples+coraza.rule.multiphase_evaluation 85.67% <100.00%> (-0.01%) ⬇️
examples+coraza.rule.no_regex_multiline 83.90% <100.00%> (-0.01%) ⬇️
examples+memoize_builders 84.03% <100.00%> (-0.01%) ⬇️
examples+no_fs_access 83.58% <100.00%> (-0.01%) ⬇️
ftw 85.95% <100.00%> (-0.01%) ⬇️
memoize_builders 86.07% <100.00%> (-0.01%) ⬇️
no_fs_access 85.46% <100.00%> (-0.01%) ⬇️
tinygo 85.93% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 *transformationValue to transformationValue across transaction, WAF initialization, and rule evaluation APIs.
  • Store transformationValue directly into the cache in Rule.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.

@jptosso jptosso marked this pull request as ready for review March 6, 2026 14:32
@jptosso jptosso requested a review from a team as a code owner March 6, 2026 14:32
return cached.arg, cached.errs
} else {
ars, es := r.executeTransformations(arg.Value())
errs := es
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very curious about this, I think the idea is to avoid mutations of the errors but I am not sure why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants