From 8ff03eda80839189e2ac1df99b89106246c4fd39 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 17 Jul 2023 17:06:41 -0400 Subject: [PATCH] Dedup OnWriteResume between {ReadOnly}Memory converters --- .../Converters/Collection/MemoryConverter.cs | 36 +------------------ .../Collection/ReadOnlyMemoryConverter.cs | 13 ++++--- 2 files changed, 9 insertions(+), 40 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/MemoryConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/MemoryConverter.cs index 853d8a3159cd0..55f145e535479 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/MemoryConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/MemoryConverter.cs @@ -27,41 +27,7 @@ protected override void ConvertCollection(ref ReadStack state, JsonSerializerOpt protected override bool OnWriteResume(Utf8JsonWriter writer, Memory value, JsonSerializerOptions options, ref WriteStack state) { - int index = state.Current.EnumeratorIndex; - - JsonConverter elementConverter = GetElementConverter(ref state); - ReadOnlySpan valueSpan = value.Span; - - if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null) - { - // Fast path that avoids validation and extra indirection. - for (; index < valueSpan.Length; index++) - { - elementConverter.Write(writer, valueSpan[index], options); - } - } - else - { - for (; index < value.Length; index++) - { - T element = valueSpan[index]; - if (!elementConverter.TryWrite(writer, element, options, ref state)) - { - state.Current.EnumeratorIndex = index; - return false; - } - - state.Current.EndCollectionElement(); - - if (ShouldFlush(writer, ref state)) - { - state.Current.EnumeratorIndex = ++index; - return false; - } - } - } - - return true; + return ReadOnlyMemoryConverter.OnWriteResume(writer, value.Span, options, ref state); } } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ReadOnlyMemoryConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ReadOnlyMemoryConverter.cs index 6d5a39e750490..b8778c7736baa 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ReadOnlyMemoryConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ReadOnlyMemoryConverter.cs @@ -26,26 +26,29 @@ protected override void ConvertCollection(ref ReadStack state, JsonSerializerOpt } protected override bool OnWriteResume(Utf8JsonWriter writer, ReadOnlyMemory value, JsonSerializerOptions options, ref WriteStack state) + { + return OnWriteResume(writer, value.Span, options, ref state); + } + + internal static bool OnWriteResume(Utf8JsonWriter writer, ReadOnlySpan value, JsonSerializerOptions options, ref WriteStack state) { int index = state.Current.EnumeratorIndex; JsonConverter elementConverter = GetElementConverter(ref state); - ReadOnlySpan valueSpan = value.Span; if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null) { // Fast path that avoids validation and extra indirection. - for (; index < valueSpan.Length; index++) + for (; index < value.Length; index++) { - elementConverter.Write(writer, valueSpan[index], options); + elementConverter.Write(writer, value[index], options); } } else { for (; index < value.Length; index++) { - T element = valueSpan[index]; - if (!elementConverter.TryWrite(writer, element, options, ref state)) + if (!elementConverter.TryWrite(writer, value[index], options, ref state)) { state.Current.EnumeratorIndex = index; return false;