From b7207d5833932f3e6ebb44fb4ab893e74f5831b0 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 2 Jul 2024 13:34:40 -0700 Subject: [PATCH 1/3] Improve performance of expensive operation --- .../Aggregator/SettingsAggregator.cs | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/EditorFeatures/Core/EditorConfigSettings/Aggregator/SettingsAggregator.cs b/src/EditorFeatures/Core/EditorConfigSettings/Aggregator/SettingsAggregator.cs index 77c8f71c618ce..18b5510d8f54b 100644 --- a/src/EditorFeatures/Core/EditorConfigSettings/Aggregator/SettingsAggregator.cs +++ b/src/EditorFeatures/Core/EditorConfigSettings/Aggregator/SettingsAggregator.cs @@ -7,6 +7,8 @@ using System.Linq; using Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Data; using Microsoft.CodeAnalysis.Editor.EditorConfigSettings.DataProvider; +using Microsoft.CodeAnalysis.Shared.Collections; +using Microsoft.CodeAnalysis.Shared.Extensions; namespace Microsoft.CodeAnalysis.Editor.EditorConfigSettings; @@ -76,30 +78,25 @@ private void UpdateProviders(object? sender, WorkspaceChangeEventArgs e) private static ISettingsProviderFactory GetOptionsProviderFactory(Workspace workspace) { - var providers = new List>(); + using var providers = TemporaryArray>.Empty; + var commonProvider = workspace.Services.GetRequiredService>(); providers.Add(commonProvider); - var solution = workspace.CurrentSolution; - var supportsCSharp = solution.Projects.Any(p => p.Language.Equals(LanguageNames.CSharp, StringComparison.OrdinalIgnoreCase)); - var supportsVisualBasic = solution.Projects.Any(p => p.Language.Equals(LanguageNames.VisualBasic, StringComparison.OrdinalIgnoreCase)); - if (supportsCSharp) - { - TryAddProviderForLanguage(LanguageNames.CSharp, workspace, providers); - } - if (supportsVisualBasic) - { - TryAddProviderForLanguage(LanguageNames.VisualBasic, workspace, providers); - } + var projectCountByLanguage = workspace.CurrentSolution.SolutionState.ProjectCountByLanguage; + + TryAddProviderForLanguage(LanguageNames.CSharp); + TryAddProviderForLanguage(LanguageNames.VisualBasic); - return new CombinedOptionsProviderFactory([.. providers]); + return new CombinedOptionsProviderFactory(providers.ToImmutableAndClear()); - static void TryAddProviderForLanguage(string language, Workspace workspace, List> providers) + void TryAddProviderForLanguage(string language) { - var provider = workspace.Services.GetLanguageServices(language).GetService>(); - if (provider is not null) + if (projectCountByLanguage.TryGetValue(language, out var languageCount) && languageCount > 0) { - providers.Add(provider); + var provider = workspace.Services.GetLanguageServices(language).GetService>(); + if (provider != null) + providers.Add(provider); } } } From d33127495e9b7821b2bb857f24d5fed7ca5eef78 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 2 Jul 2024 13:37:04 -0700 Subject: [PATCH 2/3] Simplify --- .../Core/EditorConfigSettings/Aggregator/SettingsAggregator.cs | 2 +- .../Core/Portable/Workspace/Solution/SolutionState.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/EditorFeatures/Core/EditorConfigSettings/Aggregator/SettingsAggregator.cs b/src/EditorFeatures/Core/EditorConfigSettings/Aggregator/SettingsAggregator.cs index 18b5510d8f54b..174e9d7c074f9 100644 --- a/src/EditorFeatures/Core/EditorConfigSettings/Aggregator/SettingsAggregator.cs +++ b/src/EditorFeatures/Core/EditorConfigSettings/Aggregator/SettingsAggregator.cs @@ -92,7 +92,7 @@ private static ISettingsProviderFactory GetOptionsProviderFactory(Workspac void TryAddProviderForLanguage(string language) { - if (projectCountByLanguage.TryGetValue(language, out var languageCount) && languageCount > 0) + if (projectCountByLanguage.ContainsKey(language)) { var provider = workspace.Services.GetLanguageServices(language).GetService>(); if (provider != null) diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs b/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs index 15e051cfb20c4..e774fd5094f92 100644 --- a/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs +++ b/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs @@ -48,7 +48,8 @@ internal sealed partial class SolutionState public ImmutableDictionary FallbackAnalyzerOptions { get; } = ImmutableDictionary.Empty; /// - /// Number of projects in the solution of the given language. + /// Number of projects in the solution of the given language. The value is guaranteed to always be greater than zero. + /// If the project count does ever hit then there simply is no key/value pair for that language in this map. /// internal ImmutableDictionary ProjectCountByLanguage { get; } = ImmutableDictionary.Empty; From e09a38ad0e5e52e046a15a5c169cf0137c554616 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 2 Jul 2024 14:02:06 -0700 Subject: [PATCH 3/3] Update src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs --- .../Core/Portable/Workspace/Solution/SolutionState.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs b/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs index e774fd5094f92..cc386042c1c14 100644 --- a/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs +++ b/src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs @@ -49,7 +49,7 @@ internal sealed partial class SolutionState /// /// Number of projects in the solution of the given language. The value is guaranteed to always be greater than zero. - /// If the project count does ever hit then there simply is no key/value pair for that language in this map. + /// If the project count does ever hit zero then there simply is no key/value pair for that language in this map. /// internal ImmutableDictionary ProjectCountByLanguage { get; } = ImmutableDictionary.Empty;