Skip to content

Commit be92dac

Browse files
perf: replace redundant allocations with Linq (#4172)
1 parent ba58121 commit be92dac

2 files changed

Lines changed: 8 additions & 26 deletions

File tree

TUnit.Engine/Discovery/ReflectionTestDataCollector.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ private static async IAsyncEnumerable<TestMetadata> DiscoverTestsInAssemblyStrea
503503
continue;
504504
}
505505

506-
MethodInfo[] testMethods;
506+
IEnumerable<MethodInfo> testMethods;
507507
try
508508
{
509509
// Check if this class inherits tests from base classes
@@ -513,15 +513,14 @@ private static async IAsyncEnumerable<TestMetadata> DiscoverTestsInAssemblyStrea
513513
{
514514
// Get all test methods including inherited ones
515515
testMethods = GetAllTestMethods(type)
516-
.Where(static m => m.IsDefined(typeof(TestAttribute), inherit: false) && !m.IsAbstract)
517-
.ToArray();
516+
.Where(static m => m.IsDefined(typeof(TestAttribute), inherit: false) && !m.IsAbstract);
518517
}
519518
else
520519
{
521520
// Only get declared test methods
522-
testMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)
523-
.Where(static m => m.IsDefined(typeof(TestAttribute), inherit: false) && !m.IsAbstract)
524-
.ToArray();
521+
testMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static |
522+
BindingFlags.DeclaredOnly)
523+
.Where(static m => m.IsDefined(typeof(TestAttribute), inherit: false) && !m.IsAbstract);
525524
}
526525
}
527526
catch (Exception)

TUnit.Engine/Services/TestRegistry.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ private async Task<TestMetadata> CreateMetadataFromDynamicDiscoveryResult(Dynami
261261
TestName = testName,
262262
TestClassType = result.TestClassType,
263263
TestMethodName = methodInfo.Name,
264-
Dependencies = GetDependenciesOptimized(result.Attributes),
264+
Dependencies = result.Attributes.OfType<DependsOnAttribute>()
265+
.Select(x => x.ToTestDependency())
266+
.ToArray(),
265267
DataSources = [],
266268
ClassDataSources = [],
267269
PropertyDataSources = [],
@@ -339,25 +341,6 @@ private sealed class PendingDynamicTest
339341
public required Type TestClassType { get; init; }
340342
}
341343

342-
343-
/// <summary>
344-
/// Optimized method to get dependencies without LINQ allocations
345-
/// </summary>
346-
private static TestDependency[] GetDependenciesOptimized(ICollection<Attribute> attributes)
347-
{
348-
var dependencies = new List<TestDependency>(attributes.Count);
349-
foreach (var attr in attributes)
350-
{
351-
if (attr is DependsOnAttribute dependsOn)
352-
{
353-
dependencies.Add(dependsOn.ToTestDependency());
354-
}
355-
}
356-
return dependencies.ToArray();
357-
}
358-
359-
360-
361344
/// <summary>
362345
/// Optimized method to convert attributes to array without LINQ allocations
363346
/// </summary>

0 commit comments

Comments
 (0)