Skip to content

Commit 53bc194

Browse files
committed
fmt: speed up 10-20%
The check for Stringer etc. can only fire if the test is not a builtin, so avoid the expensive check if we know there's no chance. Also put in a fast path for pad, which saves a more modest amount. benchmark old ns/op new ns/op delta BenchmarkSprintfEmpty 148 152 +2.70% BenchmarkSprintfString 585 497 -15.04% BenchmarkSprintfInt 441 396 -10.20% BenchmarkSprintfIntInt 718 603 -16.02% BenchmarkSprintfPrefixedInt 676 621 -8.14% BenchmarkSprintfFloat 1003 953 -4.99% BenchmarkManyArgs 2945 2312 -21.49% BenchmarkScanInts 1704152 1734441 +1.78% BenchmarkScanRecursiveInt 1837397 1828920 -0.46% R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/6245068
1 parent d61707f commit 53bc194

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/pkg/fmt/fmt_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,14 @@ func BenchmarkSprintfFloat(b *testing.B) {
527527
}
528528
}
529529

530+
func BenchmarkManyArgs(b *testing.B) {
531+
var buf bytes.Buffer
532+
for i := 0; i < b.N; i++ {
533+
buf.Reset()
534+
Fprintf(&buf, "%2d/%2d/%2d %d:%d:%d %s %s\n", 3, 4, 5, 11, 12, 13, "hello", "world")
535+
}
536+
}
537+
530538
var mallocBuf bytes.Buffer
531539

532540
var mallocTest = []struct {

src/pkg/fmt/format.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ func (f *fmt) writePadding(n int, padding []byte) {
110110
// Append b to f.buf, padded on left (w > 0) or right (w < 0 or f.minus)
111111
// clear flags afterwards.
112112
func (f *fmt) pad(b []byte) {
113-
var padding []byte
114-
var left, right int
115-
if f.widPresent && f.wid != 0 {
116-
padding, left, right = f.computePadding(len(b))
113+
if !f.widPresent || f.wid == 0 {
114+
f.buf.Write(b)
115+
return
117116
}
117+
padding, left, right := f.computePadding(len(b))
118118
if left > 0 {
119119
f.writePadding(left, padding)
120120
}
@@ -127,11 +127,11 @@ func (f *fmt) pad(b []byte) {
127127
// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus).
128128
// clear flags afterwards.
129129
func (f *fmt) padString(s string) {
130-
var padding []byte
131-
var left, right int
132-
if f.widPresent && f.wid != 0 {
133-
padding, left, right = f.computePadding(utf8.RuneCountInString(s))
130+
if !f.widPresent || f.wid == 0 {
131+
f.buf.WriteString(s)
132+
return
134133
}
134+
padding, left, right := f.computePadding(utf8.RuneCountInString(s))
135135
if left > 0 {
136136
f.writePadding(left, padding)
137137
}

src/pkg/fmt/print.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,10 +734,6 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth
734734
return false
735735
}
736736

737-
if wasString, handled := p.handleMethods(verb, plus, goSyntax, depth); handled {
738-
return wasString
739-
}
740-
741737
// Some types can be done without reflection.
742738
switch f := field.(type) {
743739
case bool:
@@ -779,6 +775,10 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth
779775
p.fmtBytes(f, verb, goSyntax, depth)
780776
wasString = verb == 's'
781777
default:
778+
// If the type is not simple, it might have methods.
779+
if wasString, handled := p.handleMethods(verb, plus, goSyntax, depth); handled {
780+
return wasString
781+
}
782782
// Need to use reflection
783783
return p.printReflectValue(reflect.ValueOf(field), verb, plus, goSyntax, depth)
784784
}

0 commit comments

Comments
 (0)