Skip to content

Commit e507c41

Browse files
authored
eval: don't build trace message when tracing disabled (#7165)
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>
1 parent 91f20cc commit e507c41

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

topdown/eval.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -1461,20 +1461,23 @@ func (e *eval) getRules(ref ast.Ref, args []*ast.Term) (*ast.IndexResult, error)
14611461

14621462
result.EarlyExit = result.EarlyExit && e.earlyExit
14631463

1464-
var msg strings.Builder
1465-
if len(result.Rules) == 1 {
1466-
msg.WriteString("(matched 1 rule")
1467-
} else {
1468-
msg.Grow(len("(matched NNNN rules)"))
1469-
msg.WriteString("(matched ")
1470-
msg.WriteString(strconv.Itoa(len(result.Rules)))
1471-
msg.WriteString(" rules")
1472-
}
1473-
if result.EarlyExit {
1474-
msg.WriteString(", early exit")
1464+
if e.traceEnabled {
1465+
var msg strings.Builder
1466+
if len(result.Rules) == 1 {
1467+
msg.WriteString("(matched 1 rule")
1468+
} else {
1469+
msg.Grow(len("(matched NNNN rules)"))
1470+
msg.WriteString("(matched ")
1471+
msg.WriteString(strconv.Itoa(len(result.Rules)))
1472+
msg.WriteString(" rules")
1473+
}
1474+
if result.EarlyExit {
1475+
msg.WriteString(", early exit")
1476+
}
1477+
msg.WriteRune(')')
1478+
e.traceIndex(e.query[e.index], msg.String(), &ref)
14751479
}
1476-
msg.WriteRune(')')
1477-
e.traceIndex(e.query[e.index], msg.String(), &ref)
1480+
14781481
return result, err
14791482
}
14801483

0 commit comments

Comments
 (0)