-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Reduce string allocations during SimpleJson.ParseString (#2977)
Noticed this allocation while looking at a profile of solution load in visual studio. These StringBuilder allocations were showing up as 0.5% of total allocations. I took a peek at the code, and reaslized the StringBuilder need not be created unless there is a '\' in the string value being parsed. In that case, just copy already seen characters into a StringBuilder and continue as previously. Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters