Skip to content

Commit

Permalink
Added an extension point to bson reader to allow optimization of stri…
Browse files Browse the repository at this point in the history
…ng creation
  • Loading branch information
solyutor committed Jun 24, 2016
1 parent c4cea92 commit a035fae
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Src/Newtonsoft.Json/Bson/BsonReader.cs
Expand Up @@ -604,7 +604,7 @@ private string ReadString()
int length = Encoding.UTF8.GetChars(_byteBuffer, 0, byteCount, _charBuffer, 0);

MovePosition(totalBytesRead + 1);
return new string(_charBuffer, 0, length);
return GetString(_charBuffer, length);
}
else
{
Expand Down Expand Up @@ -692,7 +692,7 @@ private string GetString(int length)
// pref optimization to avoid reading into a string builder
// first iteration and all bytes read then return string directly
int charCount = Encoding.UTF8.GetChars(_byteBuffer, 0, byteCount, _charBuffer, 0);
return new string(_charBuffer, 0, charCount);
return GetString(_charBuffer, charCount);
}
else
{
Expand Down Expand Up @@ -722,6 +722,14 @@ private string GetString(int length)
return builder.ToString();
}

/// <summary>
/// An extension point that allows optimization of string instantiation. For instance, take it from a cache.
/// </summary>
protected virtual string GetString(char[] buffer, int charCount)
{
return new string(buffer, 0, charCount);
}

private int GetLastFullCharStop(int start)
{
int lookbackPos = start;
Expand Down

0 comments on commit a035fae

Please sign in to comment.