Skip to content

Commit 4adb6d7

Browse files
authored
GODRIVER-2025 Stop replacing the bsonrw.valueWriter buffer when given a SliceWriter destination. (#679)
1 parent 49af6e2 commit 4adb6d7

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

bson/bsonrw/value_writer.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ func NewBSONValueWriterPool() *BSONValueWriterPool {
4747
// Get retrieves a BSON ValueWriter from the pool and resets it to use w as the destination.
4848
func (bvwp *BSONValueWriterPool) Get(w io.Writer) ValueWriter {
4949
vw := bvwp.pool.Get().(*valueWriter)
50-
if writer, ok := w.(*SliceWriter); ok {
51-
vw.reset(*writer)
52-
vw.w = writer
53-
return vw
54-
}
50+
51+
// TODO: Having to call reset here with the same buffer doesn't really make sense.
52+
vw.reset(vw.buf)
5553
vw.buf = vw.buf[:0]
5654
vw.w = w
5755
return vw
@@ -72,11 +70,6 @@ func (bvwp *BSONValueWriterPool) Put(vw ValueWriter) (ok bool) {
7270
return false
7371
}
7472

75-
if _, ok := bvw.w.(*SliceWriter); ok {
76-
bvw.buf = nil
77-
}
78-
bvw.w = nil
79-
8073
bvwp.pool.Put(bvw)
8174
return true
8275
}
@@ -549,10 +542,6 @@ func (vw *valueWriter) Flush() error {
549542
return nil
550543
}
551544

552-
if sw, ok := vw.w.(*SliceWriter); ok {
553-
*sw = vw.buf
554-
return nil
555-
}
556545
if _, err := vw.w.Write(vw.buf); err != nil {
557546
return err
558547
}

bson/marshal.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ func MarshalAppend(dst []byte, val interface{}) ([]byte, error) {
5252
// MarshalWithRegistry returns the BSON encoding of val as a BSON document. If val is not a type that can be transformed
5353
// into a document, MarshalValueWithRegistry should be used instead.
5454
func MarshalWithRegistry(r *bsoncodec.Registry, val interface{}) ([]byte, error) {
55-
dst := make([]byte, 0, 256) // TODO: make the default cap a constant
55+
dst := make([]byte, 0)
5656
return MarshalAppendWithRegistry(r, dst, val)
5757
}
5858

5959
// MarshalWithContext returns the BSON encoding of val as a BSON document using EncodeContext ec. If val is not a type
6060
// that can be transformed into a document, MarshalValueWithContext should be used instead.
6161
func MarshalWithContext(ec bsoncodec.EncodeContext, val interface{}) ([]byte, error) {
62-
dst := make([]byte, 0, 256) // TODO: make the default cap a constant
62+
dst := make([]byte, 0)
6363
return MarshalAppendWithContext(ec, dst, val)
6464
}
6565

@@ -115,13 +115,13 @@ func MarshalValueAppend(dst []byte, val interface{}) (bsontype.Type, []byte, err
115115

116116
// MarshalValueWithRegistry returns the BSON encoding of val using Registry r.
117117
func MarshalValueWithRegistry(r *bsoncodec.Registry, val interface{}) (bsontype.Type, []byte, error) {
118-
dst := make([]byte, 0, defaultDstCap)
118+
dst := make([]byte, 0)
119119
return MarshalValueAppendWithRegistry(r, dst, val)
120120
}
121121

122122
// MarshalValueWithContext returns the BSON encoding of val using EncodeContext ec.
123123
func MarshalValueWithContext(ec bsoncodec.EncodeContext, val interface{}) (bsontype.Type, []byte, error) {
124-
dst := make([]byte, 0, defaultDstCap)
124+
dst := make([]byte, 0)
125125
return MarshalValueAppendWithContext(ec, dst, val)
126126
}
127127

0 commit comments

Comments
 (0)