-
Notifications
You must be signed in to change notification settings - Fork 5.3k
1388 xml serializer assembly load context awareness #58932
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
StephenMolloy
merged 14 commits into
dotnet:main
from
StephenMolloy:1388_XmlSerializer-AssemblyLoadContext-Awareness
Nov 5, 2021
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4647d16
Generate dynamic serialization assembly in the appropriate ALC, and d…
StephenMolloy 55eb88b
Add small test for ALC unloading.
StephenMolloy d9e96dc
PR feedback; Move into TempAssembly; Strong/Weak lookup tables; Test …
StephenMolloy b6f110a
Refining TempAssembly cache; Reflection-based fix; Test corrections
StephenMolloy 8616e29
Mono/wasm test fixup
StephenMolloy 88778c4
Ensure that serializers from XmlMappings don't run afoul of collectib…
StephenMolloy 15b7517
PR feedback
StephenMolloy 2c82165
Unloadable contexts not yet supported in Mono.
StephenMolloy da3c8c1
Fix trimming of types for XmlSerializer tests that got moved out of m…
StephenMolloy 0d37491
Encapsulating the duality of dealing with unloadable ALC instead of r…
StephenMolloy 6ab0bcd
Missed new file in last commit.
StephenMolloy f160c1b
Compilation bug not seen locally.
StephenMolloy 454a9fe
Perf improvement.
StephenMolloy d361830
Merge branch 'main' into 1388_XmlSerializer-AssemblyLoadContext-Aware…
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
Missed new file in last commit.
- Loading branch information
commit 6ab0bcd5678fdd20974fb7b24b87894441e01617
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/libraries/System.Private.Xml/src/System/Xml/Serialization/ContextAwareTables.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,62 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Xml.Serialization | ||
| { | ||
| using System; | ||
| using System.Collections; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.Loader; | ||
|
|
||
| internal class ContextAwareTables<T> where T : class? | ||
| { | ||
| private Hashtable _defaultTable; | ||
| private ConditionalWeakTable<Type, T> _collectibleTable; | ||
|
|
||
| public ContextAwareTables() | ||
| { | ||
| _defaultTable = new Hashtable(); | ||
| _collectibleTable = new ConditionalWeakTable<Type, T>(); | ||
| } | ||
|
|
||
| internal T GetOrCreateValue(Type t, Func<T> f) | ||
| { | ||
| T? ret; | ||
| AssemblyLoadContext? alc = AssemblyLoadContext.GetLoadContext(t.Assembly); | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How expensive is ALC.GetLoadContext? If it's non-trivial, it might be worth always checking the default table first and only using GetLoadContext to see which table is should go in if not found in the default. If it's cheap, then this is fine. |
||
| // Null and non-collectible load contexts use the default table | ||
| if (alc == null || !alc.IsCollectible) | ||
| { | ||
| if ((ret = (T?)_defaultTable[t]) == null) | ||
| { | ||
| lock (_defaultTable) | ||
| { | ||
| if ((ret = (T?)_defaultTable[t]) == null) | ||
| { | ||
| ret = f(); | ||
| _defaultTable[t] = ret; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Collectible load contexts should use the ConditionalWeakTable so they can be unloaded | ||
| else | ||
| { | ||
| if (!_collectibleTable.TryGetValue(t, out ret)) | ||
| { | ||
| lock (_collectibleTable) | ||
| { | ||
| if (!_collectibleTable.TryGetValue(t, out ret)) | ||
| { | ||
| ret = f(); | ||
| _collectibleTable.AddOrUpdate(t, ret); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
| } | ||
| } | ||
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.
Usings should be outside of namespace