Skip to content

Commit

Permalink
Remove allocations from IsCustomAttributeDefined (#44694)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Nov 15, 2020
1 parent 7690431 commit 25260d1
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1115,32 +1115,49 @@ internal static bool IsAttributeDefined(RuntimeModule decoratedModule, int decor
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<object> 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
{
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;
}
}
}

Expand Down

0 comments on commit 25260d1

Please sign in to comment.