Skip to content

Commit f3a26c9

Browse files
authored
Switch TextDocumentState.GetTextVersionAsync to ValueTask to reduce allocations. (#77213)
The task creation from calling this method accounts for 36 MB (0.2%) of allocations in the CSharpEditingTests.Completion speedometer test. Speedometer results with this change show marked improvement in these allocations.
1 parent c00b940 commit f3a26c9

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

src/Workspaces/Core/Portable/Workspace/Solution/ProjectState.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public ProjectState(LanguageServices languageServices, ProjectInfo projectInfo,
116116
DocumentStates = new TextDocumentStates<DocumentState>(projectInfoFixed.Documents, info => CreateDocument(info, parseOptions, loadTextOptions));
117117
AdditionalDocumentStates = new TextDocumentStates<AdditionalDocumentState>(projectInfoFixed.AdditionalDocuments, info => new AdditionalDocumentState(languageServices.SolutionServices, info, loadTextOptions));
118118

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

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

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

10911091
if (recalculateDocumentVersion)
10921092
{
1093-
dependentDocumentVersion = AsyncLazy.Create(static (arg, cancellationToken) =>
1094-
ComputeLatestDocumentVersionAsync(arg.newDocumentStates, arg.newAdditionalDocumentStates, cancellationToken),
1093+
dependentDocumentVersion = AsyncLazy.Create(static async (arg, cancellationToken) =>
1094+
await ComputeLatestDocumentVersionAsync(arg.newDocumentStates, arg.newAdditionalDocumentStates, cancellationToken).ConfigureAwait(false),
10951095
arg: (newDocumentStates, newAdditionalDocumentStates));
10961096
}
10971097
else if (contentChanged)

src/Workspaces/Core/Portable/Workspace/Solution/TextDocument.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ internal SourceText GetTextSynchronously(CancellationToken cancellationToken)
9090
/// <summary>
9191
/// Gets the version of the document's text.
9292
/// </summary>
93-
public Task<VersionStamp> GetTextVersionAsync(CancellationToken cancellationToken = default)
94-
=> State.GetTextVersionAsync(cancellationToken);
93+
public async Task<VersionStamp> GetTextVersionAsync(CancellationToken cancellationToken = default)
94+
=> await State.GetTextVersionAsync(cancellationToken).ConfigureAwait(false);
9595

9696
/// <summary>
9797
/// Fetches the current version for the document synchronously.

src/Workspaces/Core/Portable/Workspace/Solution/TextDocumentState.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public VersionStamp GetTextVersionSynchronously(CancellationToken cancellationTo
123123
return textAndVersion.Version;
124124
}
125125

126-
public async Task<VersionStamp> GetTextVersionAsync(CancellationToken cancellationToken)
126+
public async ValueTask<VersionStamp> GetTextVersionAsync(CancellationToken cancellationToken)
127127
{
128128
// try fast path first
129129
if (TryGetTextVersion(out var version))

0 commit comments

Comments
 (0)