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

Switch TextDocumentState.GetTextVersionAsync to ValueTask to reduce allocations. #77213

Merged
merged 1 commit into from
Feb 20, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public ProjectState(LanguageServices languageServices, ProjectInfo projectInfo,
DocumentStates = new TextDocumentStates<DocumentState>(projectInfoFixed.Documents, info => CreateDocument(info, parseOptions, loadTextOptions));
AdditionalDocumentStates = new TextDocumentStates<AdditionalDocumentState>(projectInfoFixed.AdditionalDocuments, info => new AdditionalDocumentState(languageServices.SolutionServices, info, loadTextOptions));

_lazyLatestDocumentVersion = AsyncLazy.Create(static (self, c) => ComputeLatestDocumentVersionAsync(self.DocumentStates, self.AdditionalDocumentStates, c), arg: this);
_lazyLatestDocumentVersion = AsyncLazy.Create(static async (self, c) => await ComputeLatestDocumentVersionAsync(self.DocumentStates, self.AdditionalDocumentStates, c).ConfigureAwait(false), arg: this);
_lazyLatestDocumentTopLevelChangeVersion = AsyncLazy.Create(static (self, c) => ComputeLatestDocumentTopLevelChangeVersionAsync(self.DocumentStates, self.AdditionalDocumentStates, c), arg: this);

// ownership of information on document has moved to project state. clear out documentInfo the state is
Expand Down Expand Up @@ -220,7 +220,7 @@ private ProjectInfo FixProjectInfo(ProjectInfo projectInfo)
return projectInfo;
}

private static async Task<VersionStamp> ComputeLatestDocumentVersionAsync(TextDocumentStates<DocumentState> documentStates, TextDocumentStates<AdditionalDocumentState> additionalDocumentStates, CancellationToken cancellationToken)
private static async ValueTask<VersionStamp> ComputeLatestDocumentVersionAsync(TextDocumentStates<DocumentState> documentStates, TextDocumentStates<AdditionalDocumentState> additionalDocumentStates, CancellationToken cancellationToken)
{
// this may produce a version that is out of sync with the actual Document versions.
var latestVersion = VersionStamp.Default;
Expand Down Expand Up @@ -1090,8 +1090,8 @@ private void GetLatestDependentVersions(

if (recalculateDocumentVersion)
{
dependentDocumentVersion = AsyncLazy.Create(static (arg, cancellationToken) =>
ComputeLatestDocumentVersionAsync(arg.newDocumentStates, arg.newAdditionalDocumentStates, cancellationToken),
dependentDocumentVersion = AsyncLazy.Create(static async (arg, cancellationToken) =>
await ComputeLatestDocumentVersionAsync(arg.newDocumentStates, arg.newAdditionalDocumentStates, cancellationToken).ConfigureAwait(false),
arg: (newDocumentStates, newAdditionalDocumentStates));
}
else if (contentChanged)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ internal SourceText GetTextSynchronously(CancellationToken cancellationToken)
/// <summary>
/// Gets the version of the document's text.
/// </summary>
public Task<VersionStamp> GetTextVersionAsync(CancellationToken cancellationToken = default)
=> State.GetTextVersionAsync(cancellationToken);
public async Task<VersionStamp> GetTextVersionAsync(CancellationToken cancellationToken = default)
=> await State.GetTextVersionAsync(cancellationToken).ConfigureAwait(false);

/// <summary>
/// Fetches the current version for the document synchronously.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public VersionStamp GetTextVersionSynchronously(CancellationToken cancellationTo
return textAndVersion.Version;
}

public async Task<VersionStamp> GetTextVersionAsync(CancellationToken cancellationToken)
public async ValueTask<VersionStamp> GetTextVersionAsync(CancellationToken cancellationToken)
{
// try fast path first
if (TryGetTextVersion(out var version))
Expand Down
Loading