aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorEmmanuel T Odeke <emmanuel@orijtech.com>2022-09-09 09:59:21 -0700
committerEmmanuel Odeke <emmanuel@orijtech.com>2022-09-14 16:11:21 +0000
commit4baa486983204fdff64f80809627f79a538d8d82 (patch)
treef410368cd3fb61dcdd3ec725f06fef3aa2ef3496 /src/cmd
parenta7e9a379e970f0c48aae47da1778213f646b5f45 (diff)
downloadgo-4baa486983204fdff64f80809627f79a538d8d82.tar.xz
all: remove unnecessary allocations from w.WriteString(fmt.Sprint*(...)) by fmt.Fprint*(w, ...)
Noticed in a manual audit from a customer codebase that the pattern w.WriteString(fmt.Sprint*(args...)) was less efficient and in most cases we can just invoke: fmt.Fprint*(w, args...) and from the simple benchmarks we can see quick wins in all dimensions: $ benchstat before.txt after.txt name old time/op new time/op delta DetailString-8 5.48µs ±23% 4.40µs ±11% -19.79% (p=0.000 n=20+17) name old alloc/op new alloc/op delta DetailString-8 2.63kB ± 0% 2.11kB ± 0% -19.76% (p=0.000 n=20+20) name old allocs/op new allocs/op delta DetailString-8 63.0 ± 0% 50.0 ± 0% -20.63% (p=0.000 n=20+20) Change-Id: I47a2827cd34d6b92644900b1bd5f4c0a3287bdb1 Reviewed-on: https://go-review.googlesource.com/c/go/+/429861 Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: hopehook <hopehook@golangcn.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/compile/internal/abt/avlint32.go2
-rw-r--r--src/cmd/compile/internal/ssagen/ssa.go2
-rw-r--r--src/cmd/compile/internal/syntax/scanner_test.go8
-rw-r--r--src/cmd/compile/internal/types/fmt.go2
4 files changed, 7 insertions, 7 deletions
diff --git a/src/cmd/compile/internal/abt/avlint32.go b/src/cmd/compile/internal/abt/avlint32.go
index eed5fa5d38..e0998f5bac 100644
--- a/src/cmd/compile/internal/abt/avlint32.go
+++ b/src/cmd/compile/internal/abt/avlint32.go
@@ -339,7 +339,7 @@ func (t *T) String() string {
}
b.WriteString(strconv.FormatInt(int64(k), 10))
b.WriteString(":")
- b.WriteString(fmt.Sprint(v))
+ fmt.Fprint(&b, v)
}
return b.String()
}
diff --git a/src/cmd/compile/internal/ssagen/ssa.go b/src/cmd/compile/internal/ssagen/ssa.go
index 21b8fbc8ec..88d43b9915 100644
--- a/src/cmd/compile/internal/ssagen/ssa.go
+++ b/src/cmd/compile/internal/ssagen/ssa.go
@@ -7271,7 +7271,7 @@ func genssa(f *ssa.Func, pp *objw.Progs) {
}
buf.WriteString("</dt>")
buf.WriteString("<dd class=\"ssa-prog\">")
- buf.WriteString(fmt.Sprintf("%.5d <span class=\"l%v line-number\">(%s)</span> %s", p.Pc, p.InnermostLineNumber(), p.InnermostLineNumberHTML(), html.EscapeString(p.InstructionString())))
+ fmt.Fprintf(&buf, "%.5d <span class=\"l%v line-number\">(%s)</span> %s", p.Pc, p.InnermostLineNumber(), p.InnermostLineNumberHTML(), html.EscapeString(p.InstructionString()))
buf.WriteString("</dd>")
}
buf.WriteString("</dl>")
diff --git a/src/cmd/compile/internal/syntax/scanner_test.go b/src/cmd/compile/internal/syntax/scanner_test.go
index 2deb3bbf84..450ec1ff8a 100644
--- a/src/cmd/compile/internal/syntax/scanner_test.go
+++ b/src/cmd/compile/internal/syntax/scanner_test.go
@@ -88,10 +88,10 @@ func TestEmbeddedTokens(t *testing.T) {
// make source
var buf bytes.Buffer
for i, s := range sampleTokens {
- buf.WriteString("\t\t\t\t"[:i&3]) // leading indentation
- buf.WriteString(s.src) // token
- buf.WriteString(" "[:i&7]) // trailing spaces
- buf.WriteString(fmt.Sprintf("/*line foo:%d */ // bar\n", i)) // comments + newline (don't crash w/o directive handler)
+ buf.WriteString("\t\t\t\t"[:i&3]) // leading indentation
+ buf.WriteString(s.src) // token
+ buf.WriteString(" "[:i&7]) // trailing spaces
+ fmt.Fprintf(&buf, "/*line foo:%d */ // bar\n", i) // comments + newline (don't crash w/o directive handler)
}
// scan source
diff --git a/src/cmd/compile/internal/types/fmt.go b/src/cmd/compile/internal/types/fmt.go
index c6e99d26c2..990f2e5f52 100644
--- a/src/cmd/compile/internal/types/fmt.go
+++ b/src/cmd/compile/internal/types/fmt.go
@@ -577,7 +577,7 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type
} else {
b.WriteString("tp")
// Print out the pointer value for now to disambiguate type params
- b.WriteString(fmt.Sprintf("%p", t))
+ fmt.Fprintf(b, "%p", t)
}
case TUNION: