Skip to content

WIP Async2 - #18873

Draft
majocha wants to merge 74 commits into
dotnet:mainfrom
majocha:async2
Draft

WIP Async2#18873
majocha wants to merge 74 commits into
dotnet:mainfrom
majocha:async2

Conversation

@majocha

@majochamajocha commented Aug 29, 2025

Copy link
Copy Markdown
Contributor

Currently just replaces cancellable.
Features: cold start, multi-start, cancellable, trampolined binds for safe non-tail recursion, fast tail-recursion with ReturnFromFinal, exceptions cached in ExceptionDispatchInfo for fast recovery from deep call stacks.

Synchronous executions should be quite fast and low alloc when it's done, but there are some fixes needed:
Currently complex mut rec expressions are not statically compiled, see the nowarn in CheckDeclarations.fs.

Very initial work.

@github-actions

github-actionsBot commented Aug 29, 2025

Copy link
Copy Markdown
Contributor

❗ Release notes required

You can open this PR in browser to add release notes: open in github.dev

@majocha,

Caution

No release notes found for the changed paths (see table below).

Please make sure to add an entry with an informative description of the change as well as link to this pull request, issue and language suggestion if applicable. Release notes for this repository are based on Keep A Changelog format.

The following format is recommended for this repository:

`* . (PR #XXXXX)`

See examples in the files, listed in the table below or in th full documentation at https://fsharp.github.io/fsharp-compiler-docs/release-notes/About.html.

If you believe that release notes are not necessary for this PR, please add NO_RELEASE_NOTES label to the pull request.

Change pathRelease notes pathDescription
`src/Compiler`docs/release-notes/.FSharp.Compiler.Service/11.0.100.mdNo release notes found or release notes format is not correct
`vsintegration/src`docs/release-notes/.VisualStudio/18.vNext.mdNo release notes found or release notes format is not correct

@vzarytovskii

Copy link
Copy Markdown
Member

To avoid confusion - it's not the same "async2" as in runtime-supported async. It's just different re-implementation of async, right?

@majocha

majocha commented Sep 1, 2025

Copy link
Copy Markdown
ContributorAuthor

To avoid confusion - it's not the same "async2" as in runtime-supported async. It's just different re-implementation of async, right?

Nope, no. I wasn't even aware this was a thing until now :) The goal is to possibly replace all the internal use of async and cancellable with one reasonably efficient resumable CE. I went with async2 name after the example in resumable code RFC.

LOL, probably before I ever finish this, the runtime async model will switch to https://github.com/dotnet/runtimelab/blob/feature/async2-experiment/docs/design/features/runtime-handled-tasks.md

I guess this is the fate, always catching up :)

@majocha

Copy link
Copy Markdown
ContributorAuthor

I updated more places where previously async (and node before) where in use. Particularly the GraphNode async lazy type.
F# inlining makes it hard to test the internal types. InternalsVisibleTo will not work in such case.
I had to actually link the relevant source files from the FCS to the test project. Async2.fs and BuildGraph.fs are now compiled also inside the test project to make the types accessible in tests.

Comment on lines +352 to +357
<Compile Include="..\..\src\Compiler\Utilities\Async2.fs">
<Link>CompilerService\Async2.fs</Link>
</Compile>
<Compile Include="..\..\src\Compiler\Facilities\AsyncMemoize.fs">
<Link>CompilerService\AsyncMemoImpl.fs</Link>
</Compile>

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Linked sources like these seem to bring down VS to a crawl.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This is probably related to in memory cross proj references. Project options manager goes into a loop of invalidating projects and type checks everything endlessly.

@majocha

majocha commented Sep 19, 2025

Copy link
Copy Markdown
ContributorAuthor

I made the accessibility public for the whole thing for now, dealing with inline internal stuff across test projects is pita.

One usability advantage over the normal async I notice: there are no weird edge cases wrt AsyncLocal diagnostics loggers now. That's because the execution is driven by AsyncTaskMethodBuilder, we can be sure everything within the braces of async2 { ... } is isolated wrt to execution context.

@majocha

majocha commented Sep 24, 2025

Copy link
Copy Markdown
ContributorAuthor

Implementing fast tail call with ReturnFromFinal is tricky While we can abandon intermediate computations in a tail call chain, we cannot abandon the first one, we must return to it, because it is the one actually awaited.

One possible solution is to thread a IValueTaskSource context between tail calls. We can bind to a ValueTask in the initial ReturnFromFinal, set the value in the last call of the chain, dropping all the intermediate computations.

@majocha

Copy link
Copy Markdown
ContributorAuthor

I went simply with TaskCompletionSource as a tail-call context. With ReturnFromFinal the overhead is now significantly lower.

Improve reduction of resumable code in state machines
Enhance application reduction in state machine lowering for F# computation expressions by tracking let-bound resumable code in the environment and resolving references during reduction. This enables correct handling of optimizer-generated continuations and deeper reduction of nested applications. Also, update test comments to reflect resolved state machine compilation issues.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

2 participants

@majocha@vzarytovskii