Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.PooledObjects;
Expand Down Expand Up @@ -68,14 +69,35 @@ private static int GetPriority(DiagnosticAnalyzer state)
}
}

private static readonly ConditionalWeakTable<Project, StrongBox<ImmutableArray<DiagnosticAnalyzer>>> s_projectToAnalyzers = new();

/// <summary>
/// Return <see cref="DiagnosticAnalyzer"/>s for the given <see cref="Project"/>.
/// </summary>
internal ImmutableArray<DiagnosticAnalyzer> GetProjectAnalyzers(Project project)
public ImmutableArray<DiagnosticAnalyzer> GetProjectAnalyzers(Project project)
{
var hostAnalyzerInfo = GetOrCreateHostAnalyzerInfo(project);
var projectAnalyzerInfo = GetOrCreateProjectAnalyzerInfo(project);
return hostAnalyzerInfo.OrderedAllAnalyzers.AddRange(projectAnalyzerInfo.Analyzers);
if (!s_projectToAnalyzers.TryGetValue(project, out var lazyAnalyzers))
{
lazyAnalyzers = new(ComputeProjectAnalyzers());
#if NET
s_projectToAnalyzers.TryAdd(project, lazyAnalyzers);
#else
lock (s_projectToAnalyzers)
{
if (!s_projectToAnalyzers.TryGetValue(project, out var existing))
s_projectToAnalyzers.Add(project, lazyAnalyzers);
}
#endif
}

return lazyAnalyzers.Value;

ImmutableArray<DiagnosticAnalyzer> ComputeProjectAnalyzers()
{
var hostAnalyzerInfo = GetOrCreateHostAnalyzerInfo(project);
var projectAnalyzerInfo = GetOrCreateProjectAnalyzerInfo(project);
return hostAnalyzerInfo.OrderedAllAnalyzers.AddRange(projectAnalyzerInfo.Analyzers);
}
}

private HostAnalyzerInfo GetOrCreateHostAnalyzerInfo(Project project)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.PooledObjects;

namespace Microsoft.CodeAnalysis.Diagnostics;

internal sealed partial class DiagnosticAnalyzerService
{
private static readonly ConditionalWeakTable<DiagnosticAnalyzer, StrongBox<bool>> s_analyzerToIsDeprioritizationCandidateMap = new();

private async Task<ImmutableArray<DiagnosticAnalyzer>> GetDeprioritizationCandidatesInProcessAsync(
Project project, ImmutableArray<DiagnosticAnalyzer> analyzers, CancellationToken cancellationToken)
{
using var _ = ArrayBuilder<DiagnosticAnalyzer>.GetInstance(out var builder);

HostAnalyzerInfo? hostAnalyzerInfo = null;
CompilationWithAnalyzersPair? compilationWithAnalyzers = null;

foreach (var analyzer in analyzers)
{
if (!s_analyzerToIsDeprioritizationCandidateMap.TryGetValue(analyzer, out var boxedBool))
{
if (hostAnalyzerInfo is null)
{
hostAnalyzerInfo = GetOrCreateHostAnalyzerInfo(project);
compilationWithAnalyzers = await GetOrCreateCompilationWithAnalyzersAsync(
project, analyzers, hostAnalyzerInfo, this.CrashOnAnalyzerException, cancellationToken).ConfigureAwait(false);
}

boxedBool = new(await IsCandidateForDeprioritizationBasedOnRegisteredActionsAsync(analyzer).ConfigureAwait(false));
#if NET
s_analyzerToIsDeprioritizationCandidateMap.TryAdd(analyzer, boxedBool);
#else
lock (s_analyzerToIsDeprioritizationCandidateMap)
{
if (!s_analyzerToIsDeprioritizationCandidateMap.TryGetValue(analyzer, out var existing))
s_analyzerToIsDeprioritizationCandidateMap.Add(analyzer, boxedBool);
}
#endif
}

if (boxedBool.Value)
builder.Add(analyzer);
}

return builder.ToImmutableAndClear();

async Task<bool> IsCandidateForDeprioritizationBasedOnRegisteredActionsAsync(DiagnosticAnalyzer analyzer)
{
// We deprioritize SymbolStart/End and SemanticModel analyzers from 'Normal' to 'Low' priority bucket,
// as these are computationally more expensive.
// Note that we never de-prioritize compiler analyzer, even though it registers a SemanticModel action.
if (compilationWithAnalyzers == null ||
analyzer.IsWorkspaceDiagnosticAnalyzer() ||
analyzer.IsCompilerAnalyzer())
{
return false;
}

var telemetryInfo = await compilationWithAnalyzers.GetAnalyzerTelemetryInfoAsync(analyzer, cancellationToken).ConfigureAwait(false);
if (telemetryInfo == null)
return false;

return telemetryInfo.SymbolStartActionsCount > 0 || telemetryInfo.SemanticModelActionsCount > 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,38 +118,7 @@ public async Task<ImmutableArray<DiagnosticAnalyzer>> GetDeprioritizationCandida
return analyzers.FilterAnalyzers(result.Value);
}

using var _ = ArrayBuilder<DiagnosticAnalyzer>.GetInstance(out var builder);

var hostAnalyzerInfo = GetOrCreateHostAnalyzerInfo(project);
var compilationWithAnalyzers = await GetOrCreateCompilationWithAnalyzersAsync(
project, analyzers, hostAnalyzerInfo, this.CrashOnAnalyzerException, cancellationToken).ConfigureAwait(false);

foreach (var analyzer in analyzers)
{
if (await IsCandidateForDeprioritizationBasedOnRegisteredActionsAsync(analyzer).ConfigureAwait(false))
builder.Add(analyzer);
}

return builder.ToImmutableAndClear();

async Task<bool> IsCandidateForDeprioritizationBasedOnRegisteredActionsAsync(DiagnosticAnalyzer analyzer)
{
// We deprioritize SymbolStart/End and SemanticModel analyzers from 'Normal' to 'Low' priority bucket,
// as these are computationally more expensive.
// Note that we never de-prioritize compiler analyzer, even though it registers a SemanticModel action.
if (compilationWithAnalyzers == null ||
analyzer.IsWorkspaceDiagnosticAnalyzer() ||
analyzer.IsCompilerAnalyzer())
{
return false;
}

var telemetryInfo = await compilationWithAnalyzers.GetAnalyzerTelemetryInfoAsync(analyzer, cancellationToken).ConfigureAwait(false);
if (telemetryInfo == null)
return false;

return telemetryInfo.SymbolStartActionsCount > 0 || telemetryInfo.SemanticModelActionsCount > 0;
}
return await GetDeprioritizationCandidatesInProcessAsync(project, analyzers, cancellationToken).ConfigureAwait(false);
}

internal async Task<ImmutableArray<DiagnosticData>> ProduceProjectDiagnosticsAsync(
Expand Down Expand Up @@ -202,23 +171,28 @@ public async Task<ImmutableArray<DiagnosticData>> ComputeDiagnosticsAsync(
if (allAnalyzers.Length == 0)
return [];

var client = await RemoteHostClient.TryGetClientAsync(document.Project, cancellationToken).ConfigureAwait(false);
if (client is not null)
{
var allAnalyzerIds = allAnalyzers.Select(a => a.GetAnalyzerId()).ToImmutableHashSet();
var syntaxAnalyzersIds = syntaxAnalyzers.Select(a => a.GetAnalyzerId()).ToImmutableHashSet();
var semanticSpanAnalyzersIds = semanticSpanAnalyzers.Select(a => a.GetAnalyzerId()).ToImmutableHashSet();
var semanticDocumentAnalyzersIds = semanticDocumentAnalyzers.Select(a => a.GetAnalyzerId()).ToImmutableHashSet();

var result = await client.TryInvokeAsync<IRemoteDiagnosticAnalyzerService, ImmutableArray<DiagnosticData>>(
document.Project,
(service, solution, cancellationToken) => service.ComputeDiagnosticsAsync(
solution, document.Id, range,
allAnalyzerIds, syntaxAnalyzersIds, semanticSpanAnalyzersIds, semanticDocumentAnalyzersIds,
incrementalAnalysis, logPerformanceInfo, cancellationToken),
cancellationToken).ConfigureAwait(false);
var useOOP = false;

return result.HasValue ? result.Value : [];
if (useOOP)
{
var client = await RemoteHostClient.TryGetClientAsync(document.Project, cancellationToken).ConfigureAwait(false);
if (client is not null)
{
var allAnalyzerIds = allAnalyzers.Select(a => a.GetAnalyzerId()).ToImmutableHashSet();
var syntaxAnalyzersIds = syntaxAnalyzers.Select(a => a.GetAnalyzerId()).ToImmutableHashSet();
var semanticSpanAnalyzersIds = semanticSpanAnalyzers.Select(a => a.GetAnalyzerId()).ToImmutableHashSet();
var semanticDocumentAnalyzersIds = semanticDocumentAnalyzers.Select(a => a.GetAnalyzerId()).ToImmutableHashSet();

var result = await client.TryInvokeAsync<IRemoteDiagnosticAnalyzerService, ImmutableArray<DiagnosticData>>(
document.Project,
(service, solution, cancellationToken) => service.ComputeDiagnosticsAsync(
solution, document.Id, range,
allAnalyzerIds, syntaxAnalyzersIds, semanticSpanAnalyzersIds, semanticDocumentAnalyzersIds,
incrementalAnalysis, logPerformanceInfo, cancellationToken),
cancellationToken).ConfigureAwait(false);

return result.HasValue ? result.Value : [];
}
}

return await ComputeDiagnosticsInProcessAsync(
Expand Down
6 changes: 5 additions & 1 deletion src/Workspaces/Core/Portable/Diagnostics/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ public static ImmutableArray<DiagnosticAnalyzer> FilterAnalyzers(
analyzerMap[analyzerId] = analyzer;
}

return [.. analyzerMap.Values];
var result = new FixedSizeArrayBuilder<DiagnosticAnalyzer>(analyzerMap.Count);
foreach (var (_, analyzer) in analyzerMap)
result.Add(analyzer);

return result.MoveToImmutable();
}
}
Loading