From 0d9d7e26d486f54c41750fab9d14e0a1d5f736f4 Mon Sep 17 00:00:00 2001 From: "duanyi.aster" Date: Tue, 2 Jul 2024 16:39:25 +0800 Subject: [PATCH] fix: `StreamEncoder` didn't recycle full buffer --- internal/encoder/stream.go | 12 ++++++------ option/option.go | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/encoder/stream.go b/internal/encoder/stream.go index d498f68fc..0368c06f8 100644 --- a/internal/encoder/stream.go +++ b/internal/encoder/stream.go @@ -36,8 +36,7 @@ func NewStreamEncoder(w io.Writer) *StreamEncoder { // Encode encodes interface{} as JSON to io.Writer func (enc *StreamEncoder) Encode(val interface{}) (err error) { - buf := newBytes() - out := buf + out := newBytes() /* encode into the buffer */ err = EncodeInto(&out, val, enc.Opts) @@ -69,9 +68,10 @@ func (enc *StreamEncoder) Encode(val interface{}) (err error) { } else { /* copy into io.Writer */ var n int - for len(out) > 0 { - n, err = enc.w.Write(out) - out = out[n:] + buf := out + for len(buf) > 0 { + n, err = enc.w.Write(buf) + buf = buf[n:] if err != nil { goto free_bytes } @@ -84,6 +84,6 @@ func (enc *StreamEncoder) Encode(val interface{}) (err error) { } free_bytes: - freeBytes(buf) + freeBytes(out) return err } diff --git a/option/option.go b/option/option.go index 70e00feac..35414ac25 100644 --- a/option/option.go +++ b/option/option.go @@ -26,9 +26,9 @@ var ( // DefaultAstBufferSize is the initial buffer size of ast.Node.MarshalJSON() DefaultAstBufferSize uint = 4 * 1024 - // LimitDecoderBufferSize indicates the max pool buffer size, in case of OOM. + // LimitBufferSize indicates the max pool buffer size, in case of OOM. // See issue https://github.com/bytedance/sonic/issues/614 - LimitDecoderBufferSize uint = 1024 * 1024 + LimitBufferSize uint = 1024 * 1024 ) // CompileOptions includes all options for encoder or decoder compiler. @@ -93,5 +93,5 @@ func WithCompileMaxInlineDepth(depth int) CompileOption { // CanSizeResue func CanSizeResue(cap int) bool { - return cap <= int(LimitDecoderBufferSize) + return cap <= int(LimitBufferSize) }