Skip to content

Commit

Permalink
small performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Mar 21, 2023
1 parent 76b95c8 commit 9d9ffed
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.0.0
- JsonPatchDocument types now adds required converters using JsonConverterAttribute and converters no longer need to be set manually to JsonSerializerOptions https://github.com/Havunen/SystemTextJsonPatch/issues/18
- netstandard2.0 target now depends on System.Text.Json v7
- Small performance optimizations

## 1.2.0
- Better support for patching arrays by index https://github.com/Havunen/SystemTextJsonPatch/issues/15
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ This test deserializes a JSON patch document of 8 operations and applies the cha

See [SystemTextJsonPatch.Benchmark](https://github.com/Havunen/SystemTextJsonPatch/tree/main/SystemTextJsonPatch.Benchmark) for more details.

BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22000.978/21H2)
BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.22621.1413/22H2/2022Update/SunValley2)
AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK=7.0.100-rc.1.22431.12
[Host] : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2
Job-URORCR : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2
.NET SDK=7.0.300-preview.23122.5
[Host] : .NET 7.0.3 (7.0.323.6910), X64 RyuJIT AVX2
Job-STVRTF : .NET 7.0.3 (7.0.323.6910), X64 RyuJIT AVX2

WarmupCount=2

| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|-------------------- |-----------:|----------:|----------:|-------:|-------:|----------:|
| SystemTextJsonPatch | 7.233 us | 0.0381 us | 0.0356 us | 0.3738 | - | 6.16 KB |
| MarvinJsonPatch | 979.525 us | 9.9310 us | 8.8036 us | 5.8594 | 3.9063 | 98.13 KB |
| AspNetCoreJsonPatch | 26.645 us | 0.2023 us | 0.1892 us | 2.5940 | 0.0610 | 42.49 KB |
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|-------------------- |-----------:|-----------:|-----------:|-------:|-------:|----------:|
| SystemTextJsonPatch | 6.579 us | 0.0537 us | 0.0476 us | 0.3433 | - | 5.69 KB |
| MarvinJsonPatch | 913.023 us | 14.2077 us | 16.9133 us | 5.8594 | 3.9063 | 96.02 KB |
| AspNetCoreJsonPatch | 24.543 us | 0.2470 us | 0.2310 us | 2.6550 | 0.0610 | 43.61 KB |
25 changes: 19 additions & 6 deletions SystemTextJsonPatch/Internal/ConversionResultProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,33 @@ private static bool TryConvertDecimalToNumber(decimal decimalValue, Type typeToC

private static object? Deserialize(object? value, Type typeToConvertTo, JsonSerializerOptions options)
{
if (typeToConvertTo == typeof(JsonNode))
if (typeToConvertTo == typeof(JsonElement))
{
return JsonSerializer.SerializeToElement(value, options);
}
if (typeToConvertTo == typeof(JsonNode))
{
return JsonSerializer.SerializeToNode(value, options);
}
if (typeToConvertTo == typeof(JsonDocument))
{
return JsonSerializer.SerializeToDocument(value, options);
}
if (typeToConvertTo == typeof(JsonElement))
{
return JsonSerializer.SerializeToElement(value, options);
}

return JsonSerializer.Deserialize(JsonSerializer.Serialize(value, options), typeToConvertTo, options);
if (value is JsonElement jsonEl)
{
return JsonSerializer.Deserialize(jsonEl, typeToConvertTo, options);
}
if (value is JsonNode jsonNode)
{
return JsonSerializer.Deserialize(jsonNode, typeToConvertTo, options);
}
if (value is JsonDocument jsonDoc)
{
return JsonSerializer.Deserialize(jsonDoc, typeToConvertTo, options);
}

return JsonSerializer.Deserialize(JsonSerializer.SerializeToUtf8Bytes(value, options), typeToConvertTo, options);
}

private static bool IsNullableType(Type type)
Expand Down
15 changes: 9 additions & 6 deletions SystemTextJsonPatch/Internal/ParsedPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,30 @@ private static string[] ParsePath(string path)

for (var i = 0; i < path.Length; i++)
{
if (path[i] == '/')
var c = path[i];

if (c == '/')
{
if (sb.Length > 0)
{
strings.Add(sb.ToString());
sb.Length = 0;
}
}
else if (path[i] == '~')
else if (c == '~')
{
++i;
if (i >= path.Length)
c = path[i];
if (i >= path.Length)
{
throw new JsonPatchException(Resources.FormatInvalidValueForPath(path), null);
}

if (path[i] == '0')
if (c == '0')
{
sb.Append('~');
}
else if (path[i] == '1')
else if (c == '1')
{
sb.Append('/');
}
Expand All @@ -77,7 +80,7 @@ private static string[] ParsePath(string path)
}
else
{
sb.Append(path[i]);
sb.Append(c);
}
}

Expand Down

0 comments on commit 9d9ffed

Please sign in to comment.