forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#37976 from steveharter/TechEmPower
Perf improvements mostly for small or value-type POCOs
- Loading branch information
Showing
49 changed files
with
401 additions
and
190 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
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
98 changes: 98 additions & 0 deletions
98
src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Escaping.cs
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,98 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Buffers; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Text.Encodings.Web; | ||
|
||
namespace System.Text.Json | ||
{ | ||
internal static partial class JsonHelpers | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static byte[] GetEscapedPropertyNameSection(ReadOnlySpan<byte> utf8Value, JavaScriptEncoder? encoder) | ||
{ | ||
int idx = JsonWriterHelper.NeedsEscaping(utf8Value, encoder); | ||
|
||
if (idx != -1) | ||
{ | ||
return GetEscapedPropertyNameSection(utf8Value, idx, encoder); | ||
} | ||
else | ||
{ | ||
return GetPropertyNameSection(utf8Value); | ||
} | ||
} | ||
|
||
public static byte[] EscapeValue( | ||
ReadOnlySpan<byte> utf8Value, | ||
int firstEscapeIndexVal, | ||
JavaScriptEncoder? encoder) | ||
{ | ||
Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length); | ||
Debug.Assert(firstEscapeIndexVal >= 0 && firstEscapeIndexVal < utf8Value.Length); | ||
|
||
byte[]? valueArray = null; | ||
|
||
int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); | ||
|
||
Span<byte> escapedValue = length <= JsonConstants.StackallocThreshold ? | ||
stackalloc byte[length] : | ||
(valueArray = ArrayPool<byte>.Shared.Rent(length)); | ||
|
||
JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written); | ||
|
||
byte[] escapedString = escapedValue.Slice(0, written).ToArray(); | ||
|
||
if (valueArray != null) | ||
{ | ||
ArrayPool<byte>.Shared.Return(valueArray); | ||
} | ||
|
||
return escapedString; | ||
} | ||
|
||
private static byte[] GetEscapedPropertyNameSection( | ||
ReadOnlySpan<byte> utf8Value, | ||
int firstEscapeIndexVal, | ||
JavaScriptEncoder? encoder) | ||
{ | ||
Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length); | ||
Debug.Assert(firstEscapeIndexVal >= 0 && firstEscapeIndexVal < utf8Value.Length); | ||
|
||
byte[]? valueArray = null; | ||
|
||
int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); | ||
|
||
Span<byte> escapedValue = length <= JsonConstants.StackallocThreshold ? | ||
stackalloc byte[length] : | ||
(valueArray = ArrayPool<byte>.Shared.Rent(length)); | ||
|
||
JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written); | ||
|
||
byte[] propertySection = GetPropertyNameSection(escapedValue.Slice(0, written)); | ||
|
||
if (valueArray != null) | ||
{ | ||
ArrayPool<byte>.Shared.Return(valueArray); | ||
} | ||
|
||
return propertySection; | ||
} | ||
|
||
private static byte[] GetPropertyNameSection(ReadOnlySpan<byte> utf8Value) | ||
{ | ||
int length = utf8Value.Length; | ||
byte[] propertySection = new byte[length + 3]; | ||
|
||
propertySection[0] = JsonConstants.Quote; | ||
utf8Value.CopyTo(propertySection.AsSpan(1, length)); | ||
propertySection[++length] = JsonConstants.Quote; | ||
propertySection[++length] = JsonConstants.KeyValueSeperator; | ||
|
||
return propertySection; | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.