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

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

Merged
merged 2 commits into from
Jun 25, 2024

Conversation

rbuckton
Copy link
Member

@rbuckton rbuckton commented Jun 19, 2024

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

@rbuckton rbuckton requested review from sandersn and iisaduan June 19, 2024 16:14
@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jun 19, 2024
@rbuckton rbuckton merged commit e70904a into main Jun 25, 2024
28 checks passed
@rbuckton rbuckton deleted the deterministic-collapse-of-await-nullish branch June 25, 2024 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Explicit Resource Management normative changes for April 2024
4 participants