Skip to content
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

Improve performance of expensive operation #74247

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -76,30 +78,25 @@ private void UpdateProviders(object? sender, WorkspaceChangeEventArgs e)

private static ISettingsProviderFactory<T> GetOptionsProviderFactory<T>(Workspace workspace)
{
var providers = new List<ISettingsProviderFactory<T>>();
using var providers = TemporaryArray<ISettingsProviderFactory<T>>.Empty;

var commonProvider = workspace.Services.GetRequiredService<IWorkspaceSettingsProviderFactory<T>>();
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<T>([.. providers]);
return new CombinedOptionsProviderFactory<T>(providers.ToImmutableAndClear());

static void TryAddProviderForLanguage(string language, Workspace workspace, List<ISettingsProviderFactory<T>> providers)
void TryAddProviderForLanguage(string language)
{
var provider = workspace.Services.GetLanguageServices(language).GetService<ILanguageSettingsProviderFactory<T>>();
if (provider is not null)
if (projectCountByLanguage.ContainsKey(language))
{
providers.Add(provider);
var provider = workspace.Services.GetLanguageServices(language).GetService<ILanguageSettingsProviderFactory<T>>();
if (provider != null)
providers.Add(provider);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ internal sealed partial class SolutionState
public ImmutableDictionary<string, StructuredAnalyzerConfigOptions> FallbackAnalyzerOptions { get; } = ImmutableDictionary<string, StructuredAnalyzerConfigOptions>.Empty;

/// <summary>
/// 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

ever hit

does ever hit zero?

/// </summary>
internal ImmutableDictionary<string, int> ProjectCountByLanguage { get; } = ImmutableDictionary<string, int>.Empty;

Expand Down
Loading