Skip to content

Commit

Permalink
fix: StreamEncoder didn't recycle full buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jul 2, 2024
1 parent f56fe93 commit 0d9d7e2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions internal/encoder/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -84,6 +84,6 @@ func (enc *StreamEncoder) Encode(val interface{}) (err error) {
}

free_bytes:
freeBytes(buf)
freeBytes(out)
return err
}
6 changes: 3 additions & 3 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -93,5 +93,5 @@ func WithCompileMaxInlineDepth(depth int) CompileOption {

// CanSizeResue
func CanSizeResue(cap int) bool {
return cap <= int(LimitDecoderBufferSize)
return cap <= int(LimitBufferSize)
}

0 comments on commit 0d9d7e2

Please sign in to comment.