-
Notifications
You must be signed in to change notification settings - Fork 5.5k
DCS work nicely with collectible AssemblyLoadContext #90437
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
39e1fc5
Add tests to check for ALC goodness with DCS.
StephenMolloy 12087fe
Add context-aware collections for caching/indexing DataContracts.
StephenMolloy d393af6
Refine the context-aware collection types.
StephenMolloy eca8155
Use lock to prevent running expensive delegate more than once.
StephenMolloy 2849664
Use double-check pattern to prevent 'create' delegate from running mu…
StephenMolloy 792fb91
Upon further review, GetValue/GetOrAdd already do this check before r…
StephenMolloy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
103 changes: 103 additions & 0 deletions
103
...System.Private.DataContractSerialization/src/System/Runtime/Serialization/ContextAware.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 |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections; | ||
| using System.Collections.Concurrent; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Reflection; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.Loader; | ||
| using System.Runtime.Serialization.DataContracts; | ||
|
|
||
| namespace System.Runtime.Serialization | ||
| { | ||
| internal sealed class ContextAwareDataContractIndex | ||
| { | ||
| private (DataContract? strong, WeakReference<DataContract>? weak)[] _contracts; | ||
| private ConditionalWeakTable<Type, DataContract> _keepAlive; | ||
|
|
||
| public int Length => _contracts.Length; | ||
|
|
||
| public ContextAwareDataContractIndex(int size) | ||
| { | ||
| _contracts = new (DataContract?, WeakReference<DataContract>?)[size]; | ||
| _keepAlive = new ConditionalWeakTable<Type, DataContract>(); | ||
| } | ||
|
|
||
| public DataContract? GetItem(int index) => _contracts[index].strong ?? (_contracts[index].weak?.TryGetTarget(out DataContract? ret) == true ? ret : null); | ||
|
|
||
| public void SetItem(int index, DataContract dataContract) | ||
| { | ||
| // Check for unloadability to decide how to store the value | ||
| AssemblyLoadContext? alc = AssemblyLoadContext.GetLoadContext(dataContract.UnderlyingType.Assembly); | ||
| if (alc == null || !alc.IsCollectible) | ||
| { | ||
| _contracts[index].strong = dataContract; | ||
| } | ||
| else | ||
| { | ||
| _contracts[index].weak = new WeakReference<DataContract>(dataContract); | ||
| _keepAlive.Add(dataContract.UnderlyingType, dataContract); | ||
| } | ||
| } | ||
|
|
||
| public void Resize(int newSize) | ||
| { | ||
| Array.Resize<(DataContract?, WeakReference<DataContract>?)>(ref _contracts, newSize); | ||
| } | ||
| } | ||
|
|
||
| internal sealed class ContextAwareDictionary<TKey, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TValue> | ||
| where TKey : Type | ||
| where TValue : class? | ||
| { | ||
| private readonly ConcurrentDictionary<TKey, TValue> _fastDictionary = new(); | ||
| private readonly ConditionalWeakTable<TKey, TValue> _collectibleTable = new(); | ||
|
|
||
|
|
||
| internal TValue GetOrAdd(TKey t, Func<TKey, TValue> f) | ||
| { | ||
| TValue? ret; | ||
|
|
||
| // The fast and most common default case | ||
| if (_fastDictionary.TryGetValue(t, out ret)) | ||
| return ret; | ||
|
|
||
| // Common case for collectible contexts | ||
| if (_collectibleTable.TryGetValue(t, out ret)) | ||
| return ret; | ||
|
StephenMolloy marked this conversation as resolved.
|
||
|
|
||
| // Not found. Do the slower work of creating the value in the correct collection. | ||
| AssemblyLoadContext? alc = AssemblyLoadContext.GetLoadContext(t.Assembly); | ||
|
|
||
| // Null and non-collectible load contexts use the default table | ||
| if (alc == null || !alc.IsCollectible) | ||
| { | ||
| // The create delegate here could be quite expensive. ConcurrentDictionary semantics would let use | ||
| // do this without a lock and not corrupt the dictionary, but the delegate still might be called multiple | ||
| // times. So we use a lock to ensure the delegate is only called once. Keep the lock off the hot path. | ||
| if (!_fastDictionary.TryGetValue(t, out ret)) | ||
| { | ||
|
StephenMolloy marked this conversation as resolved.
|
||
| lock (_fastDictionary) | ||
| { | ||
| return _fastDictionary.GetOrAdd(t, f); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Collectible load contexts should use the ConditionalWeakTable so they can be unloaded | ||
| else | ||
| { | ||
| if (!_collectibleTable.TryGetValue(t, out ret)) | ||
| { | ||
| lock (_collectibleTable) | ||
| { | ||
| return _collectibleTable.GetValue(t, k => f(k)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
| } | ||
| } | ||
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
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
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
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
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.
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.
It's only a style thing, but did you consider having providing an indexer property instead of switching to Get and Set methods?
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.
Sort of an evolution of the context-aware side of the design that got away from the original usage pattern. An indexer would fit the original code. But the nullable notation of the original code was incorrect, even if our code was dilligent about null-checking. Maybe I've still got Monday brain, but I don't know of a way to make the getter nullable and the setter non-nullable... notation-wise anyway.