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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 45 additions & 37 deletions internal/corazawaf/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,17 @@
}
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!

var errs []error
var argsLen int
for i, arg := range values {
args, errs := r.transformArg(arg, i, cache)
if r.MultiMatch {
args, errs = r.transformMultiMatchArg(arg)
argsLen = len(args)

Check warning on line 251 in internal/corazawaf/rule.go

View check run for this annotation

Codecov / codecov/patch

internal/corazawaf/rule.go#L250-L251

Added lines #L250 - L251 were not covered by tests
} else {
args[0], errs = r.transformArg(arg, i, cache)
argsLen = 1
}
if len(errs) > 0 {
vWarnLog := vLog.Warn()
if vWarnLog.IsEnabled() {
Expand All @@ -255,7 +264,7 @@
}

// args represents the transformed variables
for _, carg := range args {
for _, carg := range args[:argsLen] {
evalLog := vLog.
Debug().
Str("operator_function", r.operator.Function).
Expand Down Expand Up @@ -381,42 +390,41 @@
return matchedValues
}

func (r *Rule) transformArg(arg types.MatchData, argIdx int, cache map[transformationKey]*transformationValue) ([]string, []error) {
if r.MultiMatch {
// TODOs:
// - We don't need to run every transformation. We could try for each until found
// - Cache is not used for multimatch
return r.executeTransformationsMultimatch(arg.Value())
} else {
switch {
case len(r.transformations) == 0:
return []string{arg.Value()}, nil
case arg.Variable().Name() == "TX":
// no cache for TX
arg, errs := r.executeTransformations(arg.Value())
return []string{arg}, errs
default:
// NOTE: See comment on transformationKey struct to understand this hacky code
argKey := arg.Key()
argKeyPtr := unsafe.StringData(argKey)
key := transformationKey{
argKey: argKeyPtr,
argIndex: argIdx,
argVariable: arg.Variable(),
transformationsID: r.transformationsID,
}
if cached, ok := cache[key]; ok {
return cached.args, cached.errs
} else {
ars, es := r.executeTransformations(arg.Value())
args := []string{ars}
errs := es
cache[key] = &transformationValue{
args: args,
errs: es,
}
return args, errs
func (r *Rule) transformMultiMatchArg(arg types.MatchData) ([]string, []error) {
// TODOs:
// - We don't need to run every transformation. We could try for each until found
// - Cache is not used for multimatch
return r.executeTransformationsMultimatch(arg.Value())

Check warning on line 397 in internal/corazawaf/rule.go

View check run for this annotation

Codecov / codecov/patch

internal/corazawaf/rule.go#L393-L397

Added lines #L393 - L397 were not covered by tests
}

func (r *Rule) transformArg(arg types.MatchData, argIdx int, cache map[transformationKey]*transformationValue) (string, []error) {
switch {
case len(r.transformations) == 0:
return arg.Value(), nil
case arg.Variable().Name() == "TX":
// no cache for TX
arg, errs := r.executeTransformations(arg.Value())
return arg, errs
default:
// NOTE: See comment on transformationKey struct to understand this hacky code
argKey := arg.Key()
argKeyPtr := unsafe.StringData(argKey)
key := transformationKey{
argKey: argKeyPtr,
argIndex: argIdx,
argVariable: arg.Variable(),
transformationsID: r.transformationsID,
}
if cached, ok := cache[key]; ok {
return cached.arg, cached.errs
} else {
ars, es := r.executeTransformations(arg.Value())
errs := es
cache[key] = &transformationValue{
arg: ars,
errs: es,

Check warning on line 425 in internal/corazawaf/rule.go

View check run for this annotation

Codecov / codecov/patch

internal/corazawaf/rule.go#L404-L425

Added lines #L404 - L425 were not covered by tests
}
return ars, errs

Check warning on line 427 in internal/corazawaf/rule.go

View check run for this annotation

Codecov / codecov/patch

internal/corazawaf/rule.go#L427

Added line #L427 was not covered by tests
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions internal/corazawaf/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,23 +506,23 @@ func TestTransformArgSimple(t *testing.T) {
rule := NewRule()
_ = rule.AddTransformation("AppendA", transformationAppendA)
_ = rule.AddTransformation("AppendB", transformationAppendB)
args, errs := rule.transformArg(md, 0, transformationCache)
arg, errs := rule.transformArg(md, 0, transformationCache)
if errs != nil {
t.Fatalf("Unexpected errors executing transformations: %v", errs)
}
if args[0] != "/testAB" {
t.Errorf("Expected \"/testAB\", got \"%s\"", args[0])
if arg != "/testAB" {
t.Errorf("Expected \"/testAB\", got \"%s\"", arg)
}
if len(transformationCache) != 1 {
t.Errorf("Expected 1 transformations in cache, got %d", len(transformationCache))
}
// Repeating the same transformation, expecting still one element in the cache (that means it is a cache hit)
args, errs = rule.transformArg(md, 0, transformationCache)
arg, errs = rule.transformArg(md, 0, transformationCache)
if errs != nil {
t.Fatalf("Unexpected errors executing transformations: %v", errs)
}
if args[0] != "/testAB" {
t.Errorf("Expected \"/testAB\", got \"%s\"", args[0])
if arg != "/testAB" {
t.Errorf("Expected \"/testAB\", got \"%s\"", arg)
}
if len(transformationCache) != 1 {
t.Errorf("Expected 1 transformations in cache, got %d", len(transformationCache))
Expand All @@ -538,12 +538,12 @@ func TestTransformArgNoCacheForTXVariable(t *testing.T) {
}
rule := NewRule()
_ = rule.AddTransformation("AppendA", transformationAppendA)
args, errs := rule.transformArg(md, 0, transformationCache)
arg, errs := rule.transformArg(md, 0, transformationCache)
if errs != nil {
t.Fatalf("Unexpected errors executing transformations: %v", errs)
}
if args[0] != "testA" {
t.Errorf("Expected \"testA\", got \"%s\"", args[0])
if arg != "testA" {
t.Errorf("Expected \"testA\", got \"%s\"", arg)
}
if len(transformationCache) != 0 {
t.Errorf("Expected 0 transformations in cache, got %d. It is not expected to cache TX variable transformations", len(transformationCache))
Expand Down
2 changes: 1 addition & 1 deletion internal/corazawaf/rulegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,6 @@ type transformationKey struct {
}

type transformationValue struct {
args []string
arg string
errs []error
}
Loading