Skip to content

[MTP] Improve performance of validating command line options #5655

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented May 27, 2025

This PR improves the performance of CommandLineOptionsValidator which was consuming an unnecessarily large amount of CPU time as identified in a trace involving multiple test processes.

Performance improvements:

Algorithm Optimizations

  • Reduced algorithmic complexity from O(n²) to O(n) in critical validation methods
  • Replaced expensive LINQ operation chains with direct data structure access
  • Optimized ValidateNoUnknownOptions by using a HashSet for O(1) lookups instead of nested LINQ operations

Memory Allocations

  • Optimized ToTrimmedString to avoid unnecessary string allocations
  • Created StringBuilder instances only when needed instead of always pre-allocating
  • Reduced intermediate collection creation during validation

Data Structure Improvements

  • Used HashSet and Dictionary for fast lookups instead of repeated LINQ operations
  • Eliminated expensive Union() and Intersect() operations with direct dictionary operations

Documentation

  • Added PerformanceSensitive attribute to document performance-critical code paths
  • Annotated all validation methods with appropriate performance attributes

Before Optimization

Profiling data showing high CPU time in CommandLineOptionsValidator.ValidateAsync

Fixes #5651.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [MTP] Improve performance of validating command line options [MTP] Improve performance of validating command line options May 27, 2025
@Copilot Copilot AI requested a review from Youssef1313 May 27, 2025 13:52
Copilot finished work on behalf of Youssef1313 May 27, 2025 13:52
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot finished work on behalf of Youssef1313 May 27, 2025 14:05
Copilot finished work on behalf of Youssef1313 May 27, 2025 14:12
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot finished work on behalf of Youssef1313 May 27, 2025 15:35
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot finished work on behalf of Youssef1313 May 27, 2025 15:44
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot finished work on behalf of Youssef1313 May 27, 2025 15:47
if (invalidReservedOptions.Any())
// Create a HashSet of all system option names for faster lookup
HashSet<string> systemOptionNames = new();
foreach (var provider in systemOptionsByProvider)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use explicit type instead of 'var'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed var to explicit type KeyValuePair<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> in 86c987c.

{
var stringBuilder = new StringBuilder();
foreach (string reservedOption in invalidReservedOptions)
foreach (var option in provider.Value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use explicit type instead of 'var'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed var to explicit type CommandLineOption in 2e2ee02.


return ValidationResult.Invalid(stringBuilder.ToTrimmedString());
StringBuilder? stringBuilder = null;
foreach (var provider in extensionOptionsByProvider)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use explicit type instead of 'var'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed var to explicit type KeyValuePair<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> in 2e2ee02.

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot finished work on behalf of Youssef1313 June 1, 2025 10:03
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot finished work on behalf of Youssef1313 June 1, 2025 10:09
…tation

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 1, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Copilot finished work on behalf of Youssef1313 June 1, 2025 10:57
…thod

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 1, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • kmuvsblobprodcus389.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25229.4.txt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of Youssef1313 June 1, 2025 11:03
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.32%. Comparing base (65f8636) to head (97c8299).
Report is 18 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5655      +/-   ##
==========================================
- Coverage   76.37%   76.32%   -0.05%     
==========================================
  Files         602      602              
  Lines       36759    36813      +54     
==========================================
+ Hits        28075    28099      +24     
- Misses       8684     8714      +30     
Flag Coverage Δ
Debug 76.32% <100.00%> (-0.05%) ⬇️
integration 76.32% <100.00%> (-0.05%) ⬇️
production 76.32% <100.00%> (-0.05%) ⬇️
unit 76.33% <100.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...latform/CommandLine/CommandLineOptionsValidator.cs 100.00% <100.00%> (ø)

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[MTP] Improve performance of validating command line options
3 participants