Skip to content

Commit

Permalink
cache Rule ID string version
Browse files Browse the repository at this point in the history
Remove `strconv.Itoa(rid)` from hot path by caching its result
  • Loading branch information
noboruma committed Apr 9, 2024
1 parent dc77812 commit a312768
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
36 changes: 23 additions & 13 deletions internal/corazarules/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@
package corazarules

import (
"strconv"

"github.com/corazawaf/coraza/v3/types"
)

// RuleMetadata is used to store rule metadata
// that can be used across packages
type RuleMetadata struct {
ID_ int
File_ string
Line_ int
Rev_ string
Severity_ types.RuleSeverity
Version_ string
Tags_ []string
Maturity_ int
Accuracy_ int
Operator_ string
Phase_ types.RulePhase
Raw_ string
SecMark_ string
ID_ int
File_ string
Line_ int
Rev_ string
Severity_ types.RuleSeverity
Version_ string
Tags_ []string
Maturity_ int
Accuracy_ int
Operator_ string
Phase_ types.RulePhase
Raw_ string
SecMark_ string
cachedStrID_ string
}

func (r *RuleMetadata) ID() int {
Expand Down Expand Up @@ -76,3 +79,10 @@ func (r *RuleMetadata) Raw() string {
func (r *RuleMetadata) SecMark() string {
return r.SecMark_
}

func (r *RuleMetadata) StrID() string {
if r.cachedStrID_ == "" {
r.cachedStrID_ = strconv.Itoa(r.ID_)
}
return r.cachedStrID_
}
3 changes: 1 addition & 2 deletions internal/corazawaf/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"unsafe"
Expand Down Expand Up @@ -204,7 +203,7 @@ func (r *Rule) doEvaluate(logger debuglog.Logger, phase types.RulePhase, tx *Tra
defer logger.Debug().Msg("Finished rule evaluation")

ruleCol := tx.variables.rule
ruleCol.SetIndex("id", 0, strconv.Itoa(rid))
ruleCol.SetIndex("id", 0, r.StrID())
if r.Msg != nil {
ruleCol.SetIndex("msg", 0, r.Msg.String())
}
Expand Down

0 comments on commit a312768

Please sign in to comment.