From 9a4e78c7365cb2d5f71b7cb08f0a21bd11074b9c Mon Sep 17 00:00:00 2001 From: Achilles1515 Date: Tue, 27 Nov 2018 12:01:41 -0500 Subject: [PATCH] Increase StringBuilder capacity 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 instantiation 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. --- src/MongoDB.Bson/IO/JsonWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MongoDB.Bson/IO/JsonWriter.cs b/src/MongoDB.Bson/IO/JsonWriter.cs index dc253e8e368..62fd71d8208 100644 --- a/src/MongoDB.Bson/IO/JsonWriter.cs +++ b/src/MongoDB.Bson/IO/JsonWriter.cs @@ -710,7 +710,7 @@ private string EscapedString(string value) return value; } - var sb = new StringBuilder(value.Length); + var sb = new StringBuilder(value.Length + 1); foreach (char c in value) {