Skip to content

Conversation

rbuckton
Copy link
Contributor

@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
@rbuckton rbuckton deleted the deterministic-collapse-of-await-nullish branch June 25, 2024 19:28
@sandersn sandersn removed this from PR Backlog Apr 22, 2025
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
None yet
Development

Successfully merging this pull request may close these issues.

Explicit Resource Management normative changes for April 2024
4 participants