Skip to content

Commit 25260d1

Browse files
authored
Remove allocations from IsCustomAttributeDefined (#44694)
1 parent 7690431 commit 25260d1

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,32 +1115,49 @@ private static bool IsCustomAttributeDefined(
11151115
private static bool IsCustomAttributeDefined(
11161116
RuntimeModule decoratedModule, int decoratedMetadataToken, RuntimeType? attributeFilterType, int attributeCtorToken, bool mustBeInheritable)
11171117
{
1118-
CustomAttributeRecord[] car = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);
1118+
MetadataImport scope = decoratedModule.MetadataImport;
1119+
1120+
scope.EnumCustomAttributes(decoratedMetadataToken, out MetadataEnumResult attributeTokens);
1121+
1122+
if (attributeTokens.Length == 0)
1123+
{
1124+
return false;
1125+
}
11191126

1127+
CustomAttributeRecord record = default;
11201128
if (attributeFilterType != null)
11211129
{
11221130
Debug.Assert(attributeCtorToken == 0);
11231131

1124-
MetadataImport scope = decoratedModule.MetadataImport;
11251132
RuntimeType.ListBuilder<object> derivedAttributes = default;
11261133

1127-
for (int i = 0; i < car.Length; i++)
1134+
for (int i = 0; i < attributeTokens.Length; i++)
11281135
{
1129-
if (FilterCustomAttributeRecord(car[i].tkCtor, in scope,
1136+
scope.GetCustomAttributeProps(attributeTokens[i],
1137+
out record.tkCtor.Value, out record.blob);
1138+
1139+
if (FilterCustomAttributeRecord(record.tkCtor, in scope,
11301140
decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, ref derivedAttributes,
11311141
out _, out _, out _))
1142+
{
11321143
return true;
1144+
}
11331145
}
11341146
}
11351147
else
11361148
{
11371149
Debug.Assert(attributeFilterType == null);
11381150
Debug.Assert(!MetadataToken.IsNullToken(attributeCtorToken));
11391151

1140-
for (int i = 0; i < car.Length; i++)
1152+
for (int i = 0; i < attributeTokens.Length; i++)
11411153
{
1142-
if (car[i].tkCtor == attributeCtorToken)
1154+
scope.GetCustomAttributeProps(attributeTokens[i],
1155+
out record.tkCtor.Value, out record.blob);
1156+
1157+
if (record.tkCtor == attributeCtorToken)
1158+
{
11431159
return true;
1160+
}
11441161
}
11451162
}
11461163

0 commit comments

Comments
 (0)