json-iterator/go

Why does stream.Write appends to stream.buf if stream.out is not nil?

KromDaniel opened this issue · 0 comments

Hey,
Thanks for writing this amazing library, I'm having a small thought about some allocation.

Stream.Write appends to stream.buf even if stream.out is not nil, making it allocate (sometimes) extra slice even if it is not needed.

this is the Write method:

func (stream *Stream) Write(p []byte) (nn int, err error) {
	stream.buf = append(stream.buf, p...)
	if stream.out != nil {
		nn, err = stream.out.Write(stream.buf)
		stream.buf = stream.buf[nn:]
		return
	}
	return len(p), nil
}

I do see that methods like WriteByte does only append, I guess it is to save overhead of small writings to io.Writer but in that case user can simply compose some chunkedStream (but will need to flush also outside stream)

jsoniter.NewStream(SomeChunkedStreamThatFlushesEveryXBytes(theActualWriter))

also, sometimes the writer is bytes.Buffer and in that case small writes are fine.

Another option is maybe to add config KeepBuffer with default true to keep current behavior and not break anything and let the user decide, If you think this is good idea I'd gladly PR it.

Would like to hear your opinion, thanks a lot