Optimize TestMethodRunner: fix double enumeration, reduce allocations#7541
Merged
Evangelink merged 5 commits intomainfrom Mar 15, 2026
Merged
Optimize TestMethodRunner: fix double enumeration, reduce allocations#7541Evangelink merged 5 commits intomainfrom
Evangelink merged 5 commits intomainfrom
Conversation
Member
Evangelink
commented
Mar 12, 2026
- Fix double enumeration in TryExecuteFoldedDataDrivenTestsAsync by materializing GetData() into an IReadOnlyList once instead of calling .Any() then re-enumerating
- Remove unnecessary List allocation in UpdateResultsWithParentInfo by mutating in place and returning void
- Cache result[result.Length - 1] into a local in ExecuteAsync catch block
- Skip redundant first-element self-comparison in GetAggregateOutcome
- Use Stopwatch.StartNew() in ExecuteTestFromDataSourceAttributeAsync
- Fix double enumeration in TryExecuteFoldedDataDrivenTestsAsync by materializing GetData() into an IReadOnlyList once instead of calling .Any() then re-enumerating - Remove unnecessary List allocation in UpdateResultsWithParentInfo by mutating in place and returning void - Cache result[result.Length - 1] into a local in ExecuteAsync catch block - Skip redundant first-element self-comparison in GetAggregateOutcome - Use Stopwatch.StartNew() in ExecuteTestFromDataSourceAttributeAsync
- Hoist loop-invariant 'testContext as TestContextImplementation' cast above the foreach loop in RunAssemblyCleanupAsync - Remove unnecessary nested scope in IsTestMethodRunnable - Use 'is null' pattern instead of '== null' - Simplify ignore message logic by inlining into the conditional block
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes TestMethodRunner execution paths in the MSTest adapter by reducing redundant work (enumerations/allocations) during data-driven execution and result aggregation.
Changes:
- Materialize folded data-driven
GetData()results once to avoid double enumeration and enableCountchecks. - Remove an unnecessary list allocation by mutating results in place in
UpdateResultsWithParentInfo. - Minor micro-optimizations (cache last result in exception path, skip redundant first element in aggregate outcome, use
Stopwatch.StartNew()).
You can also share your feedback on Copilot code review. Take the survey.
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs
Outdated
Show resolved
Hide resolved
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs
Show resolved
Hide resolved
Youssef1313
reviewed
Mar 12, 2026
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs
Outdated
Show resolved
Hide resolved
Youssef1313
reviewed
Mar 12, 2026
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs
Outdated
Show resolved
Hide resolved
Youssef1313
reviewed
Mar 12, 2026
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs
Show resolved
Hide resolved
Youssef1313
reviewed
Mar 12, 2026
Youssef1313
reviewed
Mar 12, 2026
src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs
Outdated
Show resolved
Hide resolved
Youssef1313
reviewed
Mar 13, 2026
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs
Outdated
Show resolved
Hide resolved
- Resolve merge conflict in TestMethodRunner.cs (take main's simplified catch block) - Fix GetData() double-call: enumerate once with bool flag instead of materializing to list - Fix UpdateResultsWithParentInfo XML doc to match actual behavior - Preserve ignore message behavior in UnitTestRunner.cs (add shouldIgnoreMethod guard)
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
You can also share your feedback on Copilot code review. Take the survey.
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs
Outdated
Show resolved
Hide resolved
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestMethodRunner.cs
Show resolved
Hide resolved
…path - Update comment in TestMethodRunner to reference AssemblyEnumerator.TryUnfoldITestDataSource - Update comment in AssemblyEnumerator to reference TestMethodRunner.TryExecuteFoldedDataDrivenTestsAsync - Apply same single-enumeration pattern in AssemblyEnumerator.TryUnfoldITestDataSource to avoid double-enumerating GetData()
Youssef1313
approved these changes
Mar 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.