Skip to content

Commit

Permalink
all: remove unnecessary allocations from w.WriteString(fmt.Sprint*(..…
Browse files Browse the repository at this point in the history
….)) 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>
  • Loading branch information
odeke-em committed Sep 14, 2022
1 parent a7e9a37 commit 4baa486
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/abt/avlint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/ssagen/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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>")
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/compile/internal/syntax/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/types/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/exprstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func WriteExpr(buf *bytes.Buffer, x ast.Expr) {

switch x := x.(type) {
default:
buf.WriteString(fmt.Sprintf("(ast: %T)", x)) // nil, ast.BadExpr, ast.KeyValueExpr
fmt.Fprintf(buf, "(ast: %T)", x) // nil, ast.BadExpr, ast.KeyValueExpr

case *ast.Ident:
buf.WriteString(x.Name)
Expand Down
2 changes: 1 addition & 1 deletion src/internal/fmtsort/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func sprint(data any) string {
}
b.WriteString(sprintKey(key))
b.WriteRune(':')
b.WriteString(fmt.Sprint(om.Value[i]))
fmt.Fprint(b, om.Value[i])
}
return b.String()
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/trace/annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/wincallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
}

Expand All @@ -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)
Expand Down

0 comments on commit 4baa486

Please sign in to comment.