Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
Use eager evaluation when configuring the JsonTypeInfo type graph.#81576
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
Merged
eiriktsarpalis
merged 2 commits into
dotnet:main
from
eiriktsarpalis:jsontypeinfo-lockingFeb 3, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
23 changes: 13 additions & 10 deletions
23 ...ries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 12 additions & 21 deletions
33 ...ibraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 50 additions & 77 deletions
127 src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -295,9 +295,6 @@ public JsonPolymorphismOptions? PolymorphismOptions | ||
| internal PolymorphicTypeResolver? PolymorphicTypeResolver { get; private set; } | ||
| // If enumerable or dictionary, the JsonTypeInfo for the element type. | ||
| private JsonTypeInfo? _elementTypeInfo; | ||
| // Flag indicating that JsonTypeInfo<T>.SerializeHandler is populated and is compatible with the associated Options instance. | ||
| internal bool CanUseSerializeHandler { get; private protected set; } | ||
| @@ -314,84 +311,47 @@ internal void ValidateCanBeUsedForMetadataSerialization() | ||
| } | ||
| } | ||
| internal Type? ElementType { get; } | ||
| internal Type? KeyType { get; } | ||
| /// <summary> | ||
| /// Return the JsonTypeInfo for the element type, or null if the type is not an enumerable or dictionary. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This should not be called during warm-up (initial creation of JsonTypeInfos) to avoid recursive behavior | ||
| /// which could result in a StackOverflowException. | ||
| /// </remarks> | ||
| internal JsonTypeInfo? ElementTypeInfo | ||
| { | ||
| get | ||
| { | ||
| if (_elementTypeInfo == null) | ||
| { | ||
| if (ElementType != null) | ||
| { | ||
| // GetOrAddJsonTypeInfo already ensures JsonTypeInfo is configured | ||
| // also see comment on JsonPropertyInfo.JsonTypeInfo | ||
| _elementTypeInfo = Options.GetTypeInfoInternal(ElementType); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| _elementTypeInfo.EnsureConfigured(); | ||
| } | ||
| Debug.Assert(IsConfigured); | ||
| return _elementTypeInfo; | ||
| } | ||
| set | ||
| { | ||
| // Set by JsonMetadataServices. | ||
| Debug.Assert(_elementTypeInfo == null); | ||
| Debug.Assert(!IsReadOnly); | ||
| Debug.Assert(value is null || value.Type == ElementType); | ||
| _elementTypeInfo = value; | ||
| } | ||
| } | ||
| internal Type? ElementType { get; } | ||
| // If dictionary, the JsonTypeInfo for the key type. | ||
| private JsonTypeInfo? _keyTypeInfo; | ||
| /// <summary> | ||
| /// Return the JsonTypeInfo for the key type, or null if the type is not a dictionary. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This should not be called during warm-up (initial creation of JsonTypeInfos) to avoid recursive behavior | ||
| /// which could result in a StackOverflowException. | ||
| /// </remarks> | ||
| internal JsonTypeInfo? KeyTypeInfo | ||
| { | ||
| get | ||
| { | ||
| if (_keyTypeInfo == null) | ||
| { | ||
| if (KeyType != null) | ||
| { | ||
| Debug.Assert(Kind == JsonTypeInfoKind.Dictionary); | ||
| // GetOrAddJsonTypeInfo already ensures JsonTypeInfo is configured | ||
| // also see comment on JsonPropertyInfo.JsonTypeInfo | ||
| _keyTypeInfo = Options.GetTypeInfoInternal(KeyType); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| _keyTypeInfo.EnsureConfigured(); | ||
| } | ||
| Debug.Assert(IsConfigured); | ||
| return _keyTypeInfo; | ||
| } | ||
| set | ||
| { | ||
| // Set by JsonMetadataServices. | ||
| Debug.Assert(_keyTypeInfo == null); | ||
| Debug.Assert(!IsReadOnly); | ||
| Debug.Assert(value is null || value.Type == KeyType); | ||
| _keyTypeInfo = value; | ||
| } | ||
| } | ||
| internal Type? KeyType { get; } | ||
| private JsonTypeInfo? _elementTypeInfo; | ||
| private JsonTypeInfo? _keyTypeInfo; | ||
| /// <summary> | ||
| /// Gets the <see cref="JsonSerializerOptions"/> value associated with the current <see cref="JsonTypeInfo" /> instance. | ||
| @@ -530,61 +490,62 @@ internal void VerifyMutable() | ||
| } | ||
| } | ||
| private volatile bool _isConfigured; | ||
| private readonly object _configureLock = new object(); | ||
| private ExceptionDispatchInfo? _cachedConfigureError; | ||
| internal bool IsConfigured => _configurationState == ConfigurationState.Configured; | ||
| private volatile ConfigurationState _configurationState; | ||
| private enum ConfigurationState : byte { NotConfigured = 0, Configuring = 1, Configured = 2 }; | ||
| internal bool IsConfigured => _isConfigured; | ||
| private ExceptionDispatchInfo? _cachedConfigureError; | ||
| internal void EnsureConfigured() | ||
| { | ||
| Debug.Assert(!Monitor.IsEntered(_configureLock), "recursive locking detected."); | ||
| if (!_isConfigured) | ||
| ConfigureLocked(); | ||
| if (!IsConfigured) | ||
| ConfigureSynchronized(); | ||
| void ConfigureLocked() | ||
| void ConfigureSynchronized() | ||
| { | ||
| Options.MakeReadOnly(); | ||
| MakeReadOnly(); | ||
| _cachedConfigureError?.Throw(); | ||
| lock (_configureLock) | ||
| lock (Options.CacheContext) | ||
| { | ||
| if (_isConfigured) | ||
| if (_configurationState != ConfigurationState.NotConfigured) | ||
| { | ||
| // The value of _configurationState is either | ||
| // 'Configuring': recursive instance configured by this thread or | ||
| // 'Configured' : instance already configured by another thread. | ||
| // We can safely yield the configuration operation in both cases. | ||
| return; | ||
| } | ||
| _cachedConfigureError?.Throw(); | ||
| try | ||
| { | ||
| _configurationState = ConfigurationState.Configuring; | ||
| Configure(); | ||
| IsReadOnly = true; | ||
| _isConfigured = true; | ||
| _configurationState = ConfigurationState.Configured; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _cachedConfigureError = ExceptionDispatchInfo.Capture(e); | ||
| _configurationState = ConfigurationState.NotConfigured; | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| internal void Configure() | ||
| private void Configure() | ||
| { | ||
| Debug.Assert(Monitor.IsEntered(_configureLock), "Configure called directly, use EnsureConfigured which locks this method"); | ||
| if (!Options.IsReadOnly) | ||
| { | ||
| Options.MakeReadOnly(); | ||
| } | ||
| PropertyInfoForTypeInfo.EnsureConfigured(); | ||
| Debug.Assert(Monitor.IsEntered(Options.CacheContext), "Configure called directly, use EnsureConfigured which synchronizes access to this method"); | ||
| Debug.Assert(Options.IsReadOnly); | ||
| Debug.Assert(IsReadOnly); | ||
| PropertyInfoForTypeInfo.Configure(); | ||
| CanUseSerializeHandler &= Options.CanUseFastPathSerializationLogic; | ||
| Debug.Assert(PropertyInfoForTypeInfo.EffectiveConverter.ConverterStrategy == Converter.ConverterStrategy, | ||
| $"ConverterStrategy from PropertyInfoForTypeInfo.EffectiveConverter.ConverterStrategy ({PropertyInfoForTypeInfo.EffectiveConverter.ConverterStrategy}) does not match converter's ({Converter.ConverterStrategy})"); | ||
| if (Kind == JsonTypeInfoKind.Object) | ||
| { | ||
| ConfigureProperties(); | ||
| @@ -595,6 +556,18 @@ internal void Configure() | ||
| } | ||
| } | ||
| if (ElementType != null) | ||
| { | ||
| _elementTypeInfo ??= Options.GetTypeInfoInternal(ElementType); | ||
| _elementTypeInfo.EnsureConfigured(); | ||
| } | ||
| if (KeyType != null) | ||
| { | ||
| _keyTypeInfo ??= Options.GetTypeInfoInternal(KeyType); | ||
| _keyTypeInfo.EnsureConfigured(); | ||
| } | ||
eiriktsarpalis marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (PolymorphismOptions != null) | ||
| { | ||
| PolymorphicTypeResolver = new PolymorphicTypeResolver(this); | ||
| @@ -947,7 +920,7 @@ internal void ConfigureProperties() | ||
| } | ||
| } | ||
| property.EnsureConfigured(); | ||
| property.Configure(); | ||
| } | ||
| NumberOfRequiredProperties = numberOfRequiredProperties; | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.