Skip to content

Commit d1c26a6

Browse files
authored
cache Rule ID string version (#1039)
Remove `strconv.Itoa(rid)` from hot path by caching its result
1 parent 099f220 commit d1c26a6

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

internal/corazarules/rule.go

+32-13
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,33 @@
44
package corazarules
55

66
import (
7+
"strconv"
8+
79
"github.com/corazawaf/coraza/v3/types"
810
)
911

12+
const noID = 0
13+
1014
// RuleMetadata is used to store rule metadata
1115
// that can be used across packages
1216
type RuleMetadata struct {
13-
ID_ int
14-
File_ string
15-
Line_ int
16-
Rev_ string
17-
Severity_ types.RuleSeverity
18-
Version_ string
19-
Tags_ []string
20-
Maturity_ int
21-
Accuracy_ int
22-
Operator_ string
23-
Phase_ types.RulePhase
24-
Raw_ string
25-
SecMark_ string
17+
ID_ int
18+
File_ string
19+
Line_ int
20+
Rev_ string
21+
Severity_ types.RuleSeverity
22+
Version_ string
23+
Tags_ []string
24+
Maturity_ int
25+
Accuracy_ int
26+
Operator_ string
27+
Phase_ types.RulePhase
28+
Raw_ string
29+
SecMark_ string
30+
cachedStrID_ string
31+
// Contains the Id of the parent rule if you are inside
32+
// a chain. Otherwise, it will be 0
33+
ParentID_ int
2634
}
2735

2836
func (r *RuleMetadata) ID() int {
@@ -76,3 +84,14 @@ func (r *RuleMetadata) Raw() string {
7684
func (r *RuleMetadata) SecMark() string {
7785
return r.SecMark_
7886
}
87+
88+
func (r *RuleMetadata) StrID() string {
89+
if r.cachedStrID_ == "" {
90+
rid := r.ID_
91+
if rid == noID {
92+
rid = r.ParentID_
93+
}
94+
r.cachedStrID_ = strconv.Itoa(rid)
95+
}
96+
return r.cachedStrID_
97+
}

internal/corazawaf/rule.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"reflect"
99
"regexp"
10-
"strconv"
1110
"strings"
1211
"sync"
1312
"unsafe"
@@ -104,10 +103,6 @@ type Rule struct {
104103
// the rule evaluation process
105104
actions []ruleActionParams
106105

107-
// Contains the Id of the parent rule if you are inside
108-
// a chain. Otherwise, it will be 0
109-
ParentID_ int
110-
111106
// Capture is used by the transaction to tell the operator
112107
// to capture variables on TX:0-9
113108
Capture bool
@@ -189,11 +184,6 @@ const noID = 0
189184
func (r *Rule) doEvaluate(logger debuglog.Logger, phase types.RulePhase, tx *Transaction, collectiveMatchedValues *[]types.MatchData, chainLevel int, cache map[transformationKey]*transformationValue) []types.MatchData {
190185
tx.Capture = r.Capture
191186

192-
rid := r.ID_
193-
if rid == noID {
194-
rid = r.ParentID_
195-
}
196-
197187
if multiphaseEvaluation {
198188
computeRuleChainMinPhase(r)
199189
}
@@ -204,7 +194,7 @@ func (r *Rule) doEvaluate(logger debuglog.Logger, phase types.RulePhase, tx *Tra
204194
defer logger.Debug().Msg("Finished rule evaluation")
205195

206196
ruleCol := tx.variables.rule
207-
ruleCol.SetIndex("id", 0, strconv.Itoa(rid))
197+
ruleCol.SetIndex("id", 0, r.StrID())
208198
if r.Msg != nil {
209199
ruleCol.SetIndex("msg", 0, r.Msg.String())
210200
}

0 commit comments

Comments
 (0)