From 3922b81fc9c408639c0a090eebcd65f3c0db96ad Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 24 Sep 2022 10:44:16 -0700 Subject: [PATCH] Delete typeof caches from CoreLib (#76126) Caching typeof is a de-optimization with frozen runtime types --- .../System/Reflection/RuntimeCustomAttributeData.cs | 7 ++----- .../src/System/Reflection/RuntimeParameterInfo.cs | 11 +++-------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs index b70ccefea9e2e..489ce5373fc4b 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs @@ -766,9 +766,6 @@ public CustomAttributeType(CustomAttributeEncoding encodedType, CustomAttributeE internal static unsafe class CustomAttribute { - private static readonly RuntimeType Type_RuntimeType = (RuntimeType)typeof(RuntimeType); - private static readonly RuntimeType Type_Type = (RuntimeType)typeof(Type); - #region Internal Static Members internal static bool IsDefined(RuntimeType type, RuntimeType? caType, bool inherit) { @@ -1221,9 +1218,9 @@ private static void AddCustomAttributes( if (type is null && value is not null) { type = (RuntimeType)value.GetType(); - if (type == Type_RuntimeType) + if (type == typeof(RuntimeType)) { - type = Type_Type; + type = (RuntimeType)typeof(Type); } } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs index 434a821715caf..078343e3cb6c1 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs @@ -106,11 +106,6 @@ private static ParameterInfo[] GetParameters( } #endregion - #region Private Statics - private static readonly Type s_DecimalConstantAttributeType = typeof(DecimalConstantAttribute); - private static readonly Type s_CustomConstantAttributeType = typeof(CustomConstantAttribute); - #endregion - #region Private Data Members private int m_tkParamDef; private MetadataImport m_scope; @@ -359,7 +354,7 @@ public override bool HasDefaultValue { defaultValue = GetRawDecimalConstant(attr); } - else if (attrType!.IsSubclassOf(s_CustomConstantAttributeType)) + else if (attrType!.IsSubclassOf(typeof(CustomConstantAttribute))) { defaultValue = GetRawConstant(attr); } @@ -367,14 +362,14 @@ public override bool HasDefaultValue } else { - object[] CustomAttrs = GetCustomAttributes(s_CustomConstantAttributeType, false); + object[] CustomAttrs = GetCustomAttributes(typeof(CustomConstantAttribute), false); if (CustomAttrs.Length != 0) { defaultValue = ((CustomConstantAttribute)CustomAttrs[0]).Value; } else { - CustomAttrs = GetCustomAttributes(s_DecimalConstantAttributeType, false); + CustomAttrs = GetCustomAttributes(typeof(DecimalConstantAttribute), false); if (CustomAttrs.Length != 0) { defaultValue = ((DecimalConstantAttribute)CustomAttrs[0]).Value;