Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduces transformArg allocation without multimatch #1198

Merged
merged 8 commits into from
Nov 14, 2024

Conversation

M4tteoP
Copy link
Member

@M4tteoP M4tteoP commented Nov 5, 2024

Avoids allocations when calling transformArg() without Multimatch. This has been done by specializing the transformArg into:

  • transformArg(..): which returns just an individual string with all the transformations computed. It permits to avoid allocating slices with just one element.
  • transformMultiMatchArg: which returns a slice of strings that include all the unique intermediate transformations

Before:

goos: darwin
goarch: arm64
pkg: github.com/corazawaf/coraza/v3/testing/coreruleset
cpu: Apple M1 Pro
BenchmarkCRSSimplePOST-10    	     901	   1256223 ns/op	  320564 B/op	    6420 allocs/op
PASS
ok  	github.com/corazawaf/coraza/v3/testing/coreruleset	2.051s

After:

BenchmarkCRSSimplePOST-10    	     902	   1252754 ns/op	  309035 B/op	    5657 allocs/op
PASS
ok  	github.com/corazawaf/coraza/v3/testing/coreruleset	2.053s

@M4tteoP M4tteoP marked this pull request as ready for review November 5, 2024 23:38
@M4tteoP M4tteoP requested a review from a team as a code owner November 5, 2024 23:38
@@ -242,8 +242,17 @@ func (r *Rule) doEvaluate(logger debuglog.Logger, phase types.RulePhase, tx *Tra
}
vLog.Debug().Msg("Expanding arguments for rule")

args := make([]string, 1)

Choose a reason for hiding this comment

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

it seems worth removing this allocation as well I guess?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I have some a WIP with further refactoring, but so far I've not been able to achieve more performance / fewer allocations, even removing this line. I would merge this first to get some optimizations in and iterate over for further ones

Choose a reason for hiding this comment

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

i see, nice try anyways!

Copy link

codecov bot commented Nov 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.64%. Comparing base (12d554b) to head (135eddb).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1198      +/-   ##
==========================================
+ Coverage   81.63%   81.64%   +0.01%     
==========================================
  Files         168      168              
  Lines        9630     9637       +7     
==========================================
+ Hits         7861     7868       +7     
  Misses       1519     1519              
  Partials      250      250              
Flag Coverage Δ
coraza.rule.case_sensitive_args_keys 81.60% <100.00%> (+0.01%) ⬆️
coraza.rule.multiphase_valuation 81.64% <100.00%> (+0.01%) ⬆️
default 81.64% <100.00%> (+0.01%) ⬆️
examples+ 16.43% <25.58%> (+0.04%) ⬆️
examples+coraza.rule.case_sensitive_args_keys 81.60% <100.00%> (+0.01%) ⬆️
examples+coraza.rule.multiphase_valuation 81.52% <100.00%> (+0.01%) ⬆️
examples+memoize_builders 81.65% <100.00%> (+0.01%) ⬆️
examples+no_fs_access 80.96% <100.00%> (+0.01%) ⬆️
ftw 81.64% <100.00%> (+0.01%) ⬆️
memoize_builders 81.73% <100.00%> (+0.01%) ⬆️
no_fs_access 81.08% <100.00%> (+0.01%) ⬆️
tinygo 81.61% <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.

@M4tteoP M4tteoP merged commit a5cc4f0 into main Nov 14, 2024
39 checks passed
@M4tteoP M4tteoP deleted the trans_args_allocs branch November 14, 2024 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants