From f9a454628d7204bc30d64f9c0589d766ad5f3109 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Sun, 22 Dec 2019 22:21:41 +0100 Subject: [PATCH] Preformance!!! --- .../Json/Converters/JsonInt32Converter.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs b/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs index 0fd68babe..fe5dd6cd4 100644 --- a/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs +++ b/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs @@ -15,7 +15,20 @@ namespace MediaBrowser.Common.Json.Converters public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { static void ThrowFormatException() => throw new FormatException("Invalid format for an integer."); - ReadOnlySpan span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; + ReadOnlySpan span = stackalloc byte[0]; + + if (reader.HasValueSequence) + { + long sequenceLength = reader.ValueSequence.Length; + Span stackSpan = stackalloc byte[(int)sequenceLength]; + reader.ValueSequence.CopyTo(stackSpan); + span = stackSpan; + } + else + { + span = reader.ValueSpan; + } + if (!Utf8Parser.TryParse(span, out int number, out _)) { ThrowFormatException(); @@ -28,7 +41,7 @@ namespace MediaBrowser.Common.Json.Converters public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options) { static void ThrowInvalidOperationException() => throw new InvalidOperationException(); - Span span = new byte[16]; + Span span = stackalloc byte[16]; if (Utf8Formatter.TryFormat(value, span, out int bytesWritten)) { writer.WriteStringValue(span.Slice(0, bytesWritten));