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

cache Rule ID string version #1039

Merged
merged 1 commit into from
Apr 17, 2024
Merged

Conversation

noboruma
Copy link
Contributor

@noboruma noboruma commented Apr 9, 2024

While testing the library at 1K RPS, pprof showed that this strconv.Itoa alone is adding a ~2.2% CPU usage.
Since this ID is per rule and does not change over time, we can compute it once, cache it and reuse.

@noboruma noboruma requested a review from a team as a code owner April 9, 2024 09:39
Remove `strconv.Itoa(rid)` from hot path by caching its result
Copy link

codecov bot commented Apr 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.07%. Comparing base (4ff1f76) to head (363717c).
Report is 27 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1039      +/-   ##
==========================================
+ Coverage   82.72%   83.07%   +0.35%     
==========================================
  Files         162      162              
  Lines        9080     7611    -1469     
==========================================
- Hits         7511     6323    -1188     
+ Misses       1319     1037     -282     
- Partials      250      251       +1     
Flag Coverage Δ
default 83.07% <100.00%> (+5.23%) ⬆️
examples 83.07% <100.00%> (+56.64%) ⬆️
ftw 83.07% <100.00%> (+35.70%) ⬆️
ftw-multiphase 83.07% <100.00%> (+33.53%) ⬆️
tinygo 83.07% <100.00%> (+7.67%) ⬆️

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.

@@ -76,3 +84,14 @@ func (r *RuleMetadata) Raw() string {
func (r *RuleMetadata) SecMark() string {
return r.SecMark_
}

func (r *RuleMetadata) StrID() string {
if r.cachedStrID_ == "" {
Copy link
Member

Choose a reason for hiding this comment

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

@jcchavezs is empty comparison as fast as a nil pointer comparison?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, they both just compare a value to 0

@jptosso
Copy link
Member

jptosso commented Apr 9, 2024

Thank you very much for your contribution, looks great.
We are taking a look

Copy link
Member

@jcchavezs jcchavezs left a comment

Choose a reason for hiding this comment

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

Amazing!

@jcchavezs jcchavezs requested a review from anuraaga April 10, 2024 11:08
@@ -76,3 +84,14 @@ func (r *RuleMetadata) Raw() string {
func (r *RuleMetadata) SecMark() string {
return r.SecMark_
}

func (r *RuleMetadata) StrID() string {
if r.cachedStrID_ == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

There aren't that many rules and I think this is used fairly commonly. Should we just populate it eagerly without the lazy pattern?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would be the best solution, however it seems there is no init function for those fields. We will need to introduce a New function & use that in a few places to get this done properly (the tricky bit being determining if the rule is part of a chain or not, in which case we take parent ID)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah it's a bigger change - I think it's fine to do but if it's too much for now, we can merge as is too

Copy link
Member

Choose a reason for hiding this comment

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

In the end using lazy pattern leads to a race condition #1083

@@ -76,3 +84,14 @@ func (r *RuleMetadata) Raw() string {
func (r *RuleMetadata) SecMark() string {
return r.SecMark_
}

func (r *RuleMetadata) StrID() string {
if r.cachedStrID_ == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, they both just compare a value to 0

@jcchavezs jcchavezs added the v3.2 label Apr 11, 2024
@jcchavezs
Copy link
Member

Could you address the feedback @noboruma?

@jcchavezs jcchavezs merged commit d1c26a6 into corazawaf:main Apr 17, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants