Skip to content

Remove Type caches in DataContractSerialization #42861

Description

@eerhardt

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).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions