[CSHARP-2442] Increase StringBuilder capacity during instantiation Created: 27/Nov/18  Updated: 06/Jun/23

Status: Backlog
Project: C# Driver
Component/s: BSON, Performance
Affects Version/s: 2.7.2
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Dane Owens Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All


Issue Links:
Related
related to CSHARP-2456 Use less memory without scope capturing Backlog
Backwards Compatibility: Fully Compatible

 Description   

MongoDB.Bson/IO/JsonWriter.cs

Regarding the following function:

private string EscapedString(string value)

 

and the following line of code:

var sb = new StringBuilder(value.Length);

 

This StringBuilder object is being created because the string named "value" needs escaping. It is unknown at this point exactly how many characters need escaping, but AT MINIMUM there is one. This means the new escaped string length will be larger than original string length by at least one character, and the StringBuilder constructor capacity parameter should reflect this logic. Without this change, the StringBuilder object is GUARANTEED to allocate and chain together a new StringBuilder object under the hood as it increases it's capacity to handle the larger escaped string.



 Comments   
Comment by Robert Stam [ 03/Dec/18 ]

Pull request here:

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

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