-
Notifications
You must be signed in to change notification settings - Fork 290
Replace LINQ Any+Single patterns with Where+FirstOrDefault for better performance #6060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… performance Co-authored-by: Evangelink <[email protected]>
Any followed by Single
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 <[email protected]>
Co-authored-by: Evangelink <[email protected]>
Evangelink
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]>
Evangelink
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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 <[email protected]>
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 <[email protected]>
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.