Skip to content

Use ValueTask<T> on allocation hot paths#23557

Merged
sharwell merged 6 commits intodotnet:masterfrom
sharwell:use-valuetask
Apr 26, 2018
Merged

Use ValueTask<T> on allocation hot paths#23557
sharwell merged 6 commits intodotnet:masterfrom
sharwell:use-valuetask

Conversation

@sharwell
Copy link
Copy Markdown
Contributor

@sharwell sharwell commented Dec 4, 2017

AnalyzerRunner indicated that these asynchronous paths typically complete synchronously. Measured savings are ~2.5% (~2GiB).

Ask Mode template not completed

Customer scenario

What does the customer do to get into this situation, and why do we think this
is common enough to address for this release. (Granted, sometimes this will be
obvious "Open project, VS crashes" but in general, I need to understand how
common a scenario is)

Bugs this fixes

(either VSO or GitHub links)

Workarounds, if any

Also, why we think they are insufficient for RC vs. RC2, RC3, or RTW

Risk

This is generally a measure our how central the affected code is to adjacent
scenarios and thus how likely your fix is to destabilize a broader area of code

Performance impact

(with a brief justification for that assessment (e.g. "Low perf impact because no extra allocations/no complexity changes" vs. "Low")

Is this a regression from a previous update?

Root cause analysis

How did we miss it? What tests are we adding to guard against it in the future?

How was the bug found?

(E.g. customer reported it vs. ad hoc testing)

Test documentation updated?

If this is a new non-compiler feature or a significant improvement to an existing feature, update https://github.com/dotnet/roslyn/wiki/Manual-Testing once you know which release it is targeting.

@sharwell sharwell requested review from a team as code owners December 4, 2017 15:07
Copy link
Copy Markdown
Member

@jasonmalinowski jasonmalinowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might need more changes and confirmation about RPS.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need updates to the VisualStudioSetup.Dependencies package? Will it ship a new façade that we need to be ready for in RPS?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(VS integration test failures imply yes, this needs more façade updates.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and binding redirects.

@sharwell sharwell changed the title Use ValueTask<T> on allocation hot paths [WIP] Use ValueTask<T> on allocation hot paths Dec 4, 2017
@sharwell
Copy link
Copy Markdown
Contributor Author

sharwell commented Dec 4, 2017

@jasonmalinowski I meant to mark this one as a work in progress, to be revisited after merging the other allocation-reducing changes that don't require changes to dependencies.

@sharwell sharwell added the PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it. label Dec 4, 2017
@jasonmalinowski
Copy link
Copy Markdown
Member

@sharwell Makes sense. No problem with this going in assuming the paperwork is done though. It'll be good to start getting ValueTask in here.

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

How are you figuring out which paths to ValueTaskisfy? It seems like thsi would be useful across a lot of the LS. But it's unclear how to drive that, and my concern would be regressing some path that actually needs real async.

@sharwell
Copy link
Copy Markdown
Contributor Author

sharwell commented Dec 4, 2017

How are you figuring out which paths to ValueTaskisfy?

In this case, I'm using AnalyzerRunner and looking for Task<T> allocations where the async state machine method SetResult is called by Start.

@sharwell sharwell removed the PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it. label Dec 7, 2017
@sharwell
Copy link
Copy Markdown
Contributor Author

sharwell commented Dec 8, 2017

@jasonmalinowski My original evaluation of allocation savings understated the value of this pull request by a factor of at least 4.

@sharwell sharwell requested review from a team December 8, 2017 17:01
@jaredpar
Copy link
Copy Markdown
Member

jaredpar commented Dec 8, 2017

We need to make sure that we discuss the RPS implications of this with @MattGertz. This is a new library reference and very likely to be loading in our measured scenarios.

@sharwell
Copy link
Copy Markdown
Contributor Author

sharwell commented Dec 8, 2017

@jaredpar Yes. I plan to update this pull request as issues come to light until it gets to a point that we can claim a justifiable win. However, I'm having all sorts of problems just getting the tests to pass with it.

@sharwell sharwell force-pushed the use-valuetask branch 2 times, most recently from c07e78a to 7f4106c Compare December 11, 2017 16:10
Adds System.Threading.Tasks.Extensions as a dependency.
@sharwell sharwell changed the title [WIP] Use ValueTask<T> on allocation hot paths Use ValueTask<T> on allocation hot paths Dec 11, 2017
@jinujoseph jinujoseph added this to the 15.7 milestone Jan 30, 2018
@sharwell
Copy link
Copy Markdown
Contributor Author

sharwell commented Mar 1, 2018

Based on the size of this added library (26KiB), I think we have reached a point where the wins may justify the inclusion.

/cc @jinujoseph @jasonmalinowski @jaredpar

Copy link
Copy Markdown
Member

@jaredpar jaredpar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to understand the binding redirect problem CPS hit better before we can merge this.

@jaredpar
Copy link
Copy Markdown
Member

jaredpar commented Mar 1, 2018

@tmat the other component to updating dependencies is getting permission from DevDiv. In this case @sharwell is trying to trade off memory savings for a new image reference. Need to clear that permission hurdle for Microsoft.DiaSymReader as well.

@tmat
Copy link
Copy Markdown
Member

tmat commented Mar 1, 2018

@jaredpar I understand. Just want to use this opportunity to reduce setup authoring work, if possible.

@Pilchie
Copy link
Copy Markdown
Member

Pilchie commented Mar 1, 2018

I sent some email offline.

Copy link
Copy Markdown
Member

@jasonmalinowski jasonmalinowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My signoff remains modulo whatever CLR problems exist which I'm not looped in on.

Do make sure when this merges that we've synced to be ready for the coming insertion -- these insertions often require extra steps; the last time we got surprised by one we caused a lot of chaos with delayed divisional branch snaps.


// do it async for real.
return DocumentState.GetSyntaxTreeAsync(cancellationToken);
return DocumentState.GetSyntaxTreeAsync(cancellationToken).AsTask();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably update src/NuGet/Microsoft.CodeAnalysis.Workspaces.Common.nuspec to state the dependency too. It's "fine" for now in that workspaces depends on common, and it depends on extensions, but if ever somebody "fixes" the core one to not depend on it, this is now broken.

@sharwell
Copy link
Copy Markdown
Contributor Author

sharwell commented Mar 3, 2018

@jasonmalinowski The problems appeared to involve ValueTuple, not ValueTask. We are already using ValueTuple without problems so whatever happened with the other project doesn't seem to be impacting us.

@sharwell
Copy link
Copy Markdown
Contributor Author

@jaredpar Any remaining issues from your side?

@jaredpar
Copy link
Copy Markdown
Member

@sharwell sorry forgot to remove my hold once we realized the issue was ValueTuple not ValueTask. I'm fine with this assuming that we get RPS buy off here as this will add a DLL load to some scenarios.

I'm less concerned about the binding redirects / dependency flow as well. Now that we use our own NuGet packages for bootstrapping you pretty much have to get that all right to pass Jenkins 😄

@jcouv jcouv added the Area-IDE label Apr 19, 2018
@jinujoseph jinujoseph modified the milestones: Unknown, 15.8 Apr 19, 2018
@sharwell sharwell changed the base branch from dev15.7.x to master April 19, 2018 18:44
@sharwell
Copy link
Copy Markdown
Contributor Author

@jaredpar I can't figure this one out:
https://ci.dot.net/job/dotnet_roslyn/job/master/job/microbuild_prtest/13086/

D:\j\workspace\microbuild_pr---d013602c\Binaries\Tools\dotnet\sdk\2.1.300-preview2-008324\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error : '' is not a valid version string. [D:\j\workspace\microbuild_pr---d013602c\src\Nuget\NuGetProjectPackUtil.csproj]
20:11:53 Command failed to execute: D:\j\workspace\microbuild_pr---d013602c\Binaries\Tools\dotnet\dotnet.exe pack -nologo --no-build D:\j\workspace\microbuild_pr---d013602c\src\Nuget\NuGetProjectPackUtil.csproj /p:EmptyDir=D:\j\workspace\microbuild_pr---d013602c\Binaries\Temp\v1hq0dcn.gp5 /p:NugetPackageKind=PreRelease /p:NuspecFile=D:\j\workspace\microbuild_pr---d013602c\src\Nuget\Microsoft.CodeAnalysis.Common.nuspec /p:NuspecBasePath=D:\j\workspace\microbuild_pr---d013602c\Binaries\Release -o D:\j\workspace\microbuild_pr---d013602c\Binaries\Release\NuGet\PreRelease
20:11:53 System.Management.Automation.RuntimeException: Command failed to execute: D:\j\workspace\microbuild_pr---d013602c\Binaries\Tools\dotnet\dotnet.exe pack -nologo --no-build D:\j\workspace\microbuild_pr---d013602c\src\Nuget\NuGetProjectPackUtil.csproj /p:EmptyDir=D:\j\workspace\microbuild_pr---d013602c\Binaries\Temp\v1hq0dcn.gp5 /p:NugetPackageKind=PreRelease /p:NuspecFile=D:\j\workspace\microbuild_pr---d013602c\src\Nuget\Microsoft.CodeAnalysis.Common.nuspec /p:NuspecBasePath=D:\j\workspace\microbuild_pr---d013602c\Binaries\Release -o D:\j\workspace\microbuild_pr---d013602c\Binaries\Release\NuGet\PreRelease

<PackageReference Include="System.Collections.Immutable" Version="$(SystemCollectionsImmutableVersion)" />
<PackageReference Include="System.Reflection.Metadata" Version="$(SystemReflectionMetadataVersion)" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="$(SystemTextEncodingCodePagesVersion)" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsVersion)" />

This comment was marked as resolved.

Now that System.Threading.Tasks.Extensions is referenced, the documentation can
contain direct links to ValueTask<TResult>.
@jaredpar
Copy link
Copy Markdown
Member

@sharwell need to add the version string for your new package here

https://github.com/dotnet/roslyn/blob/master/src/NuGet/NuGetProjectPackUtil.csproj#L93

The error message is definitely not helpful. I ran into it frequently when working with dotnet pack. I've filed the following issue on NuGet to make this clearer. For instance at least tell me the line number where the version string was wrong.

NuGet/Home#6806

@sharwell
Copy link
Copy Markdown
Contributor Author

sharwell commented Apr 20, 2018

@Pilchie for approval. This change trades a one-time 28KiB assembly load for the savings described at the top, and allows us to make similar changes moving forward.

@Pilchie
Copy link
Copy Markdown
Member

Pilchie commented Apr 20, 2018

I think this is probably worthwhile, but it will likely still need an exception. Let's wait for Monday, and loop in @MattGertz about the right time to take it (since merging now might block insertions of other changes to master).

@sharwell sharwell merged commit 7786ba4 into dotnet:master Apr 26, 2018
@sharwell sharwell deleted the use-valuetask branch April 26, 2018 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants