[GODRIVER-1670] Use buffer pooling and copying return values to reduce memory allocation Created: 02/Jul/20  Updated: 28/Oct/23  Resolved: 08/Sep/22

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

Type: Improvement Priority: Major - P3
Reporter: Rachelle Palmer Assignee: Qingyang Hu
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File cursor_fetch.go    
Issue Links:
Problem/Incident
causes GODRIVER-2677 Investigate excess memory usage that ... Closed
Related
related to GODRIVER-1624 Investigate report of excessive memor... Backlog
is related to GODRIVER-2026 Reduce allocations in bsonrw.valueRea... Backlog
is related to GODRIVER-2021 Operation.createMsgWireMessage() allo... Closed
is related to GODRIVER-2025 Always reuse buffers in bsonrw.valueW... Closed
Epic Link: Reduce Allocations
Quarter: FY23Q2, FY23Q3
Case:

 Description   

Currently new byte slices are created every time Operation.Execute is called. We can reduce this by pooling the byte slices in the background and copying return values, which are usually subslices, into new slices before returning them, which will free up the original slice.



 Comments   
Comment by Qingyang Hu [ 08/Sep/22 ]

Fixed in GODRIVER-2021

Comment by Isabella Siu (Inactive) [ 05/Mar/21 ]

After some investigation, to expand on Jeff's comment above, which is likely to be be the best way to handle this, Operation.Execute() currently takes in a scratch parameter, ostensibly for setting up pooling in a ticket like this, but because of the way that scratch slice gets passed through the function and into the results means that more would need to change to enable this.
The BatchCursor and build_Result functions seem like the most logical place to make the copies, to reduce unnecessary copying and maintain correct ownership.
While sync.Pool could be used to pool both of these, note that byte slices should be presized before being stored and that performance should be checked to make sure that there's actually an improvement.

Comment by Jeffrey Yemin [ 07/Jul/20 ]

Consider using this ticket as a springboard for:

  • buffer pooling to reduce GC pressure
  • copying from the buffer for any slices returned to application, to avoid holding on to a larger slice by a smaller sub-slicehttps://jira.mongodb.org/browse/GODRIVER-1670#
Generated at Thu Feb 08 08:36:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.