Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ func Marshal(val interface{}) ([]byte, error) {
}
}()
sw.Reset()
vw := NewDocumentWriter(sw)

vw := getDocumentWriter(sw)
defer putDocumentWriter(vw)

enc := encPool.Get().(*Encoder)
defer encPool.Put(enc)
enc.Reset(vw)
Expand Down
2 changes: 2 additions & 0 deletions bson/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type ValueUnmarshaler interface {
// pointer, the pointer is set to nil without calling UnmarshalBSONValue.
func Unmarshal(data []byte, val interface{}) error {
vr := newDocumentReader(bytes.NewReader(data))
defer releaseDocumentReader(vr)

if l, err := vr.peekLength(); err != nil {
return err
} else if int(l) != len(data) {
Expand Down
65 changes: 53 additions & 12 deletions bson/value_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"io"
"math"
"sync"
)

var _ ValueReader = &valueReader{}
Expand All @@ -29,6 +30,20 @@ type vrState struct {
end int64
}

var bufioReaderPool = sync.Pool{
New: func() interface{} {
return bufio.NewReader(nil)
},
}

var vrPool = sync.Pool{
New: func() interface{} {
return &valueReader{
stack: make([]vrState, 1, 5),
}
},
}

// valueReader is for reading BSON values.
type valueReader struct {
r *bufio.Reader
Expand Down Expand Up @@ -57,14 +72,26 @@ func newValueReader(t Type, r io.Reader) ValueReader {
}

func newDocumentReader(r io.Reader) *valueReader {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newDocumentReader() is also called where releaseDocumentReader() is not called. It seems we will have to reverse newDocumentReader() and add a reset() for valueReader like what we did for valueWriter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

stack := make([]vrState, 1, 5)
stack[0] = vrState{
mode: mTopLevel,
}
return &valueReader{
r: bufio.NewReader(r),
stack: stack,
}
vr := vrPool.Get().(*valueReader)

vr.offset = 0
vr.frame = 0

vr.stack = vr.stack[:1]
vr.stack[0].mode = mTopLevel

br := bufioReaderPool.Get().(*bufio.Reader)
br.Reset(r)
vr.r = br

return vr
}

func releaseDocumentReader(vr *valueReader) {
bufioReaderPool.Put(vr.r)
vr.r = nil

vrPool.Put(vr)
}

func (vr *valueReader) advanceFrame() {
Expand Down Expand Up @@ -253,14 +280,28 @@ func (vr *valueReader) appendNextElement(dst []byte) ([]byte, error) {
return nil, err
}

buf := make([]byte, length)
_, err = io.ReadFull(vr.r, buf)
buf, err := vr.r.Peek(int(length))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Peek only allocates once for the bufio's internal buffer, we can borrow views of it with Peek without touching the heap. And append makes the copy.

if err != nil {
if err == bufio.ErrBufferFull {
temp := make([]byte, length)
if _, err = io.ReadFull(vr.r, temp); err != nil {
return nil, err
}
dst = append(dst, temp...)
vr.offset += int64(len(temp))
return dst, nil
}

Comment on lines +301 to +309
Copy link

Copilot AI Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider refactoring the error handling for bufio.ErrBufferFull in appendNextElement to streamline the code and reduce duplication in the fallback path.

Suggested change
temp := make([]byte, length)
if _, err = io.ReadFull(vr.r, temp); err != nil {
return nil, err
}
dst = append(dst, temp...)
vr.offset += int64(len(temp))
return dst, nil
}
return vr.readAndAppend(length, dst)
}

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion is a hallucination., vr.readAndAppend() is not part of the valueReader API.

return nil, err
}

dst = append(dst, buf...)
vr.offset += int64(len(buf))
return dst, err
if _, err = vr.r.Discard(int(length)); err != nil {
return nil, err
}

vr.offset += int64(length)
return dst, nil
}

func (vr *valueReader) readValueBytes(dst []byte) (Type, []byte, error) {
Expand Down
23 changes: 23 additions & 0 deletions bson/value_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ func putValueWriter(vw *valueWriter) {
}
}

var documentWriterPool = sync.Pool{
New: func() interface{} {
return NewDocumentWriter(nil)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call newDocumentWriter() because it returns a *valueWriter for syntactic clarity?

},
}

func getDocumentWriter(w io.Writer) *valueWriter {
vw := documentWriterPool.Get().(*valueWriter)

vw.reset(vw.buf)
vw.buf = vw.buf[:0]
vw.w = w

return vw
}

func putDocumentWriter(vw *valueWriter) {
if vw != nil {
vw.w = nil // don't leak the writer
documentWriterPool.Put(vw)
}
}

// This is here so that during testing we can change it and not require
// allocating a 4GB slice.
var maxSize = math.MaxInt32
Expand Down
Loading