From f3e786e61122ed23dd7f1996024ec75afc8cf551 Mon Sep 17 00:00:00 2001 From: Filip Petkovski Date: Tue, 10 Jun 2025 10:09:52 +0200 Subject: [PATCH] jsontext: Preserve the internal buffer between Reset calls The jsontext decoder throws away the internal buffer on each Reset call. This makes decoding one message at a time pretty wasteful since each new message will cause at least one allocation for the buffer. This commit makes sure the buffer gets reused when the decoder is reset with a new reader. If unbounded growth for the slice is a concern, I can add an option limiting the maxiumum size after which the bytes buffer will be thrown away and a new one can be allocated. --- jsontext/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsontext/decode.go b/jsontext/decode.go index e8441dc..d4c1b25 100644 --- a/jsontext/decode.go +++ b/jsontext/decode.go @@ -136,7 +136,7 @@ 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") } - d.s.reset(nil, r, opts...) + d.s.reset(d.s.buf[:0], r, opts...) } func (d *decoderState) reset(b []byte, r io.Reader, opts ...Options) {