In DataContractSerialization we have a few places where we are caching typeof(XX) into static Type variables. For example:
|
private static Type? s_typeOfObject; |
|
internal static Type TypeOfObject |
|
{ |
|
get |
|
{ |
|
if (s_typeOfObject == null) |
|
s_typeOfObject = typeof(object); |
|
return s_typeOfObject; |
|
} |
|
} |
|
|
|
private static Type? s_typeOfValueType; |
|
internal static Type TypeOfValueType |
|
{ |
|
get |
|
{ |
|
if (s_typeOfValueType == null) |
|
s_typeOfValueType = typeof(ValueType); |
|
return s_typeOfValueType; |
|
} |
|
} |
|
|
|
private static Type? s_typeOfArray; |
|
internal static Type TypeOfArray |
|
{ |
|
get |
|
{ |
|
if (s_typeOfArray == null) |
|
s_typeOfArray = typeof(Array); |
|
return s_typeOfArray; |
|
} |
|
} |
We should remove these caches, where possible, and instead just use typeof(XX) directly inline as needed. This will allow the JIT to see that the code is using runtime Type instances, and it can optimize the code if possible.
See #42824 (comment).
In DataContractSerialization we have a few places where we are caching
typeof(XX)into staticTypevariables. For example:runtime/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Globals.cs
Lines 47 to 78 in 855e296
We should remove these caches, where possible, and instead just use
typeof(XX)directly inline as needed. This will allow the JIT to see that the code is using runtime Type instances, and it can optimize the code if possible.See #42824 (comment).