[CSHARP-2383] Provide overloads for WriteBytes/ReadBytes methods that allow buffer reuse Created: 12/Sep/18  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.7.0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Benjamin Piorczig Assignee: Unassigned
Resolution: Unresolved Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

I'd like to reuse one allocated buffer to read and one buffer to write to the mongodb. Currently one must use WriteBytes(byte[]) and byte[] ReadBytes() to read and write byte arrays. These arrays must be pre allocated with the correct size.

If you want to reuse a buffer it would be nice to get some overloads with a size restriction like:

 

void IBsonWriter.WriteBytes(byte[] bytes, int size);

This overload should only write size bytes to the document.

 

void IBsonWriter.ReadBytes(byte[] buffer, int offset, int size);

This overload should only fill the count of bytes defined by size starting at offset. 

 

This would reduce memory fragmentation in the program.

 



 Comments   
Comment by Benjamin Piorczig [ 27/Oct/18 ]

@ugurpelister: Thank you for pointing this out. This may be a solution in future but for now i need one that runs on .NET 4.6.x.

@jeff.yemin: You are right. I am currently using this in a custom serializer. I'd like to store a large amount of float values in a document with a timestamp. If i store all the values normalized as float[] things seem to get too slow when reading the data. I do convert these values to a byte array and write it to the document as binary data. The byte arrays i use are managed from a .NET BufferManager derivate.

@vincent.kam: Thanks for posting a link to my github pull request that implements this. Seem like i forgot to post that here.

Comment by Jeffrey Yemin [ 22/Oct/18 ]

Hi bpiorczig this seems like a reasonable request, but we'd like more information on the scenario in which you envision these new methods being used. In particular, the driver itself would not be able to make use of these methods when reading from or writing to MongoDB. It would have to be via a custom-written Serializer.

Comment by Vincent Kam (Inactive) [ 22/Oct/18 ]

https://github.com/mongodb/mongo-csharp-driver/pull/342

Comment by Uğur Pelister [ 21/Sep/18 ]

Hi. I just created the issue CSHARP-2391. If the new Span<T> gets implemented in the driver, then pre-allocated buffers can be reused while portions of these buffers can be read/written using Span<byte> in your case.

Generated at Wed Feb 07 21:42:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.