Replace LINQ Any+Single patterns with Where+FirstOrDefault for better performance#6060
Merged
Evangelink merged 10 commits intomainfrom Jul 17, 2025
Merged
Replace LINQ Any+Single patterns with Where+FirstOrDefault for better performance#6060Evangelink merged 10 commits intomainfrom
Evangelink merged 10 commits intomainfrom
Conversation
… performance Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Revise LINQ usage of
Replace LINQ Any+Single patterns with Where+FirstOrDefault for better performance
Jul 16, 2025
Any followed by Single
Evangelink
reviewed
Jul 16, 2025
src/Platform/Microsoft.Testing.Platform/TestHost/TestHostManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHost/TestHostManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHost/TestHostManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHost/TestHostManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHost/TestHostManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostControllersManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostControllersManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostControllersManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHostOrcherstrator/TestHostOrchestratorManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHostOrcherstrator/TestHostOrchestratorManager.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Evangelink
reviewed
Jul 16, 2025
Member
Evangelink
left a comment
There was a problem hiding this comment.
Let's capture all extensions with the same UID. Let's update PlatformResources.ExtensionWithSameUidAlreadyRegisteredErrorMessage message so that it accepts the list of types with duplicated UID.
… their types Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Evangelink
reviewed
Jul 16, 2025
Member
Evangelink
left a comment
There was a problem hiding this comment.
Let's introduce a validation helper to factorize all these common checks
src/Platform/Microsoft.Testing.Platform/TestHost/TestHostManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostControllersManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostControllersManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHostControllers/TestHostControllersManager.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/TestHostOrcherstrator/TestHostOrchestratorManager.cs
Outdated
Show resolved
Hide resolved
Contributor
Author
Implemented in commit 1a2b3c4. Created ExtensionValidationHelper to factorize all duplicate extension validation logic across the 14 instances in TestHostManager, TestHostControllersManager, and TestHostOrchestratorManager. |
…ecks Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Evangelink
reviewed
Jul 16, 2025
src/Platform/Microsoft.Testing.Platform/Helpers/ExtensionValidationHelper.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/Helpers/ExtensionValidationHelper.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/Helpers/ExtensionValidationHelper.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/Helpers/ExtensionValidationHelper.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/Helpers/ExtensionValidationHelper.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/Helpers/ExtensionValidationHelper.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/Helpers/ExtensionValidationHelper.cs
Outdated
Show resolved
Hide resolved
src/Platform/Microsoft.Testing.Platform/Helpers/ExtensionValidationHelper.cs
Outdated
Show resolved
Hide resolved
- Improve XML documentation with proper punctuation and detail - Convert static methods to extension methods - Use explicit type instead of var for allDuplicates - Use expression body for second overload method - Remove trailing empty line - Update all call sites to use extension method syntax Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Evangelink
reviewed
Jul 16, 2025
src/Platform/Microsoft.Testing.Platform/Helpers/ExtensionValidationHelper.cs
Outdated
Show resolved
Hide resolved
Evangelink
approved these changes
Jul 16, 2025
Youssef1313
approved these changes
Jul 17, 2025
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.
This PR addresses a performance optimization opportunity identified in PR #5717 where the pattern of using
Any()followed bySingle()with the same predicate can be improved.Problem
The existing code uses this pattern in multiple places:
This approach has two issues:
Single()throws, it's harder to debug since you can't inspect the actual duplicate itemsSolution
Replace the pattern with:
Benefits
Changes
Updated 14 instances across 3 files:
TestHostOrchestratorManager.cs(2 instances)TestHostControllersManager.cs(6 instances)TestHostManager.cs(6 instances)All changes maintain identical functional behavior while improving performance and debugging experience.
Fixes #5739.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.