]> Cypherpunks repositories - gostls13.git/commit
Make printing faster by avoiding mallocs and some other advances.
authorRob Pike <r@golang.org>
Sun, 6 Dec 2009 20:03:52 +0000 (12:03 -0800)
committerRob Pike <r@golang.org>
Sun, 6 Dec 2009 20:03:52 +0000 (12:03 -0800)
commit4c0e51cd43cef26d066d35eb07fb7689e9fc50b6
tree4f3810b479b100741700368439a4f6f9c90d6375
parented6fd1bcbe06a69e9469fe59d92840a84b41c07b
Make printing faster by avoiding mallocs and some other advances.
Roughly 33% faster for simple cases, probably more for complex ones.

Before:

mallocs per Sprintf(""): 4
mallocs per Sprintf("xxx"): 6
mallocs per Sprintf("%x"): 10
mallocs per Sprintf("%x %x"): 12

Now:

mallocs per Sprintf(""): 2
mallocs per Sprintf("xxx"): 3
mallocs per Sprintf("%x"): 5
mallocs per Sprintf("%x %x"): 7

Speed improves because of avoiding mallocs and also by sharing a bytes.Buffer
between print.go and format.go rather than copying the data back after each
printed item.

Before:

fmt_test.BenchmarkSprintfEmpty 1000000       1346 ns/op
fmt_test.BenchmarkSprintfString 500000       3461 ns/op
fmt_test.BenchmarkSprintfInt 500000       3671 ns/op

Now:

fmt_test.BenchmarkSprintfEmpty  2000000        995 ns/op
fmt_test.BenchmarkSprintfString  1000000       2745 ns/op
fmt_test.BenchmarkSprintfInt  1000000       2391 ns/op
fmt_test.BenchmarkSprintfIntInt   500000       3751 ns/op

I believe there is more to get but this is a good milestone.

R=rsc
CC=golang-dev, hong
https://golang.org/cl/166076
src/pkg/bytes/buffer.go
src/pkg/fmt/fmt_test.go
src/pkg/fmt/format.go
src/pkg/fmt/print.go