Skip to content

Commit

Permalink
Merge pull request #73 from tdakkota/refactor/set-buffer-size
Browse files Browse the repository at this point in the history
refactor: handle `bufSize` properly
  • Loading branch information
tdakkota committed Feb 2, 2023
2 parents 9a31093 + 79d79ec commit d16d313
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/x.yml
Expand Up @@ -15,6 +15,8 @@ jobs:
uses: go-faster/x/.github/workflows/cover.yml@main
lint:
uses: go-faster/x/.github/workflows/lint.yml@main
with:
golangci-lint: v1.50.1
commit:
uses: go-faster/x/.github/workflows/commit.yml@main
nancy:
Expand Down
2 changes: 1 addition & 1 deletion enc_bench_test.go
Expand Up @@ -130,7 +130,7 @@ func BenchmarkEncodeFloats(b *testing.B) {
b.Run("Stream", func(b *testing.B) {
b.SetBytes(size)
b.RunParallel(func(pb *testing.PB) {
enc := NewStreamingEncoder(io.Discard, 512)
enc := NewStreamingEncoder(io.Discard, -1)
for pb.Next() {
enc.ResetWriter(io.Discard)
encodeFloats(enc, arr)
Expand Down
7 changes: 5 additions & 2 deletions enc_stream.go
Expand Up @@ -3,14 +3,17 @@ package jx
import "io"

const (
minEncoderBufSize = 32
encoderBufSize = 512
minEncoderBufSize = 32
)

// NewStreamingEncoder creates new streaming encoder.
func NewStreamingEncoder(w io.Writer, bufSize int) *Encoder {
if bufSize < minEncoderBufSize {
switch {
case bufSize < 0:
bufSize = encoderBufSize
case bufSize < minEncoderBufSize:
bufSize = minEncoderBufSize
}
return &Encoder{
w: Writer{
Expand Down
18 changes: 13 additions & 5 deletions enc_stream_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/go-faster/errors"
Expand All @@ -13,7 +14,7 @@ import (
func TestEncoderStreamingCheck(t *testing.T) {
a := require.New(t)

e := NewStreamingEncoder(io.Discard, 512)
e := NewStreamingEncoder(io.Discard, -1)

_, err := e.Write([]byte("hello"))
a.ErrorIs(err, errStreaming)
Expand Down Expand Up @@ -51,9 +52,9 @@ func TestEncoder_Close(t *testing.T) {
})
t.Run("WriteErr", func(t *testing.T) {
ew := &errWriter{err: errTest}
e := NewStreamingEncoder(ew, 32)
e := NewStreamingEncoder(ew, minEncoderBufSize)
e.Obj(func(e *Encoder) {
e.FieldStart(strings.Repeat("a", 32))
e.FieldStart(strings.Repeat("a", minEncoderBufSize))
e.Null()
})

Expand Down Expand Up @@ -83,7 +84,7 @@ func TestEncoder_Close(t *testing.T) {
func TestEncoder_ResetWriter(t *testing.T) {
do := func(e *Encoder) {
e.ObjStart()
e.FieldStart(strings.Repeat("a", 32))
e.FieldStart(strings.Repeat("a", minEncoderBufSize))
e.Null()
e.ObjEnd()

Expand All @@ -104,7 +105,7 @@ func TestEncoder_ResetWriter(t *testing.T) {

// This benchmark is used to measure the overhead of ignoring errors.
func BenchmarkSkipError(b *testing.B) {
e := NewStreamingEncoder(io.Discard, 32)
e := NewStreamingEncoder(io.Discard, minEncoderBufSize)
e.w.stream.setError(errors.New("test"))

b.ResetTimer()
Expand All @@ -114,3 +115,10 @@ func BenchmarkSkipError(b *testing.B) {
encodeObject(e)
}
}

func TestStreamingEncoderBufferSize(t *testing.T) {
e := NewStreamingEncoder(io.Discard, -1)
assert.Equal(t, encoderBufSize, cap(e.w.Buf))
e = NewStreamingEncoder(io.Discard, minEncoderBufSize-1)
assert.Equal(t, minEncoderBufSize, cap(e.w.Buf))
}

0 comments on commit d16d313

Please sign in to comment.