Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2442

Increase StringBuilder capacity during instantiation

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.25.0
    • Affects Version/s: 2.7.2
    • Component/s: BSON, Performance
    • None
    • Environment:
      All
    • Fully Compatible
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            Achilles1515 Dane Owens
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: