Skip to content

Add fast path for Promise creation#1387

Merged
saghul merged 1 commit intomasterfrom
promise-fast-path
Mar 7, 2026
Merged

Add fast path for Promise creation#1387
saghul merged 1 commit intomasterfrom
promise-fast-path

Conversation

@saghul
Copy link
Copy Markdown
Contributor

@saghul saghul commented Mar 6, 2026

When this is the builtin Promise constructor, call js_promise_new directly instead of
going through js_new_promise_capability which creates an unnecessary executor intermediary.

@bnoordhuis
Copy link
Copy Markdown
Contributor

Have you checked the spec whether that's sound?

I remember when promises got introduced in node (and bluebird still was a thing), that there are some corner cases where simply replacing globalThis.Promise results in user-observable changes in behavior but I don't remember the details.

test262 results may not be reliable for this; it doesn't appear to have coverage.

@saghul
Copy link
Copy Markdown
Contributor Author

saghul commented Mar 7, 2026

I'll check the spec, I admit I haven't.

Thanks for the insight!

@saghul
Copy link
Copy Markdown
Contributor Author

saghul commented Mar 7, 2026

I couldn't find an explanation in the spec, but since the Promise prototype is not writable and doesn't use @@species, I don't see how this change could be observed.

V8 seems to do the same trick, if I'm reading this correctly: https://github.com/v8/v8/blob/5fe0aa3bc79c0a9d3ad546b79211f07105f09585/src/builtins/promise-resolve.tq#L66

@saghul
Copy link
Copy Markdown
Contributor Author

saghul commented Mar 7, 2026

Looks like V8 also fast-paths .then, if the @@species chain is intact, I'll look into that for a follow-up PR. https://github.com/v8/v8/blob/f9e2258a2c6c7aa50372c496f75db8ddcecff25f/src/builtins/promise-then.tq#L42

Copy link
Copy Markdown
Contributor

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

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

Okay, LGTM then. One final question though: can't you move the this_val == ctx->promise_ctor inside js_new_promise_capability?

@saghul
Copy link
Copy Markdown
Contributor Author

saghul commented Mar 7, 2026

I can take a look but I think not because some functions do look at species so we'd also need to check for that.

Nevermind, you're right! It can be made simpler and all call sites for js_new_promise_capability benefit.

When `this` is the builtin Promise constructor, `js_new_promise_capability` will call `js_promise_new`,
which initializes a promise and resolving functions, without the unnecessary executor intermediary.
@saghul saghul force-pushed the promise-fast-path branch from d8a6ab0 to dc09bf8 Compare March 7, 2026 22:10
@saghul saghul changed the title Add fast path for Promise.{resolve,reject,try,withResolvers} Add fast path for Promise creation Mar 7, 2026
@saghul saghul merged commit 4951c83 into master Mar 7, 2026
122 checks passed
@saghul saghul deleted the promise-fast-path branch March 7, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants