Skip to content

Commit

Permalink
Avoid Linq allocations from usage of Union (#4265)
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 authored Dec 7, 2024
1 parent beb890d commit d269367
Showing 1 changed file with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,24 @@ private static async Task<ValidationResult> ValidateOptionsArgumentsAsync(
}

private static async Task<ValidationResult> ValidateConfigurationAsync(
IEnumerable<ICommandLineOptionsProvider> extensionsProviders,
IEnumerable<ICommandLineOptionsProvider> systemProviders,
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>>.KeyCollection extensionsProviders,
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>>.KeyCollection systemProviders,
ICommandLineOptions commandLineOptions)
{
StringBuilder? stringBuilder = null;
foreach (ICommandLineOptionsProvider commandLineOptionsProvider in systemProviders.Union(extensionsProviders))
StringBuilder? stringBuilder = await ValidateConfigurationAsync(systemProviders, commandLineOptions, null);
stringBuilder = await ValidateConfigurationAsync(extensionsProviders, commandLineOptions, stringBuilder);

return stringBuilder?.Length > 0
? ValidationResult.Invalid(stringBuilder.ToTrimmedString())
: ValidationResult.Valid();
}

private static async Task<StringBuilder?> ValidateConfigurationAsync(
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>>.KeyCollection providers,
ICommandLineOptions commandLineOptions,
StringBuilder? stringBuilder)
{
foreach (ICommandLineOptionsProvider commandLineOptionsProvider in providers)
{
ValidationResult result = await commandLineOptionsProvider.ValidateCommandLineOptionsAsync(commandLineOptions);
if (!result.IsValid)
Expand All @@ -241,9 +253,7 @@ private static async Task<ValidationResult> ValidateConfigurationAsync(
}
}

return stringBuilder?.Length > 0
? ValidationResult.Invalid(stringBuilder.ToTrimmedString())
: ValidationResult.Valid();
return stringBuilder;
}

private static string ToTrimmedString(this StringBuilder stringBuilder)
Expand Down

0 comments on commit d269367

Please sign in to comment.