Skip to content

Commit

Permalink
log: optimize logEntry.SafeFormat
Browse files Browse the repository at this point in the history
via `BenchmarkEventf_WithVerboseTraceSpan`.

```
❯ benchstat before.txt after.txt
name                           old time/op    new time/op    delta
Eventf_WithVerboseTraceSpan-8    5.01µs ± 1%    4.37µs ± 2%  -12.72%  (p=0.000 n=8+9)

name                           old alloc/op   new alloc/op   delta
Eventf_WithVerboseTraceSpan-8    1.20kB ± 0%    1.08kB ± 0%  -10.07%  (p=0.000 n=10+10)

name                           old allocs/op  new allocs/op  delta
Eventf_WithVerboseTraceSpan-8      22.0 ± 0%      16.0 ± 0%  -27.27%  (p=0.000 n=10+10)
```

Release note: None
  • Loading branch information
tbg authored and dhartunian committed Dec 3, 2021
1 parent fa6af06 commit 5cd6c77
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/util/log/log_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ func (e *logEntry) SafeFormat(w interfaces.SafePrinter, _ rune) {
// with a colon between the line number and the message.
// However, some location filter deep inside SQL doesn't
// understand a colon after the line number.
w.Printf("%s:%d ", redact.Safe(e.file), redact.Safe(e.line))
w.SafeString(redact.SafeString(e.file))
w.SafeRune(':')
w.SafeInt(redact.SafeInt(e.line))
w.SafeRune(' ')
}
if e.tags != nil {
w.SafeString("[")
Expand All @@ -110,10 +113,10 @@ func (e *logEntry) SafeFormat(w interfaces.SafePrinter, _ rune) {
// that the format strings for `log.Infof` etc are const strings.
k := redact.SafeString(tag.Key())
v := tag.Value()
w.SafeString(k)
if v != nil {
w.Printf("%s=%v", k, tag.Value())
} else {
w.Printf("%s", k)
w.SafeRune('=')
w.Print(tag.Value())
}
}
w.SafeString("] ")
Expand Down

0 comments on commit 5cd6c77

Please sign in to comment.