Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce string allocations during SimpleJson.ParseString #2977

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Octokit.Tests/SimpleJsonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Octokit;
using System.Threading.Tasks;
using Xunit;

public class SimpleJsonTests
{
[Theory]
[InlineData("\"abc\"", "abc")]
[InlineData(" \"abc\" ", "abc")]
[InlineData("\" abc \" ", " abc ")]
[InlineData("\"abc\\\"def\"", "abc\"def")]
[InlineData("\"abc\\r\\ndef\"", "abc\r\ndef")]
public async Task ParseStringSuccess(string input, string expected)
{
int index = 0;
bool success = true;

string actual = SimpleJson.ParseString(input.ToCharArray(), ref index, ref success);

Assert.True(success);
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("\"abc")]
public async Task ParseStringIncomplete(string input)
{
int index = 0;
bool success = true;

string actual = SimpleJson.ParseString(input.ToCharArray(), ref index, ref success);

Assert.False(success);
Assert.Null(actual);
}
}
25 changes: 21 additions & 4 deletions Octokit/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,15 +792,18 @@ static object ParseValue(char[] json, ref int index, ref bool success)
return null;
}

static string ParseString(char[] json, ref int index, ref bool success)
internal static string ParseString(char[] json, ref int index, ref bool success)
{
StringBuilder s = new StringBuilder(BUILDER_CAPACITY);
// Avoid allocating this StringBuilder unless a backslash is encountered in the json
StringBuilder s = null;
char c;

EatWhitespace(json, ref index);

// "
c = json[index++];

int startIndex = index;
bool complete = false;
while (!complete)
{
Expand All @@ -815,6 +818,13 @@ static string ParseString(char[] json, ref int index, ref bool success)
}
else if (c == '\\')
{
if (s == null)
{
s = new StringBuilder(BUILDER_CAPACITY);
for (int i = startIndex; i < index - 1; i++)
s.Append(json[i]);
}

if (index == json.Length)
break;
c = json[index++];
Expand Down Expand Up @@ -875,14 +885,21 @@ static string ParseString(char[] json, ref int index, ref bool success)
}
}
else
s.Append(c);
{
if (s != null)
s.Append(c);
}
}
if (!complete)
{
success = false;
return null;
}
return s.ToString();

if (s != null)
return s.ToString();

return new string(json, startIndex, index - startIndex - 1);
}

private static string ConvertFromUtf32(int utf32)
Expand Down
Loading