From 191272bcb77e719c36ec067cd9ab056d6ef9e9ea Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sun, 15 Nov 2020 01:09:30 +0000 Subject: [PATCH] Remove allocations from IsCustomAttributeDefined --- .../src/System/Reflection/CustomAttribute.cs | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs index 49f4cedbeef7e3..41534ab0651837 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs @@ -1115,21 +1115,33 @@ private static bool IsCustomAttributeDefined( private static bool IsCustomAttributeDefined( RuntimeModule decoratedModule, int decoratedMetadataToken, RuntimeType? attributeFilterType, int attributeCtorToken, bool mustBeInheritable) { - CustomAttributeRecord[] car = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken); + MetadataImport scope = decoratedModule.MetadataImport; + + scope.EnumCustomAttributes(decoratedMetadataToken, out MetadataEnumResult attributeTokens); + + if (attributeTokens.Length == 0) + { + return false; + } + CustomAttributeRecord record = default; if (attributeFilterType != null) { Debug.Assert(attributeCtorToken == 0); - MetadataImport scope = decoratedModule.MetadataImport; RuntimeType.ListBuilder derivedAttributes = default; - for (int i = 0; i < car.Length; i++) + for (int i = 0; i < attributeTokens.Length; i++) { - if (FilterCustomAttributeRecord(car[i].tkCtor, in scope, + scope.GetCustomAttributeProps(attributeTokens[i], + out record.tkCtor.Value, out record.blob); + + if (FilterCustomAttributeRecord(record.tkCtor, in scope, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, ref derivedAttributes, out _, out _, out _)) + { return true; + } } } else @@ -1137,10 +1149,15 @@ private static bool IsCustomAttributeDefined( Debug.Assert(attributeFilterType == null); Debug.Assert(!MetadataToken.IsNullToken(attributeCtorToken)); - for (int i = 0; i < car.Length; i++) + for (int i = 0; i < attributeTokens.Length; i++) { - if (car[i].tkCtor == attributeCtorToken) + scope.GetCustomAttributeProps(attributeTokens[i], + out record.tkCtor.Value, out record.blob); + + if (record.tkCtor == attributeCtorToken) + { return true; + } } }