Skip to content

Use lazy initialization for members in CodeFixService #78484

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

Merged
merged 1 commit into from
May 8, 2025
Merged
Changes from all 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
18 changes: 9 additions & 9 deletions src/Features/Core/Portable/CodeFixes/Service/CodeFixService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ namespace Microsoft.CodeAnalysis.CodeFixes;
internal sealed partial class CodeFixService : ICodeFixService
{
private readonly ImmutableArray<Lazy<CodeFixProvider, CodeChangeProviderMetadata>> _fixers;
private readonly ImmutableDictionary<string, ImmutableArray<Lazy<CodeFixProvider, CodeChangeProviderMetadata>>> _fixersPerLanguageMap;
private readonly Lazy<ImmutableDictionary<string, ImmutableArray<Lazy<CodeFixProvider, CodeChangeProviderMetadata>>>> _fixersPerLanguageMap;

private readonly ConditionalWeakTable<IReadOnlyList<AnalyzerReference>, ImmutableDictionary<DiagnosticId, ImmutableArray<CodeFixProvider>>> _projectFixersMap = new();

// Shared by project fixers and workspace fixers.
private readonly ImmutableDictionary<LanguageKind, Lazy<ImmutableArray<IConfigurationFixProvider>>> _configurationProvidersMap;
private readonly Lazy<ImmutableDictionary<LanguageKind, Lazy<ImmutableArray<IConfigurationFixProvider>>>> _configurationProvidersMap;
private readonly ImmutableArray<Lazy<IErrorLoggerService>> _errorLoggers;

private ImmutableDictionary<LanguageKind, Lazy<ImmutableDictionary<DiagnosticId, ImmutableArray<CodeFixProvider>>>>? _lazyWorkspaceFixersMap;
Expand All @@ -65,9 +65,9 @@ public CodeFixService(
_errorLoggers = [.. loggers];

_fixers = [.. fixers];
_fixersPerLanguageMap = _fixers.ToPerLanguageMapWithMultipleLanguages();
_fixersPerLanguageMap = new(() => _fixers.ToPerLanguageMapWithMultipleLanguages());

_configurationProvidersMap = GetConfigurationProvidersPerLanguageMap(configurationProviders);
_configurationProvidersMap = new(() => GetConfigurationProvidersPerLanguageMap(configurationProviders));
}

private Func<string, bool>? GetShouldIncludeDiagnosticPredicate(
Expand Down Expand Up @@ -455,7 +455,7 @@ private async IAsyncEnumerable<CodeFixCollection> StreamFixesAsync(
var hasAnySharedFixer = TryGetWorkspaceFixersMap(document, out var fixerMap);

var projectFixersMap = GetProjectFixers(document);
var hasAnyProjectFixer = projectFixersMap.Any();
var hasAnyProjectFixer = !projectFixersMap.IsEmpty;

if (!hasAnySharedFixer && !hasAnyProjectFixer)
yield break;
Expand Down Expand Up @@ -715,7 +715,7 @@ private async IAsyncEnumerable<CodeFixCollection> StreamConfigurationFixesAsync(
{
cancellationToken.ThrowIfCancellationRequested();

if (!_configurationProvidersMap.TryGetValue(document.Project.Language, out var lazyConfigurationProviders) ||
if (!_configurationProvidersMap.Value.TryGetValue(document.Project.Language, out var lazyConfigurationProviders) ||
lazyConfigurationProviders.Value == null)
{
yield break;
Expand Down Expand Up @@ -812,7 +812,7 @@ await diagnosticsWithSameSpan.OrderByDescending(d => d.Severity)
/// <summary> Looks explicitly for an <see cref="AbstractSuppressionCodeFixProvider"/>.</summary>
public CodeFixProvider? GetSuppressionFixer(string language, IEnumerable<string> diagnosticIds)
{
if (!_configurationProvidersMap.TryGetValue(language, out var lazyConfigurationProviders) ||
if (!_configurationProvidersMap.Value.TryGetValue(language, out var lazyConfigurationProviders) ||
lazyConfigurationProviders.Value.IsDefault)
{
return null;
Expand Down Expand Up @@ -881,7 +881,7 @@ private ImmutableDictionary<LanguageKind, Lazy<ImmutableDictionary<DiagnosticId,
{
var fixerMap = ImmutableDictionary.Create<LanguageKind, Lazy<ImmutableDictionary<DiagnosticId, ImmutableArray<CodeFixProvider>>>>();
var extensionManager = services.GetService<IExtensionManager>();
foreach (var (diagnosticId, lazyFixers) in _fixersPerLanguageMap)
foreach (var (diagnosticId, lazyFixers) in _fixersPerLanguageMap.Value)
{
var lazyMap = new Lazy<ImmutableDictionary<DiagnosticId, ImmutableArray<CodeFixProvider>>>(() =>
{
Expand Down Expand Up @@ -942,7 +942,7 @@ static ImmutableArray<IConfigurationFixProvider> GetConfigurationFixProviders(Im
private ImmutableDictionary<LanguageKind, Lazy<ImmutableDictionary<CodeFixProvider, int>>> GetFixerPriorityPerLanguageMap(SolutionServices services)
{
var languageMap = ImmutableDictionary.CreateBuilder<LanguageKind, Lazy<ImmutableDictionary<CodeFixProvider, int>>>();
foreach (var (diagnosticId, lazyFixers) in _fixersPerLanguageMap)
foreach (var (diagnosticId, lazyFixers) in _fixersPerLanguageMap.Value)
{
var lazyMap = new Lazy<ImmutableDictionary<CodeFixProvider, int>>(() =>
{
Expand Down
Loading