diff options
| author | Emmanuel T Odeke <emmanuel@orijtech.com> | 2022-09-09 09:59:21 -0700 |
|---|---|---|
| committer | Emmanuel Odeke <emmanuel@orijtech.com> | 2022-09-14 16:11:21 +0000 |
| commit | 4baa486983204fdff64f80809627f79a538d8d82 (patch) | |
| tree | f410368cd3fb61dcdd3ec725f06fef3aa2ef3496 /src/runtime | |
| parent | a7e9a379e970f0c48aae47da1778213f646b5f45 (diff) | |
| download | go-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/runtime')
| -rw-r--r-- | src/runtime/trace/annotation_test.go | 2 | ||||
| -rw-r--r-- | src/runtime/wincallback.go | 8 |
2 files changed, 5 insertions, 5 deletions
diff --git a/src/runtime/trace/annotation_test.go b/src/runtime/trace/annotation_test.go index 31fccef206..69ea8f2d3b 100644 --- a/src/runtime/trace/annotation_test.go +++ b/src/runtime/trace/annotation_test.go @@ -147,7 +147,7 @@ func TestUserTaskRegion(t *testing.T) { pretty := func(data []testData) string { var s strings.Builder for _, d := range data { - s.WriteString(fmt.Sprintf("\t%+v\n", d)) + fmt.Fprintf(&s, "\t%+v\n", d) } return s.String() } diff --git a/src/runtime/wincallback.go b/src/runtime/wincallback.go index 442a984708..9ec2027f97 100644 --- a/src/runtime/wincallback.go +++ b/src/runtime/wincallback.go @@ -62,7 +62,7 @@ func genasmArm() { TEXT runtime·callbackasm(SB),NOSPLIT|NOFRAME,$0 `) for i := 0; i < maxCallback; i++ { - buf.WriteString(fmt.Sprintf("\tMOVW\t$%d, R12\n", i)) + fmt.Fprintf(&buf, "\tMOVW\t$%d, R12\n", i) buf.WriteString("\tB\truntime·callbackasm1(SB)\n") } @@ -90,7 +90,7 @@ func genasmArm64() { TEXT runtime·callbackasm(SB),NOSPLIT|NOFRAME,$0 `) for i := 0; i < maxCallback; i++ { - buf.WriteString(fmt.Sprintf("\tMOVD\t$%d, R12\n", i)) + fmt.Fprintf(&buf, "\tMOVD\t$%d, R12\n", i) buf.WriteString("\tB\truntime·callbackasm1(SB)\n") } @@ -104,12 +104,12 @@ TEXT runtime·callbackasm(SB),NOSPLIT|NOFRAME,$0 func gengo() { var buf bytes.Buffer - buf.WriteString(fmt.Sprintf(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT. + fmt.Fprintf(&buf, `// Code generated by wincallback.go using 'go generate'. DO NOT EDIT. package runtime const cb_max = %d // maximum number of windows callbacks allowed -`, maxCallback)) +`, maxCallback) err := os.WriteFile("zcallback_windows.go", buf.Bytes(), 0666) if err != nil { fmt.Fprintf(os.Stderr, "wincallback: %s\n", err) |
