Skip to content

Commit eee0e69

Browse files
perf: eliminate OfType iterator allocation with manual looping (#4513)
1 parent f4f2b5f commit eee0e69

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

TUnit.Core/Helpers/ClassConstructorHelper.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static class ClassConstructorHelper
4343
TestBuilderContext testBuilderContext,
4444
string testSessionId)
4545
{
46-
var classConstructorAttribute = attributes.OfType<ClassConstructorAttribute>().FirstOrDefault();
46+
var classConstructorAttribute = GetClassConstructorAttribute(attributes);
4747

4848
if (classConstructorAttribute == null)
4949
{
@@ -78,6 +78,21 @@ public static bool HasClassConstructorAttribute(Attribute[] attributes)
7878
/// </summary>
7979
public static ClassConstructorAttribute? GetClassConstructorAttribute(Attribute[] attributes)
8080
{
81-
return attributes.OfType<ClassConstructorAttribute>().FirstOrDefault();
81+
return GetClassConstructorAttribute((IReadOnlyList<ClassConstructorAttribute>)attributes);
82+
}
83+
84+
private static ClassConstructorAttribute? GetClassConstructorAttribute(IReadOnlyList<Attribute> attributes)
85+
{
86+
ClassConstructorAttribute? classConstructorAttribute = null;
87+
foreach (Attribute attribute in attributes)
88+
{
89+
if (attribute is ClassConstructorAttribute classAttribute)
90+
{
91+
classConstructorAttribute = classAttribute;
92+
break;
93+
}
94+
}
95+
96+
return classConstructorAttribute;
8297
}
8398
}

TUnit.Engine/Building/TestBuilder.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,13 +949,17 @@ public async Task<AbstractExecutableTest> BuildTestAsync(TestMetadata metadata,
949949
private static string? GetBasicSkipReason(TestMetadata metadata, Attribute[]? cachedAttributes = null)
950950
{
951951
var attributes = cachedAttributes ?? metadata.AttributeFactory();
952-
var skipAttributes = attributes.OfType<SkipAttribute>();
953952

954953
SkipAttribute? firstSkipAttribute = null;
955954

956955
// Check if all skip attributes are basic (non-derived) SkipAttribute instances
957-
foreach (var skipAttribute in skipAttributes)
956+
foreach (var attribute in attributes)
958957
{
958+
if (attribute is not SkipAttribute skipAttribute)
959+
{
960+
continue;
961+
}
962+
959963
firstSkipAttribute ??= skipAttribute;
960964

961965
var attributeType = skipAttribute.GetType();

0 commit comments

Comments
 (0)