diff options
| author | Jeremy Jackins <jeremyjackins@gmail.com> | 2017-12-19 15:43:30 -0800 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2018-04-13 17:31:52 +0000 |
| commit | c0547476f342665514904cf2581a62135d2366c3 (patch) | |
| tree | cb7cf4d0ed63f05937f14128e1bb5efbb1d69391 /src/encoding/json | |
| parent | 98dfd400e6418c04a8dcb93fb90a006dd0bcf5d9 (diff) | |
| download | go-c0547476f342665514904cf2581a62135d2366c3.tar.xz | |
encoding/json: make use of encodeStatePool in Marshal
On my system, this seems to be a significant win, with a major
reduction in allocations and minor speed improvement.
name old time/op new time/op delta
CodeMarshal 9.75ms ± 3% 9.24ms ± 1% -5.21% (p=0.001 n=5+10)
CodeMarshal-4 4.98ms ± 1% 4.71ms ± 1% -5.44% (p=0.001 n=5+10)
CodeMarshal-8 4.80ms ± 0% 4.77ms ± 1% -0.70% (p=0.012 n=5+9)
name old speed new speed delta
CodeMarshal 199MB/s ± 3% 210MB/s ± 1% +5.46% (p=0.001 n=5+10)
CodeMarshal-4 390MB/s ± 1% 412MB/s ± 1% +5.76% (p=0.001 n=5+10)
CodeMarshal-8 404MB/s ± 0% 407MB/s ± 1% +0.70% (p=0.012 n=5+9)
name old alloc/op new alloc/op delta
CodeMarshal 4.59MB ± 0% 1.96MB ± 0% -57.22% (p=0.000 n=5+9)
CodeMarshal-4 4.59MB ± 0% 2.00MB ± 0% -56.39% (p=0.000 n=5+8)
CodeMarshal-8 4.59MB ± 0% 2.06MB ± 0% -55.05% (p=0.001 n=5+9)
name old allocs/op new allocs/op delta
CodeMarshal 16.0 ± 0% 1.0 ± 0% -93.75% (p=0.000 n=5+10)
CodeMarshal-4 16.0 ± 0% 1.0 ± 0% -93.75% (p=0.000 n=5+10)
CodeMarshal-8 16.0 ± 0% 1.0 ± 0% -93.75% (p=0.000 n=5+10)
Change-Id: I9d09850d8227f523f861ae1b4ca248c4a4b16aaf
Reviewed-on: https://go-review.googlesource.com/84897
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/json')
| -rw-r--r-- | src/encoding/json/encode.go | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/encoding/json/encode.go b/src/encoding/json/encode.go index 46aa78a70b..99407e0f7a 100644 --- a/src/encoding/json/encode.go +++ b/src/encoding/json/encode.go @@ -155,12 +155,18 @@ import ( // an infinite recursion. // func Marshal(v interface{}) ([]byte, error) { - e := &encodeState{} + e := newEncodeState() + err := e.marshal(v, encOpts{escapeHTML: true}) if err != nil { return nil, err } - return e.Bytes(), nil + buf := append([]byte(nil), e.Bytes()...) + + e.Reset() + encodeStatePool.Put(e) + + return buf, nil } // MarshalIndent is like Marshal but applies Indent to format the output. |
