]> Cypherpunks repositories - gostls13.git/commit
encoding/json: use pooled encoder in Encoder.Encode
authorJoe Tsai <joetsai@digital-static.net>
Fri, 30 Jan 2026 20:15:31 +0000 (12:15 -0800)
committerJoseph Tsai <joetsai@digital-static.net>
Mon, 2 Feb 2026 21:05:45 +0000 (13:05 -0800)
commit35abaf75c35adc9b22038885781b8be70a8476e0
tree6f0340cc0277f98012ed45410d6852cc6bcf7754
parent07f7f8ca5202bdcd39216e25b4a5b2c309ea727f
encoding/json: use pooled encoder in Encoder.Encode

Due to the lack of MarshalWrite in the v1 API,
it is unfortunately common to see:

    json.NewEncoder(out).Encode(in)

where a single-use Encoder is constructed and thrown away.
This performed acceptably in v1 since every call to Encode
used a globally pooled encoder resource.

Prior to this change, the v1-in-v2 implementation relied
on a bytes.Buffer cached only for the lifetime of the Encoder
object itself. Thus, a single-use Encoder does not benefit.
Modify the wrapper implementation to use the internal
pooled encoder from v2 and use the intermediate buffer
to write directly to the output io.Writer.

We assume that the user-provided io.Writer never leaks the buffer,
but this assumption was already held in the v1 implementation.
We are not increasing the surface area of data corruption risk.

Performance of v1 to v1-in-v2 (before the pool fix):

name                 old time/op    new time/op    delta
NewEncoderEncode-32    30.2ms ± 4%    28.3ms ± 9%    -6.19%  (p=0.002 n=9+10)

name                 old alloc/op   new alloc/op   delta
NewEncoderEncode-32    7.64MB ± 0%   28.37MB ± 0%  +271.23%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
NewEncoderEncode-32      200k ± 0%      100k ± 0%   -49.99%  (p=0.000 n=9+10)

Interestingly, v1-in-2 is slightly faster,
but the amount of allocated memory is massive.

Performance of v1 to v1-in-v2 (after the pool fix):

name                 old time/op    new time/op    delta
NewEncoderEncode-32    30.2ms ± 4%    24.0ms ± 7%  -20.36%  (p=0.000 n=9+10)

name                 old alloc/op   new alloc/op   delta
NewEncoderEncode-32    7.64MB ± 0%    4.09MB ± 3%  -46.52%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
NewEncoderEncode-32      200k ± 0%      100k ± 0%  -50.00%  (p=0.000 n=9+9)

Now, the v1-in-v2 implementation is better than v1 on all metrics.

Fixes #75026

Change-Id: I50c975b1d5b8da806e46bc627966b0a39c1817eb
Reviewed-on: https://go-review.googlesource.com/c/go/+/740660
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
src/encoding/json/bench_test.go
src/encoding/json/v2_bench_test.go
src/encoding/json/v2_stream.go