]> Cypherpunks repositories - gostls13.git/commitdiff
json: use strconv.Append variants to avoid allocations in encoding
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 15 Dec 2011 19:21:21 +0000 (11:21 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 15 Dec 2011 19:21:21 +0000 (11:21 -0800)
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

src/pkg/encoding/json/encode.go

index ff8e80c091ed0876e24a99b179417ac71eea558c..3d2f4fc316ea8e837995399d637851d3832ac7c3 100644 (file)
@@ -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())