Skip to content

Commit c49d16f

Browse files
Evangelinkgithub-actions[bot]Copilot
authored
[Perf Improver] perf: reduce allocations in test discovery and data-driven test execution (#7818)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7d4c0f0 commit c49d16f

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,19 @@ private async Task<TestResult[]> ExecuteTestWithDataSourceAsync(UTF.ITestDataSou
321321
data = tupleExpandedToArray;
322322
}
323323

324-
displayName = displayNameFromTestDataRow
325-
?? testDataSource?.GetDisplayName(new ReflectionTestMethodInfo(_testMethodInfo.MethodInfo, _test.DisplayName), data)
326-
?? (testDataSource is null ? displayName : TestDataSourceUtilities.ComputeDefaultDisplayName(new ReflectionTestMethodInfo(_testMethodInfo.MethodInfo, _test.DisplayName), data))
327-
?? displayName;
324+
// PERF: Extract ReflectionTestMethodInfo to avoid allocating it twice when testDataSource is not null
325+
// and both GetDisplayName and ComputeDefaultDisplayName need to be consulted.
326+
if (displayNameFromTestDataRow is null && testDataSource is not null)
327+
{
328+
var reflectionMethodInfo = new ReflectionTestMethodInfo(_testMethodInfo.MethodInfo, _test.DisplayName);
329+
displayName = testDataSource.GetDisplayName(reflectionMethodInfo, data)
330+
?? TestDataSourceUtilities.ComputeDefaultDisplayName(reflectionMethodInfo, data)
331+
?? displayName;
332+
}
333+
else
334+
{
335+
displayName = displayNameFromTestDataRow ?? displayName;
336+
}
328337

329338
var stopwatch = Stopwatch.StartNew();
330339
_testMethodInfo.SetArguments(data);

src/Adapter/MSTestAdapter.PlatformServices/Services/TestSourceHandler.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,22 @@ internal sealed class TestSourceHandler : ITestSourceHandler
5252
];
5353
#endif
5454

55-
/// <summary>
56-
/// Gets the set of valid extensions for sources targeting this platform.
57-
/// </summary>
58-
public IEnumerable<string> ValidSourceExtensions => new List<string>
59-
{
60-
EngineConstants.DllExtension,
55+
// PERF: Cached to avoid allocating a new list on every property access.
56+
private static readonly IEnumerable<string> CachedValidSourceExtensions =
57+
[
58+
EngineConstants.DllExtension,
6159
#if NETFRAMEWORK
62-
EngineConstants.PhoneAppxPackageExtension,
60+
EngineConstants.PhoneAppxPackageExtension,
6361
#elif WINDOWS_UWP || WIN_UI
64-
EngineConstants.AppxPackageExtension,
62+
EngineConstants.AppxPackageExtension,
6563
#endif
66-
EngineConstants.ExeExtension,
67-
};
64+
EngineConstants.ExeExtension,
65+
];
66+
67+
/// <summary>
68+
/// Gets the set of valid extensions for sources targeting this platform.
69+
/// </summary>
70+
public IEnumerable<string> ValidSourceExtensions => CachedValidSourceExtensions;
6871

6972
/// <summary>
7073
/// Verifies if the assembly provided is referenced by the source.

0 commit comments

Comments
 (0)