Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dedup OnWriteResume between {ReadOnly}Memory converters #89049

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,7 @@ protected override void ConvertCollection(ref ReadStack state, JsonSerializerOpt

protected override bool OnWriteResume(Utf8JsonWriter writer, Memory<T> value, JsonSerializerOptions options, ref WriteStack state)
{
int index = state.Current.EnumeratorIndex;

JsonConverter<T> elementConverter = GetElementConverter(ref state);
ReadOnlySpan<T> 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<T>.OnWriteResume(writer, value.Span, options, ref state);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,29 @@ protected override void ConvertCollection(ref ReadStack state, JsonSerializerOpt
}

protected override bool OnWriteResume(Utf8JsonWriter writer, ReadOnlyMemory<T> value, JsonSerializerOptions options, ref WriteStack state)
{
return OnWriteResume(writer, value.Span, options, ref state);
}

internal static bool OnWriteResume(Utf8JsonWriter writer, ReadOnlySpan<T> value, JsonSerializerOptions options, ref WriteStack state)
{
int index = state.Current.EnumeratorIndex;

JsonConverter<T> elementConverter = GetElementConverter(ref state);
ReadOnlySpan<T> 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;
Expand Down
Loading