Preformance!!!

This commit is contained in:
Bond_009 2019-12-22 22:21:41 +01:00
parent cf2e2a3f30
commit f9a454628d

View File

@ -15,7 +15,20 @@ namespace MediaBrowser.Common.Json.Converters
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ {
static void ThrowFormatException() => throw new FormatException("Invalid format for an integer."); static void ThrowFormatException() => throw new FormatException("Invalid format for an integer.");
ReadOnlySpan<byte> span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; ReadOnlySpan<byte> span = stackalloc byte[0];
if (reader.HasValueSequence)
{
long sequenceLength = reader.ValueSequence.Length;
Span<byte> stackSpan = stackalloc byte[(int)sequenceLength];
reader.ValueSequence.CopyTo(stackSpan);
span = stackSpan;
}
else
{
span = reader.ValueSpan;
}
if (!Utf8Parser.TryParse(span, out int number, out _)) if (!Utf8Parser.TryParse(span, out int number, out _))
{ {
ThrowFormatException(); ThrowFormatException();
@ -28,7 +41,7 @@ namespace MediaBrowser.Common.Json.Converters
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{ {
static void ThrowInvalidOperationException() => throw new InvalidOperationException(); static void ThrowInvalidOperationException() => throw new InvalidOperationException();
Span<byte> span = new byte[16]; Span<byte> span = stackalloc byte[16];
if (Utf8Formatter.TryFormat(value, span, out int bytesWritten)) if (Utf8Formatter.TryFormat(value, span, out int bytesWritten))
{ {
writer.WriteStringValue(span.Slice(0, bytesWritten)); writer.WriteStringValue(span.Slice(0, bytesWritten));