Skip to content

Commit e778137

Browse files
committed
internal/core/adt: prevent logging overhead when disabled
LogEval is zero by default, and its implementation via Indentf has a non-zero cost which I spotted via pprof, so avoid calling the funcs entirely in all the call sites. Some already skipped via an early LogEval check, but others did not. Benchmarking the code from https://cuelang.org/issue/3881 via benchcmd -n 8 CueVetIssue3881 cue vet -c f.cue: │ old │ new │ │ sec/op │ sec/op vs base │ CueVetIssue3881 507.4m ± 0% 496.0m ± 1% -2.24% (p=0.000 n=8) │ old │ new │ │ user-sec/op │ user-sec/op vs base │ CueVetIssue3881 571.9m ± 1% 560.0m ± 1% -2.08% (p=0.003 n=8) │ old │ new │ │ sys-sec/op │ sys-sec/op vs base │ CueVetIssue3881 12.159m ± 51% 9.510m ± 58% ~ (p=0.234 n=8) │ old │ new │ │ peak-RSS-bytes │ peak-RSS-bytes vs base │ CueVetIssue3881 47.95Mi ± 3% 49.42Mi ± 4% ~ (p=0.857 n=8) Updates #3881. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I8ec0215239e97f6e8aeaeb50196b42910d91a815 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1213054 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent a09a38b commit e778137

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

Diff for: internal/core/adt/log.go

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func (c *OpContext) Un(s nestString, id int) {
6868
// Indentf logs a function call and increases the nesting level.
6969
// The first argument must be the function name.
7070
func (c *OpContext) Indentf(v *Vertex, format string, args ...any) (s nestString, id int) {
71+
if c.LogEval == 0 {
72+
panic("avoid calling OpContext.Indentf when logging is disabled to prevent overhead")
73+
}
7174
name := strings.Split(format, "(")[0]
7275
if name == "" {
7376
name, _ = getCallerFunctionName(1)

Diff for: internal/core/adt/sched.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,9 @@ func runTask(t *task, mode runMode) {
655655
return
656656
}
657657
ctx := t.node.ctx
658-
defer ctx.Un(ctx.Indentf(t.node.node, "RUNTASK(%v, %v)", t.run.name, t.x))
658+
if ctx.LogEval > 0 {
659+
defer ctx.Un(ctx.Indentf(t.node.node, "RUNTASK(%v, %v)", t.run.name, t.x))
660+
}
659661

660662
switch t.state {
661663
case taskSUCCESS, taskFAILED:

Diff for: internal/core/adt/unify.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,9 @@ func (v *Vertex) unify(c *OpContext, needs condition, mode runMode, checkTypos b
486486
// NOT:
487487
// - complete value. That is reserved for Unify.
488488
func (n *nodeContext) completeNodeTasks(mode runMode) {
489-
defer n.ctx.Un(n.ctx.Indentf(n.node, "(%v)", mode))
489+
if n.ctx.LogEval > 0 {
490+
defer n.ctx.Un(n.ctx.Indentf(n.node, "(%v)", mode))
491+
}
490492

491493
n.assertInitialized()
492494

0 commit comments

Comments
 (0)