[GODRIVER-2025] Always reuse buffers in bsonrw.valueWriter Created: 25/May/21  Updated: 25/Jul/22  Resolved: 14/Jun/21

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 1.6.0

Type: Improvement Priority: Unknown
Reporter: Matt Dale Assignee: Matt Dale
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File memprofile-marshal-bson.out    
Issue Links:
Related
related to GODRIVER-1670 Use buffer pooling and copying return... Closed
Epic Link: Reduce Allocations
Quarter: FY23Q2, FY23Q3

 Description   

Currently, a bsonrw.valueWriter will use a new byte slice as the write buffer for each document written if one is passed in as the writer (if it is a SliceWriter). The result is that the write buffer capacity must be grown many times, each resulting in an allocate-then-copy operation. Instead, we should reuse the write buffer for every document written, only allocating a new byte slice when flushing the full document from the buffer (or when the write buffer needs to grow).

In the attached memory profile, observe that >75% of the allocations are in the bsoncore.Append* functions which append to a byte slice buffer provided by bsonrw.valueWriter. We should expect very few allocations in the bsoncore.Append* functions once the buffer is grown the first time.



 Comments   
Comment by Githook User [ 14/Jun/21 ]

Author:

{'name': 'Matt Dale', 'email': '9760375+matthewdale@users.noreply.github.com', 'username': 'matthewdale'}

Message: GODRIVER-2025 Stop replacing the bsonrw.valueWriter buffer when given a SliceWriter destination. (#679)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/4adb6d73ca0fe53c99e11765005f37033acd6471

Comment by Matt Dale [ 27/May/21 ]

PR: https://github.com/mongodb/mongo-go-driver/pull/679

Generated at Thu Feb 08 08:37:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.