From 91f4a6405df74ab5e68a3c219e52ce8b5fd30efc Mon Sep 17 00:00:00 2001 From: Rob Figueiredo Date: Fri, 17 Jan 2020 21:57:59 -0500 Subject: [PATCH] (*Stream).Flush: reset buffer to beginning Previously it would append to the end of the buffer instead of reusing the now-free space. Benchmark demonstrates the improvement, run with -benchtime=10s benchmark old ns/op new ns/op delta Benchmark_encode_string_with_SetEscapeHTML-8 447 442 -1.12% Benchmark_jsoniter_large_file-8 20998 21222 +1.07% Benchmark_json_large_file-8 39593 40187 +1.50% Benchmark_stream_encode_big_object-8 10787 8611 -20.17% benchmark old allocs new allocs delta Benchmark_encode_string_with_SetEscapeHTML-8 6 6 +0.00% Benchmark_jsoniter_large_file-8 78 78 +0.00% Benchmark_json_large_file-8 13 13 +0.00% Benchmark_stream_encode_big_object-8 31 0 -100.00% benchmark old bytes new bytes delta Benchmark_encode_string_with_SetEscapeHTML-8 760 760 +0.00% Benchmark_jsoniter_large_file-8 4920 4920 +0.00% Benchmark_json_large_file-8 6640 6640 +0.00% Benchmark_stream_encode_big_object-8 10056 0 -100.00% Fixes #438 --- stream.go | 4 ++-- stream_test.go | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/stream.go b/stream.go index 17662fd..de2c13e 100644 --- a/stream.go +++ b/stream.go @@ -103,14 +103,14 @@ func (stream *Stream) Flush() error { if stream.Error != nil { return stream.Error } - n, err := stream.out.Write(stream.buf) + _, err := stream.out.Write(stream.buf) if err != nil { if stream.Error == nil { stream.Error = err } return err } - stream.buf = stream.buf[n:] + stream.buf = stream.buf[:0] return nil } diff --git a/stream_test.go b/stream_test.go index d407c7a..4100f22 100644 --- a/stream_test.go +++ b/stream_test.go @@ -1,8 +1,9 @@ package jsoniter import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func Test_writeByte_should_grow_buffer(t *testing.T) { @@ -65,5 +66,10 @@ func Test_flush_buffer_should_stop_grow_buffer(t *testing.T) { writer := new(NopWriter) NewEncoder(writer).Encode(make([]int, 10000000)) should := require.New(t) - should.Equal(8, writer.bufferSize) + + // 512 is the internal buffer size set in NewEncoder + // + // Flush is called after each array element, so only the first 8 bytes of it + // is ever used, and it is never extended. Capacity remains 512. + should.Equal(512, writer.bufferSize) }