[CSHARP-2693] Performance: Reduce byte buffer allocations while writing to server Created: 15/Aug/19  Updated: 28/Oct/23  Resolved: 01/Mar/21

Status: Closed
Project: C# Driver
Component/s: Performance, Serialization
Affects Version/s: 2.8.1, 2.9.0
Fix Version/s: 2.12.0

Type: Improvement Priority: Major - P3
Reporter: Daniel Hegener Assignee: Boris Dogadov
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Similar to https://jira.mongodb.org/browse/CSHARP-2692, calling Utf8Encoding.GetBytes without specifying a byte buffer should be avoided at all costs for best performance.

The current implementation of ByteBufferStream does already have a concept (using the "_tempUtf8" buffer) where it ends up calling the fast GetBytes() overload that doesn't allocate its own buffer for strings up to a length that fits into a byte[128] buffer. The 128 elements are a hard limit, though, so for bigger strings, there is a performance penalty.

I suggest changing this by auto-growing the byte[128] buffer to handle bigger strings, too. This is trading a little bit of memory for performance.



 Comments   
Comment by Githook User [ 01/Mar/21 ]

Author:

{'name': 'Boris', 'email': 'boris.dogadov@mongodb.com', 'username': 'BorisDog'}

Message: CSHARP-2693/CSHARP-2692: Encoding.GetBytes buffer caching optimization
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/f0bece570fdd3c8ede73981adaa89a225caa978c

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