-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Make resolver GetTypeInfo methods return new instances on every call #76683
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
Conversation
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsFixes #76535.
|
|
||
HashSet<TypeGenerationSpec> types = new(_currentContext.TypesWithMetadataGenerated); | ||
public override {JsonTypeInfoTypeRef} {GetTypeInfoMethodName}({TypeTypeRef} {TypeLocalVariableName}) | ||
=> {GetTypeInfoCoreMethodName}({TypeLocalVariableName}, {OptionsInstanceVariableName}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
=> {GetTypeInfoCoreMethodName}({TypeLocalVariableName}, {OptionsInstanceVariableName}); | |
=> {GetTypeInfoCoreMethodName}({TypeLocalVariableName}, {OptionsPropertyName}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good change overall, but it will introduce susbtantial performance regressions in all serialization methods accepting JsonSerializerContext
(since metadata will be created from scratch on each operation).
Possible alternatives:
- Take Ensure source generated metadata properties are read-only. #76540 instead so that
JsonSerializerContext.GetTypeInfo
returns the same instances but guarantee that those are read-only. Note thatIJsonTypeInfoResolver
would still return fresh mutable instances to guarantee compositionality. - Update the implementation of
JsonSerializers
'sJsonSerializerContext
resolution logic so that metadata is looked up via theJsonSerializerOptions
cache. Note that the standardJsonSerializerOptions.GetTypeInfo*
methods will returnNotSupportedException
on missing metadata instead ofInvalidOperationException
so some adjustment might be required there.
On that note, it appears that this approach has the potential of interacting with the reflection fallback compatibility switch that was recently added. TL;DR if the compatibility switch is enabled, it would most likely result in reflection metadata leaking into |
@layomia I pushed commit eiriktsarpalis@240a24c in my fork of your PR branch which demonstrate how we could keep using cached metadata in the |
Yeah can confirm this happens; I noticed reflection-fallback test failures in an earlier iteration of this PR when taking this approach.
I agree this would be the right way to mitigate the performance hit from this change were we to go down the path this PR proposes. But the solution in #76540 is more complete and less breaking so I'll close this in favor of that approach. |
Fixes #76535.