]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: use sync.Pool
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 18 Dec 2013 23:52:05 +0000 (15:52 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 18 Dec 2013 23:52:05 +0000 (15:52 -0800)
Benchmark is within the noise. I had to run this a dozen times
each before & after (on wall power, without a browser running)
before I could get halfway consistent numbers, and even then
they jumped all over the place, with the new one sometimes
being better. But these are the best of a dozen each.

Slowdown is expected anyway, since I imagine channels are
optimized more.

benchmark                 old ns/op    new ns/op    delta
BenchmarkCodeEncoder       26556987     27291072   +2.76%
BenchmarkEncoderEncode         1069         1071   +0.19%

benchmark                  old MB/s     new MB/s  speedup
BenchmarkCodeEncoder          73.07        71.10    0.97x

benchmark                old allocs   new allocs    delta
BenchmarkEncoderEncode            2            2    0.00%

benchmark                 old bytes    new bytes    delta
BenchmarkEncoderEncode          221          221    0.00%

Update #4720

R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/37720047

src/pkg/encoding/json/encode.go
src/pkg/encoding/json/stream.go

index 8c71770ca4624dd60c96fb44121b8647e287d4fc..4a77ba1cd256e1f47709979c06325d64ddca8b88 100644 (file)
@@ -241,24 +241,15 @@ type encodeState struct {
        scratch      [64]byte
 }
 
-// TODO(bradfitz): use a sync.Cache here
-var encodeStatePool = make(chan *encodeState, 8)
+var encodeStatePool sync.Pool
 
 func newEncodeState() *encodeState {
-       select {
-       case e := <-encodeStatePool:
+       if v := encodeStatePool.Get(); v != nil {
+               e := v.(*encodeState)
                e.Reset()
                return e
-       default:
-               return new(encodeState)
-       }
-}
-
-func putEncodeState(e *encodeState) {
-       select {
-       case encodeStatePool <- e:
-       default:
        }
+       return new(encodeState)
 }
 
 func (e *encodeState) marshal(v interface{}) (err error) {
index 1928abadb7d4ae40613ef8b8b46000d734d932c2..e8d6bd4c2ce00a4f4ed7bbb1e7dc647b4db66b9e 100644 (file)
@@ -173,7 +173,7 @@ func (enc *Encoder) Encode(v interface{}) error {
        if _, err = enc.w.Write(e.Bytes()); err != nil {
                enc.err = err
        }
-       putEncodeState(e)
+       encodeStatePool.Put(e)
        return err
 }