From ebdbfccd989b07a8aef75af5fbe7448f035ee239 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 24 Jun 2025 18:56:26 -0700 Subject: encoding/json/jsontext: preserve buffer capacity in Encoder.Reset This does the equivalent of CL 681177 for the Encoder. It preserves the internal buffer between resets. Change-Id: I5e9353b6d7755e067d4f9a4d1ea3d8f056253027 Reviewed-on: https://go-review.googlesource.com/c/go/+/690375 Reviewed-by: Johan Brandhorst-Satzkorn Auto-Submit: Joseph Tsai LUCI-TryBot-Result: Go LUCI Reviewed-by: Damien Neil Reviewed-by: Michael Knyszek --- src/encoding/json/jsontext/decode.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/encoding/json/jsontext/decode.go') diff --git a/src/encoding/json/jsontext/decode.go b/src/encoding/json/jsontext/decode.go index b9f247bff4..f505de4468 100644 --- a/src/encoding/json/jsontext/decode.go +++ b/src/encoding/json/jsontext/decode.go @@ -138,12 +138,10 @@ func (d *Decoder) Reset(r io.Reader, opts ...Options) { case d.s.Flags.Get(jsonflags.WithinArshalCall): panic("jsontext: cannot reset Decoder passed to json.UnmarshalerFrom") } - // If the decoder was previously aliasing a bytes.Buffer, - // invalidate the alias to avoid writing into the bytes.Buffer's - // internal buffer. + // Reuse the buffer if it does not alias a previous [bytes.Buffer]. b := d.s.buf[:0] if _, ok := d.s.rd.(*bytes.Buffer); ok { - b = nil // avoid reusing b since it aliases the previous bytes.Buffer. + b = nil } d.s.reset(b, r, opts...) } -- cgit v1.3