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

System.Text.Json: .NET 5.0 perf regression serializing Nullable<T> #46788

Merged
merged 2 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -70,5 +70,7 @@ internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, T? v
_converter.WriteNumberWithCustomHandling(writer, value.Value, handling);
}
}

internal override bool IsNull(T? value) => !value.HasValue;
Copy link
Member

@steveharter steveharter Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having to do this seems a bit strange. I'll do some local prototyping to see why is going on here -- boxing issues with Nullable<T> or otherwise. If there's an issue with Nullable<T> then we should try to fix it in the runtime.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ internal override sealed bool TryReadAsObject(ref Utf8JsonReader reader, JsonSer
return success;
}

internal virtual bool IsNull(T value) => value == null;

internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions options, ref WriteStack state)
{
if (writer.CurrentDepth >= options.EffectiveMaxDepth)
Expand Down Expand Up @@ -342,7 +344,7 @@ internal bool TryWrite(Utf8JsonWriter writer, in T value, JsonSerializerOptions
}
}
}
else if (value == null && !HandleNullOnWrite)
else if (IsNull(value) && !HandleNullOnWrite)
{
// We do not pass null values to converters unless HandleNullOnWrite is true. Null values for properties were
// already handled in GetMemberAndWriteJson() so we don't need to check for IgnoreNullValues here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static void WriteCore<TValue>(
Debug.Assert(writer != null);

// We treat typeof(object) special and allow polymorphic behavior.
if (value != null && inputType == JsonClassInfo.ObjectType)
if (inputType == JsonClassInfo.ObjectType && value != null)
{
inputType = value!.GetType();
}
Expand Down