Skip to content

Commit

Permalink
eval: don't build trace message when tracing disabled (#7165)
Browse files Browse the repository at this point in the history
Running `regal lint` on Regal's own policies, this accounts for nearly
2 million allocations 😵

**Before**
```
BenchmarkRegalLintingItself-10    	       1	3314104416 ns/op	6720813824 B/op	128361292 allocs/op
```

**After**
```
BenchmarkRegalLintingItself-10    	       1	3185755333 ns/op	6684952216 B/op	126912887 allocs/op
```

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored Nov 10, 2024
1 parent 91f20cc commit e507c41
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions topdown/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1461,20 +1461,23 @@ func (e *eval) getRules(ref ast.Ref, args []*ast.Term) (*ast.IndexResult, error)

result.EarlyExit = result.EarlyExit && e.earlyExit

var msg strings.Builder
if len(result.Rules) == 1 {
msg.WriteString("(matched 1 rule")
} else {
msg.Grow(len("(matched NNNN rules)"))
msg.WriteString("(matched ")
msg.WriteString(strconv.Itoa(len(result.Rules)))
msg.WriteString(" rules")
}
if result.EarlyExit {
msg.WriteString(", early exit")
if e.traceEnabled {
var msg strings.Builder
if len(result.Rules) == 1 {
msg.WriteString("(matched 1 rule")
} else {
msg.Grow(len("(matched NNNN rules)"))
msg.WriteString("(matched ")
msg.WriteString(strconv.Itoa(len(result.Rules)))
msg.WriteString(" rules")
}
if result.EarlyExit {
msg.WriteString(", early exit")
}
msg.WriteRune(')')
e.traceIndex(e.query[e.index], msg.String(), &ref)
}
msg.WriteRune(')')
e.traceIndex(e.query[e.index], msg.String(), &ref)

return result, err
}

Expand Down

0 comments on commit e507c41

Please sign in to comment.