]> Cypherpunks repositories - gostls13.git/commit
encoding/json/jsontext: preserve buffer capacity in Decoder.Reset
authorFilip Petkovski <filip.petkovsky@gmail.com>
Mon, 23 Jun 2025 11:47:21 +0000 (11:47 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 24 Jul 2025 16:01:15 +0000 (09:01 -0700)
commit6536a9354728523f7ac3627f558ba3f51163e72c
tree58588b17878f5cbf3a050611ce23edebfe890e90
parentefc37e97c0f358f3cffe7ca2b78c743470345189
encoding/json/jsontext: preserve buffer capacity in Decoder.Reset

The Decoder.Reset method is not preserving the internal buffer between
resets, causing buffer capacity to be lost and resulting in unnecessary
allocations when reusing decoders. This is particularly problematic when
decoding many small messages.

This commit fixes the Reset method to preserve the internal buffer. It
makes sure aliasing is removed if the buffer currently points to an
internal byte slice of a bytes.Buffer. It adds a TestDecoderReset test
structured into subtests to better validate the different scenarios.

Change-Id: Ia685bff47034598224489173bb7f2ffd48e89da5
GitHub-Last-Rev: 462ddc936409ee3da6d75def7d9de59eb3c65f8d
GitHub-Pull-Request: golang/go#74120
Reviewed-on: https://go-review.googlesource.com/c/go/+/681177
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
src/encoding/json/jsontext/decode.go
src/encoding/json/jsontext/decode_test.go