From: Brad Fitzpatrick Date: Thu, 15 Dec 2011 19:21:21 +0000 (-0800) Subject: json: use strconv.Append variants to avoid allocations in encoding X-Git-Tag: weekly.2011-12-22~174 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=29264c6f4f341586b733e2c5b165ca627b6050d8;p=gostls13.git json: use strconv.Append variants to avoid allocations in encoding Before/after, best of 3: json.BenchmarkCodeEncoder 10 183495300 ns/op 10.58 MB/s -> json.BenchmarkCodeEncoder 10 133025100 ns/op 14.59 MB/s But don't get too excited about this. These benchmarks, while stable at any point of time, fluctuate wildly with any line of code added or removed anywhere in the path due to stack splitting issues. It's currently much faster, though, and this is the API that doesn't allocate so should always be faster in theory. R=golang-dev, dsymonds, rsc, r, gri CC=golang-dev https://golang.org/cl/5411052 --- diff --git a/src/pkg/encoding/json/encode.go b/src/pkg/encoding/json/encode.go index ff8e80c091..3d2f4fc316 100644 --- a/src/pkg/encoding/json/encode.go +++ b/src/pkg/encoding/json/encode.go @@ -197,6 +197,7 @@ var hex = "0123456789abcdef" // An encodeState encodes JSON into a bytes.Buffer. type encodeState struct { bytes.Buffer // accumulated output + scratch [64]byte } func (e *encodeState) marshal(v interface{}) (err error) { @@ -275,14 +276,26 @@ func (e *encodeState) reflectValueQuoted(v reflect.Value, quoted bool) { } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - writeString(e, strconv.FormatInt(v.Int(), 10)) - + b := strconv.AppendInt(e.scratch[:0], v.Int(), 10) + if quoted { + writeString(e, string(b)) + } else { + e.Write(b) + } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - writeString(e, strconv.FormatUint(v.Uint(), 10)) - + b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10) + if quoted { + writeString(e, string(b)) + } else { + e.Write(b) + } case reflect.Float32, reflect.Float64: - writeString(e, strconv.FormatFloat(v.Float(), 'g', -1, v.Type().Bits())) - + b := strconv.AppendFloat(e.scratch[:0], v.Float(), 'g', -1, v.Type().Bits()) + if quoted { + writeString(e, string(b)) + } else { + e.Write(b) + } case reflect.String: if quoted { sb, err := Marshal(v.String())