From 4baa486983204fdff64f80809627f79a538d8d82 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Fri, 9 Sep 2022 09:59:21 -0700 Subject: all: remove unnecessary allocations from w.WriteString(fmt.Sprint*(...)) by fmt.Fprint*(w, ...) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: hopehook Reviewed-by: Ian Lance Taylor Run-TryBot: Emmanuel Odeke TryBot-Result: Gopher Robot Reviewed-by: Daniel Martí --- src/runtime/trace/annotation_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/runtime/trace') 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() } -- cgit v1.3-5-g9baa