You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Json generator should hoist property values in serialization fast-path when ignore condition on write is set #80703
Just noticed a minor missed optimization opportunity in the source generated serialization fast-path while going over some code in the Store. Consider this example where I have a model that has a property that allocates something on every read:
[JsonSerializable(typeof(MyModel))][JsonSourceGenerationOptions(DefaultIgnoreCondition=JsonIgnoreCondition.WhenWritingNull)]publicpartialclassMyJsonContext:JsonSerializerContext{}publicclassMyModel{publicstringSomeAllocatingProperty=>$"The current time is: {DateTime.Now}";}
The serialization fast-path is currently this one:
Playing devil's advocate, this would impact readability to account for the rare case where you have a DTO that isn't using auto properties.
The generator could improve this if:
Ignore condition is to ignore null/default values on write
The property is of a nullable or reference type
I'm pretty sure that the source generator specializes generated code depending on this configuration. If we've missed something, we'd definitely accept a PR that adds it.
if ((object)__value_SomeAllocatingProperty != null)
Why is the cast to object necessary here? I'm pretty certain that the compiler emits optimal code without it.
"Why is the cast to object necessary here? I'm pretty certain that the compiler emits optimal code without it."
Whoops, missed this message. The cast is needed to make sure to do a reference comparison with null even in the case where the type of that property has a custom equality operator implemented. If we can use new C# syntax, of course, we can also just do is not null (or is { }), which also always ignore any custom equality operators, even if present 🙂
Edit: I suppose since the existing behavior is (by design?) not doing this, we might leave it as is, or address it separately.
I think is not would have been the best way forward, but there's already a discussion about what the minimal supported language version should be (IIRC is not patterns are newer than C# 7.3)
I suppose since the existing behavior is (by design?)
Probably not, but then again I wouldn't worry too much about operators where != null has different meaning than expected.
Activity
eiriktsarpalis commentedon Jan 17, 2023
Playing devil's advocate, this would impact readability to account for the rare case where you have a DTO that isn't using auto properties.
I'm pretty sure that the source generator specializes generated code depending on this configuration. If we've missed something, we'd definitely accept a PR that adds it.
Why is the cast to
objectnecessary here? I'm pretty certain that the compiler emits optimal code without it.Sergio0694 commentedon Jun 17, 2023
Whoops, missed this message. The cast is needed to make sure to do a reference comparison with
nulleven in the case where the type of that property has a custom equality operator implemented. If we can use new C# syntax, of course, we can also just dois not null(oris { }), which also always ignore any custom equality operators, even if present 🙂Edit: I suppose since the existing behavior is (by design?) not doing this, we might leave it as is, or address it separately.
eiriktsarpalis commentedon Jun 19, 2023
I think
is notwould have been the best way forward, but there's already a discussion about what the minimal supported language version should be (IIRCis notpatterns are newer than C# 7.3)Probably not, but then again I wouldn't worry too much about operators where
!= nullhas different meaning than expected.eiriktsarpalis commentedon Jun 19, 2023
Fixed by #87725.