-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
1388 xml serializer assembly load context awareness #58932
Changes from 6 commits
4647d16
55eb88b
d9e96dc
b6f110a
8616e29
88778c4
15b7517
2c82165
da3c8c1
0d37491
6ab0bcd
f160c1b
454a9fe
d361830
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -627,15 +627,16 @@ private static void AddObjectsIntoTargetCollection(object targetCollection, List | |
} | ||
} | ||
|
||
private static readonly ConcurrentDictionary<(Type, string), ReflectionXmlSerializationReaderHelper.SetMemberValueDelegate> s_setMemberValueDelegateCache = new ConcurrentDictionary<(Type, string), ReflectionXmlSerializationReaderHelper.SetMemberValueDelegate>(); | ||
private static readonly ConditionalWeakTable<Type, ConcurrentDictionary<string, ReflectionXmlSerializationReaderHelper.SetMemberValueDelegate>> s_setMemberValueDelegateCache = new ConditionalWeakTable<Type, ConcurrentDictionary<string, ReflectionXmlSerializationReaderHelper.SetMemberValueDelegate>>(); | ||
|
||
[RequiresUnreferencedCode(XmlSerializer.TrimSerializationWarning)] | ||
private static ReflectionXmlSerializationReaderHelper.SetMemberValueDelegate GetSetMemberValueDelegate(object o, string memberName) | ||
{ | ||
Debug.Assert(o != null, "Object o should not be null"); | ||
Debug.Assert(!string.IsNullOrEmpty(memberName), "memberName must have a value"); | ||
(Type, string) typeMemberNameTuple = (o.GetType(), memberName); | ||
if (!s_setMemberValueDelegateCache.TryGetValue(typeMemberNameTuple, out ReflectionXmlSerializationReaderHelper.SetMemberValueDelegate? result)) | ||
Type type = o.GetType(); | ||
var delegateCacheForType = s_setMemberValueDelegateCache.GetOrCreateValue(type); | ||
if (!delegateCacheForType.TryGetValue(memberName, out var result)) | ||
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. GetOrCreateValue uses Activator.CreateInstance to create an instance of TValue (ConcurrentDictionary in this case). There's another method you can use where you provide a callback which would be more efficient. var delegateCacheForType = s_setMemberValueDelegateCache.GetValue(type, _ => return new ConcurrentDictionary<string, ReflectionXmlSerializationReaderHelper.SetMemberValueDelegate>()); Also ConcurrentDictionary is very wasteful for space in it's defaults. The default capacity is 31 and default concurrency is the number of CPU's there are. It creates the same number of locks as the concurrency and spreads the data among that number of internal data stores. The likelyhood of there being multiple threads trying to write to this concurrent dictionary is very low, and that will only happen the first time serializing a particular type. I have 2 suggestions.
Also this is going to regress performance when using the reflection based serializer. I would suggest having two collections, one for the default ALC, and one for everything else. The extra cost would be on the creation, but lookup for the default case will remain exactly the same. |
||
{ | ||
MemberInfo memberInfo = ReflectionXmlSerializationHelper.GetEffectiveSetInfo(o.GetType(), memberName); | ||
Debug.Assert(memberInfo != null, "memberInfo could not be retrieved"); | ||
|
@@ -657,7 +658,7 @@ private static ReflectionXmlSerializationReaderHelper.SetMemberValueDelegate Get | |
MethodInfo getSetMemberValueDelegateWithTypeMi = getSetMemberValueDelegateWithTypeGenericMi.MakeGenericMethod(o.GetType(), memberType); | ||
var getSetMemberValueDelegateWithType = (Func<MemberInfo, ReflectionXmlSerializationReaderHelper.SetMemberValueDelegate>)getSetMemberValueDelegateWithTypeMi.CreateDelegate(typeof(Func<MemberInfo, ReflectionXmlSerializationReaderHelper.SetMemberValueDelegate>)); | ||
result = getSetMemberValueDelegateWithType(memberInfo); | ||
s_setMemberValueDelegateCache.TryAdd(typeMemberNameTuple, result); | ||
delegateCacheForType.TryAdd(memberName, result); | ||
} | ||
|
||
return result; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ namespace System.Xml.Serialization | |
using System.Xml.Serialization; | ||
using System.Xml; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
|
||
///<internalonly/> | ||
public abstract class XmlSerializationWriter : XmlSerializationGeneratedCode | ||
|
@@ -1465,13 +1466,13 @@ internal static class DynamicAssemblies | |
{ | ||
private static readonly Hashtable s_nameToAssemblyMap = new Hashtable(); | ||
private static readonly Hashtable s_assemblyToNameMap = new Hashtable(); | ||
private static readonly Hashtable s_tableIsTypeDynamic = Hashtable.Synchronized(new Hashtable()); | ||
private static readonly ConditionalWeakTable<Type, object> s_tableIsTypeDynamic = new ConditionalWeakTable<Type, object>(); | ||
|
||
StephenMolloy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// SxS: This method does not take any resource name and does not expose any resources to the caller. | ||
// It's OK to suppress the SxS warning. | ||
internal static bool IsTypeDynamic(Type type) | ||
{ | ||
object? oIsTypeDynamic = s_tableIsTypeDynamic[type]; | ||
s_tableIsTypeDynamic.TryGetValue(type, out object? oIsTypeDynamic); | ||
if (oIsTypeDynamic == null) | ||
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. I'm confident this isn't on a hot code path for RefEmit serializer, but what about the reflection based serializer? Does this get called for every object that's being serialized or is it an initialization only usage? If the former, then this needs to do the dual table route. |
||
{ | ||
Assembly assembly = type.Assembly; | ||
|
@@ -1500,7 +1501,7 @@ internal static bool IsTypeDynamic(Type type) | |
} | ||
} | ||
} | ||
s_tableIsTypeDynamic[type] = oIsTypeDynamic = isTypeDynamic; | ||
s_tableIsTypeDynamic.AddOrUpdate(type, oIsTypeDynamic = isTypeDynamic); | ||
} | ||
return (bool)oIsTypeDynamic; | ||
} | ||
|
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.
You are still using a ConditionalWeakTable for lookups even when using the default ALC. This will regress a lot as every single property set will hit this method and based on looking at the code for CWT, it's going to be expensive. This is used on a hot code path. You need to do the double table approach for this too.