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

Reduce allocations in ProjectState's ctor #76369

Merged
merged 2 commits into from
Dec 11, 2024
Merged
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
49 changes: 37 additions & 12 deletions src/Workspaces/Core/Portable/Workspace/Solution/ProjectState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
Expand All @@ -12,7 +11,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.Host;
Expand Down Expand Up @@ -49,10 +47,11 @@ internal sealed partial class ProjectState
private readonly AsyncLazy<VersionStamp> _lazyLatestDocumentVersion;
private readonly AsyncLazy<VersionStamp> _lazyLatestDocumentTopLevelChangeVersion;

// Checksums for this solution state
private readonly AsyncLazy<ProjectStateChecksums> _lazyChecksums;
// Checksums for this solution state (access via LazyChecksums)
private AsyncLazy<ProjectStateChecksums>? _lazyChecksums;

private readonly AsyncLazy<Dictionary<ImmutableArray<byte>, DocumentId>> _lazyContentHashToDocumentId;
// Mapping from content has to document id (access via LazyContentHashToDocumentId)
private AsyncLazy<Dictionary<ImmutableArray<byte>, DocumentId>>? _lazyContentHashToDocumentId;

/// <summary>
/// Analyzer config options to be used for specific trees.
Expand Down Expand Up @@ -87,9 +86,6 @@ private ProjectState(
// holding on. otherwise, these information will be held onto unnecessarily by projectInfo even after
// the info has changed by DocumentState.
ProjectInfo = ClearAllDocumentsFromProjectInfo(projectInfo);

_lazyChecksums = AsyncLazy.Create(static (self, cancellationToken) => self.ComputeChecksumsAsync(cancellationToken), arg: this);
_lazyContentHashToDocumentId = AsyncLazy.Create(static (self, cancellationToken) => self.ComputeContentHashToDocumentIdAsync(cancellationToken), arg: this);
}

public ProjectState(LanguageServices languageServices, ProjectInfo projectInfo, StructuredAnalyzerConfigOptions fallbackAnalyzerOptions)
Expand Down Expand Up @@ -128,9 +124,6 @@ public ProjectState(LanguageServices languageServices, ProjectInfo projectInfo,
// the info has changed by DocumentState.
// we hold onto the info so that we don't need to duplicate all information info already has in the state
ProjectInfo = ClearAllDocumentsFromProjectInfo(projectInfoFixed);

_lazyChecksums = AsyncLazy.Create(static (self, cancellationToken) => self.ComputeChecksumsAsync(cancellationToken), arg: this);
_lazyContentHashToDocumentId = AsyncLazy.Create(static (self, cancellationToken) => self.ComputeContentHashToDocumentIdAsync(cancellationToken), arg: this);
}

public TextDocumentStates<TDocumentState> GetDocumentStates<TDocumentState>()
Expand Down Expand Up @@ -158,6 +151,38 @@ private async Task<Dictionary<ImmutableArray<byte>, DocumentId>> ComputeContentH
return result;
}

private AsyncLazy<ProjectStateChecksums> LazyChecksums
{
get
{
if (_lazyChecksums is null)
{
Interlocked.CompareExchange(
ref _lazyChecksums,
AsyncLazy.Create(static (self, cancellationToken) => self.ComputeChecksumsAsync(cancellationToken), arg: this),
null);
}

return _lazyChecksums;
}
}

private AsyncLazy<Dictionary<ImmutableArray<byte>, DocumentId>> LazyContentHashToDocumentId
{
get
{
if (_lazyContentHashToDocumentId is null)
{
Interlocked.CompareExchange(
ref _lazyContentHashToDocumentId,
AsyncLazy.Create(static (self, cancellationToken) => self.ComputeContentHashToDocumentIdAsync(cancellationToken), arg: this),
null);
}

return _lazyContentHashToDocumentId;
}
}

private static ProjectInfo ClearAllDocumentsFromProjectInfo(ProjectInfo projectInfo)
{
return projectInfo
Expand All @@ -168,7 +193,7 @@ private static ProjectInfo ClearAllDocumentsFromProjectInfo(ProjectInfo projectI

public async ValueTask<DocumentId?> GetDocumentIdAsync(ImmutableArray<byte> contentHash, CancellationToken cancellationToken)
{
var map = await _lazyContentHashToDocumentId.GetValueAsync(cancellationToken).ConfigureAwait(false);
var map = await LazyContentHashToDocumentId.GetValueAsync(cancellationToken).ConfigureAwait(false);
return map.TryGetValue(contentHash, out var documentId) ? documentId : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ namespace Microsoft.CodeAnalysis;
internal sealed partial class ProjectState
{
public bool TryGetStateChecksums([NotNullWhen(true)] out ProjectStateChecksums? stateChecksums)
=> _lazyChecksums.TryGetValue(out stateChecksums);
=> LazyChecksums.TryGetValue(out stateChecksums);

public Task<ProjectStateChecksums> GetStateChecksumsAsync(CancellationToken cancellationToken)
=> _lazyChecksums.GetValueAsync(cancellationToken);
=> LazyChecksums.GetValueAsync(cancellationToken);

public Task<Checksum> GetChecksumAsync(CancellationToken cancellationToken)
{
return SpecializedTasks.TransformWithoutIntermediateCancellationExceptionAsync(
static (lazyChecksums, cancellationToken) => new ValueTask<ProjectStateChecksums>(lazyChecksums.GetValueAsync(cancellationToken)),
static (projectStateChecksums, _) => projectStateChecksums.Checksum,
_lazyChecksums,
LazyChecksums,
cancellationToken).AsTask();
}

Expand Down
Loading