-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1388 xml serializer assembly load context awareness (#58932)
* Generate dynamic serialization assembly in the appropriate ALC, and don't keep any hard refs to types that could prevent unloading.
- Loading branch information
1 parent
1c7100b
commit d2415c4
Showing
12 changed files
with
416 additions
and
193 deletions.
There are no files selected for viewing
This file contains 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 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 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
309 changes: 183 additions & 126 deletions
309
src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compilation.cs
Large diffs are not rendered by default.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
src/libraries/System.Private.Xml/src/System/Xml/Serialization/ContextAwareTables.cs
This file contains 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,66 @@ | ||
// 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.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Loader; | ||
|
||
internal class ContextAwareTables<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]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) | ||
{ | ||
// The fast and most common default case | ||
T? ret = (T?)_defaultTable[t]; | ||
if (ret != null) | ||
return ret; | ||
|
||
// Common case for collectible contexts | ||
if (_collectibleTable.TryGetValue(t, out ret)) | ||
return ret; | ||
|
||
// 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) | ||
{ | ||
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 | ||
{ | ||
lock (_collectibleTable) | ||
{ | ||
if (!_collectibleTable.TryGetValue(t, out ret)) | ||
{ | ||
ret = f(); | ||
_collectibleTable.AddOrUpdate(t, ret); | ||
} | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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
Oops, something went wrong.