Skip to content

GODRIVER-3533 Optimize value reader and writer #2022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion bson/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ type ValueUnmarshaler interface {
// When unmarshaling BSON, if the BSON value is null and the Go value is a
// pointer, the pointer is set to nil without calling UnmarshalBSONValue.
func Unmarshal(data []byte, val interface{}) error {
vr := newDocumentReader(bytes.NewReader(data))
vr := getDocumentReader(bytes.NewReader(data))
defer putDocumentReader(vr)

if l, err := vr.peekLength(); err != nil {
return err
} else if int(l) != len(data) {
Expand Down
64 changes: 60 additions & 4 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 All @@ -38,6 +53,33 @@ type valueReader struct {
frame int64
}

func getDocumentReader(r io.Reader) *valueReader {
vr := vrPool.Get().(*valueReader)

vr.offset = 0
vr.frame = 0

vr.stack = vr.stack[:1]
vr.stack[0] = vrState{mode: mTopLevel}

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

return vr
}

func putDocumentReader(vr *valueReader) {
if vr == nil {
return
}

Copy link
Preview

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.

Consider resetting the bufio.Reader (for example, by calling Reset(nil)) before returning it to the pool to clear its internal buffer and help prevent unintended memory retention.

Suggested change
vr.r.Reset(nil) // Clear the internal buffer to prevent memory retention.

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 is a dangerous change that could cause OOB errors, e.g:

panic: runtime error: slice bounds out of range [62:0]

bufioReaderPool.Put(vr.r)
vr.r = nil

vrPool.Put(vr)
}

// NewDocumentReader returns a ValueReader using b for the underlying BSON
// representation.
func NewDocumentReader(r io.Reader) ValueReader {
Expand Down Expand Up @@ -253,14 +295,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
Preview

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)
},
}

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