Commit 7da4cedd authored by Martin Möhrmann's avatar Martin Möhrmann Committed by Rob Pike

fmt: simplify buffer write methods and adjust calls to them

Once upon a time fmt did use bytes.Buffer for its buffer.
The buffer write methods still mimic the bytes.Buffer signatures.
The current code depends on manipulating the buffer []bytes array directly
which makes going back to bytes.Buffer by only changing the type of buffer
impossible. Since type buffer is not exported the methods can be simplified
to the needs of fmt. This saves space and avoids unnecessary overhead.

Use WriteString instead of Write for known inputs since
WriteString is faster than Write to append the same data.
This also saves space in the binary.

Remove the add method from Printer and depending on the data to be written
use WriteRune or WriteByte directly instead.

In total makes the go binary around 4 kilobyte smaller.

name                  old time/op  new time/op  delta
SprintfEmpty-2        24.1ns ± 3%  23.8ns ± 1%  -1.14%  (p=0.000 n=20+20)
SprintfString-2        114ns ± 2%   114ns ± 4%    ~     (p=0.558 n=20+19)
SprintfInt-2           116ns ± 9%   118ns ± 7%    ~     (p=0.086 n=20+20)
SprintfIntInt-2        195ns ± 6%   193ns ± 5%    ~     (p=0.345 n=20+19)
SprintfPrefixedInt-2   251ns ±16%   241ns ± 9%  -3.69%  (p=0.024 n=20+19)
SprintfFloat-2         203ns ± 4%   205ns ± 5%    ~     (p=0.153 n=20+20)
SprintfBoolean-2       101ns ± 7%    96ns ±11%  -5.23%  (p=0.005 n=19+20)
ManyArgs-2             651ns ± 7%   628ns ± 7%  -3.44%  (p=0.002 n=20+20)
FprintInt-2            164ns ± 2%   158ns ± 2%  -3.62%  (p=0.000 n=20+18)
FprintfBytes-2         215ns ± 1%   216ns ± 1%  +0.58%  (p=0.000 n=20+20)
FprintIntNoAlloc-2     115ns ± 0%   112ns ± 0%  -2.61%  (p=0.000 n=20+20)
ScanInts-2             700µs ± 0%   702µs ± 1%  +0.38%  (p=0.000 n=18+20)
ScanRecursiveInt-2    82.7ms ± 0%  82.7ms ± 0%    ~     (p=0.820 n=20+20)

Change-Id: I0409eb170b8a26d9f4eb271f6292e5d39faf2d8b
Reviewed-on: https://go-review.googlesource.com/19955Reviewed-by: default avatarRob Pike <r@golang.org>
parent 9d73a6dc
......@@ -943,6 +943,13 @@ func BenchmarkSprintfFloat(b *testing.B) {
}
})
}
func BenchmarkSprintfBoolean(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Sprintf("%t", true)
}
})
}
func BenchmarkManyArgs(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
......
......@@ -124,17 +124,12 @@ func (f *fmt) padString(s string) {
}
}
var (
trueBytes = []byte("true")
falseBytes = []byte("false")
)
// fmt_boolean formats a boolean.
func (f *fmt) fmt_boolean(v bool) {
if v {
f.pad(trueBytes)
f.padString("true")
} else {
f.pad(falseBytes)
f.padString("false")
}
}
......@@ -511,5 +506,5 @@ func (f *fmt) fmt_complex(r, j float64, size int, verb rune) {
f.space = oldSpace
f.plus = oldPlus
f.wid = oldWid
f.buf.Write(irparenBytes)
f.buf.WriteString("i)")
}
This diff is collapsed.
......@@ -839,7 +839,7 @@ func (s *ss) quotedString() string {
return string(s.buf)
case '"':
// Double-quoted: Include the quotes and let strconv.Unquote do the backslash escapes.
s.buf.WriteRune(quote)
s.buf.WriteByte('"')
for {
r := s.mustReadRune()
s.buf.WriteRune(r)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment