Repository: dotnet/roslyn
File: src/VisualStudio/Core/Def/TaskList/ExternalErrorDiagnosticUpdateSource.cs (C#)

Code before:

    private InProgressState? ClearInProgressState()
    {
        lock (_gate)
        {
            var state = _stateDoNotAccessDirectly;

            _stateDoNotAccessDirectly = null;
            return state;
        }
    }

    private InProgressState GetOrCreateInProgressState()
    {
        lock (_gate)
        {
            // We take current snapshot of solution when the state is first created. and through out this code, we use this snapshot.
            // Since we have no idea what actual snapshot of solution the out of proc build has picked up, it doesn't remove the race we can have
            // between build and diagnostic service, but this at least make us to consistent inside of our code.
            _stateDoNotAccessDirectly ??= new InProgressState(_workspace.CurrentSolution);
            return _stateDoNotAccessDirectly;
        }
    }

    private sealed class InProgressState(Solution solution)
    {
        /// <summary>
        /// Map from project ID to all the possible analyzer diagnostic IDs that can be reported in the project.
        /// </summary>
        private ImmutableDictionary<ProjectId, AsyncLazy<ImmutableHashSet<string>>> _allDiagnosticIdMap = ImmutableDictionary<ProjectId, AsyncLazy<ImmutableHashSet<string>>>.Empty;

        public Solution Solution { get; } = solution;

        public async Task<bool> IsSupportedDiagnosticIdAsync(ProjectId projectId, string id, CancellationToken cancellationToken)
        {
            var lazyIds = _allDiagnosticIdMap.TryGetValue(projectId, out var temp)
                ? temp
                : GetLazyIdsSlow();

            var ids = await lazyIds.GetValueAsync(cancellationToken).ConfigureAwait(false);
            return ids.Contains(id);

            AsyncLazy<ImmutableHashSet<string>> GetLazyIdsSlow()
            {
                return ImmutableInterlocked.GetOrAdd(ref _allDiagnosticIdMap, projectId, projectId => AsyncLazy.Create(async cancellationToken =>
                {
                    var project = Solution.GetProject(projectId);
                    if (project == null)
                    {
                        // projectId no longer exist
                        return [];

Review comment (performance):
It may be a practical question of whether this line is doing much -- I assume we create new InProgressStates, and this will force the cache to be initialized earlier; in a CPS-project only solution we'll fetch this and then never use it.

Code after:
    private InProgressState? ClearInProgressState()
    {
        lock (_gate)
        {
            var state = _stateDoNotAccessDirectly;

            _stateDoNotAccessDirectly = null;
            return state;
        }
    }

    private InProgressState GetOrCreateInProgressState()
    {
        lock (_gate)
        {
            // We take current snapshot of solution when the state is first created. and through out this code, we use this snapshot.
            // Since we have no idea what actual snapshot of solution the out of proc build has picked up, it doesn't remove the race we can have
            // between build and diagnostic service, but this at least make us to consistent inside of our code.
            _stateDoNotAccessDirectly ??= new InProgressState(_workspace.CurrentSolution);
            return _stateDoNotAccessDirectly;
        }
    }

    private sealed class InProgressState(Solution solution)
    {
        private readonly VisualStudioDiagnosticIdCache _diagnosticIdCache = solution.Workspace.Services.GetRequiredService<VisualStudioDiagnosticIdCache>();

        public Solution Solution { get; } = solution;

        public bool IsUnsupportedDiagnosticId(ProjectId projectId, string id)
        {
            var project = Solution.GetProject(projectId);
            if (project is null)
            {
                return true;
            }

            if (_diagnosticIdCache.TryGetDiagnosticIds(projectId, out var supportedIds))
            {
                return !supportedIds.Contains(id);
            }

            // The cache hasn't been populated yet. We will report false because we do not know
            // for certain that we do not support the diagnostic id.
            return false;
        }
    }
}