]> Cypherpunks repositories - gostls13.git/commit
encoding/json/jsontext: avoid pinning application data in pools
authorJoe Tsai <joetsai@digital-static.net>
Tue, 21 Oct 2025 22:11:32 +0000 (15:11 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 24 Oct 2025 16:07:03 +0000 (09:07 -0700)
commitf5f69a3de9a3e0b8d4e211f155853a29cd400e92
treeefe2f4a8f076f29f5ab4a7c3e0ee4d19ac0cb0a0
parenta6a59f0762787fd50c7069b77b0addbc2339c8d2
encoding/json/jsontext: avoid pinning application data in pools

Previously, we put a jsontext.Encoder (or Decoder)
back into a pool with minimal reset logic.
This was semantically safe since we always did a full reset
after obtaining an Encoder/Decoder back out of the pool.

However, this meant that so long as an Encoder/Decoder was
alive in the pool, any application data referenced by the coder
would be kept alive longer than necessary.
Explicitly, clear such fields so that application data
can be more aggressively garbage collected.

Performance:

name               old time/op    new time/op    delta
Unmarshal/Bool-32    52.0ns ± 3%    50.3ns ± 3%  -3.30%  (p=0.001 n=10+10)
Marshal/Bool-32      55.4ns ± 3%    54.4ns ± 2%  -1.75%  (p=0.006 n=10+9)

This only impacts the performance of discrete Marshal/Unmarshal calls.
For the simplest possible call (i.e., to marsha/unmarshal a bool),
there is a 1-2ns slow down. This is an appropriate slowdown
for an improvement in memory utilization.

Change-Id: I5e7d7827473773e53a9dcb3d7fe9052a75481e9f
Reviewed-on: https://go-review.googlesource.com/c/go/+/713640
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
TryBot-Bypass: Damien Neil <dneil@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Auto-Submit: Damien Neil <dneil@google.com>
src/encoding/json/jsontext/pools.go
src/encoding/json/v2/arshal.go