summaryrefslogtreecommitdiff
path: root/lib/textui/log.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-05 19:41:12 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-25 17:28:49 -0700
commit9c5d61133af93ddf279e355a06d4c1ae78c568b3 (patch)
tree6bacb35630ce0044dc5d841c03d97d036930dd4e /lib/textui/log.go
parent21fc68264ae07da11ed90d1f44b9a24225c799dc (diff)
textui: Avoid allocating strings when logging
Diffstat (limited to 'lib/textui/log.go')
-rw-r--r--lib/textui/log.go35
1 files changed, 24 insertions, 11 deletions
diff --git a/lib/textui/log.go b/lib/textui/log.go
index 41c874f..605755d 100644
--- a/lib/textui/log.go
+++ b/lib/textui/log.go
@@ -18,7 +18,6 @@ import (
"path/filepath"
"runtime"
"sort"
- "strconv"
"strings"
"sync"
"time"
@@ -348,35 +347,49 @@ func fieldOrd(key string) int {
}
func writeField(w io.Writer, key string, val any) {
- valStr := printer.Sprint(val)
+ valBuf, _ := logBufPool.Get()
+ defer func() {
+ // The wrapper `func()` is important to defer
+ // evaluating `valBuf`, since we might re-assign it
+ // below.
+ valBuf.Reset()
+ logBufPool.Put(valBuf)
+ }()
+ _, _ = printer.Fprint(valBuf, val)
needsQuote := false
- if strings.HasPrefix(valStr, `"`) {
+ if bytes.HasPrefix(valBuf.Bytes(), []byte(`"`)) {
needsQuote = true
} else {
- for _, r := range valStr {
- if !(unicode.IsPrint(r) && r != ' ') {
+ for _, r := range valBuf.Bytes() {
+ if !(unicode.IsPrint(rune(r)) && r != ' ') {
needsQuote = true
break
}
}
}
if needsQuote {
- valStr = strconv.Quote(valStr)
+ valBuf2, _ := logBufPool.Get()
+ fmt.Fprintf(valBuf2, "%q", valBuf.Bytes())
+ valBuf.Reset()
+ logBufPool.Put(valBuf)
+ valBuf = valBuf2
}
+ valStr := valBuf.Bytes()
name := key
switch {
case name == "THREAD":
name = "thread"
- switch valStr {
- case "", "/main":
+ switch {
+ case len(valStr) == 0 || bytes.Equal(valStr, []byte("/main")):
return
default:
- if strings.HasPrefix(valStr, "/main/") {
- valStr = strings.TrimPrefix(valStr, "/main")
+ if bytes.HasPrefix(valStr, []byte("/main/")) {
+ valStr = valStr[len("/main/"):]
+ } else if bytes.HasPrefix(valStr, []byte("/")) {
+ valStr = valStr[len("/"):]
}
- valStr = strings.TrimPrefix(valStr, "/")
}
case strings.HasSuffix(name, ".pass"):
fmt.Fprintf(w, "/pass-%s", valStr)