Skip to content

Implement deterministic collapse of 'await' in 'await using' - #58929

Merged
Ron Buckton (rbuckton) merged 2 commits into
mainfrom
deterministic-collapse-of-await-nullish
Jun 25, 2024
Merged

Implement deterministic collapse of 'await' in 'await using'#58929
Ron Buckton (rbuckton) merged 2 commits into
mainfrom
deterministic-collapse-of-await-nullish

Conversation

@rbuckton

@rbucktonRon Buckton (rbuckton) commented Jun 19, 2024

Copy link
Copy Markdown
Contributor

This updates the __disposeResources helper to align with a recent change to the DisposeResources algorithm that reached consensus in the June TC39 meeting. It has the effect of reducing extraneous Await operations for cases like the following:

{await using x=null,y=null,z=null;HAPPENS_BEFORE();}HAPPENS_AFTER();

It is a requirement that an await using declaration must trigger an Await when exiting the block such that HAPPENS_BEFORE() and HAPPENS_AFTER() occur in different turns, even if the resource recorded was null/undefined. Prior to this change, the DisposeResources algorithm would Await for each null/undefined in an await using, which could introduce significant delays to function execution. Rather than Await-ing three times in the above example, we can collapse these down to a single Await instead to preserve the aforementioned requirement.

I should note that our downlevel emit is not as efficient as a native implementation since we must await the result of __disposeResources, which introduces 1-2 turn overhead as it adopts the state of the resulting Promise. The only way we could make that more efficient would be to inline the behavior __disposeResources into the finally block, which would dramatically increase emit size.

For more context as to the effects of the DisposeResources algorithm as a result of this change, see tc39/proposal-explicit-resource-management#219 (comment)

As this changes our helpers, you can find a companion PR for tslib here: microsoft/tslib#262

Fixes#58876

Comment threadsrc/testRunner/unittests/evaluation/usingDeclarations.ts Outdated
@rbuckton
Ron Buckton (rbuckton) deleted the deterministic-collapse-of-await-nullish branch June 25, 2024 19:28
@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: TeamFor Milestone BugPRs that fix a bug with a specific milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explicit Resource Management normative changes for April 2024

5 participants

@rbuckton@jakebailey@iisaduan@sandersn@typescript-bot