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

feat(core): Reland support for async ops in realms #17204

Merged
merged 17 commits into from
Jan 14, 2023

Conversation

andreubotella
Copy link
Contributor

@andreubotella andreubotella commented Dec 28, 2022

Currently realms are supported on deno_core, but there was no support for async ops anywhere other than the main realm. The main issue is that the js_recv_cb callback, which resolves promises corresponding to async ops, was only set for the main realm, so async ops in other realms would never resolve. Furthermore, promise ID's are specific to each realm, which meant that async ops from other realms would result in a wrong promise from the main realm being resolved.

This change takes the ContextState struct added in #17050, and adds to it a js_recv_cb callback for each realm. Combined with the fact that that same PR also added a list of known realms to JsRuntimeState, and that #17174 made OpCtx instances realm-specific and had them include an index into that list of known realms, this makes it possible to know the current realm in the queue_async_op and queue_fast_async_op methods, and therefore to send the results of promises for each realm to that realm, and prevent the ID's from getting mixed up.

Additionally, since promise ID's are no longer unique to the isolate, having a single set of unrefed ops doesn't work. This change therefore also moves unrefed_ops from JsRuntimeState to ContextState, and adds the lengths of the unrefed op sets for all known realms to get the total number of unrefed ops to compare in the event loop.

This PR is a reland of #14734 after it was reverted in #16366, except that ContextState and JsRuntimeState::known_realms were previously relanded in #17050. Another significant difference with the original PR is passing around an index into JsRuntimeState::known_realms instead of a v8::Global<v8::Context> to identify the realm, because async op queuing in fast calls cannot call into V8, and therefore cannot have access to V8 globals. This also simplified the implementation of resolve_async_ops.


cc @lmalheiro, since the this reland includes a test they provided for the original PR.

andreubotella and others added 2 commits December 28, 2022 06:11
Currently realms are supported on `deno_core`, but there was no
support for async ops anywhere other than the main realm. The main
issue is that the `js_recv_cb` callback, which resolves promises
corresponding to async ops, was only set for the main realm, so async
ops in other realms would never resolve. Furthermore, promise ID's are
specific to each realm, which meant that async ops from other realms
would result in a wrong promise from the main realm being resolved.

This change takes the `ContextState` struct added in denoland#17050, and adds
to it a `js_recv_cb` callback for each realm. Combined with the fact
that that same PR also added a list of known realms to
`JsRuntimeState`, and that denoland#17174 made `OpCtx` instances
realm-specific and had them include an index into that list of known
realms, this makes it possible to know the current realm in the
`queue_async_op` and `queue_fast_async_op` methods, and therefore to
send the results of promises for each realm to that realm, and prevent
the ID's from getting mixed up.

Additionally, since promise ID's are no longer unique to the isolate,
having a single set of unrefed ops doesn't work. This change therefore
also moves `unrefed_ops` from `JsRuntimeState` to `ContextState`, and
adds the lengths of the unrefed op sets for all known realms to get
the total number of unrefed ops to compare in the event loop.

This PR is a reland of denoland#14734 after it was reverted in denoland#16366, except
that `ContextState` and `JsRuntimeState::known_realms` were previously
relanded in denoland#17050. Another significant difference with the original
PR is passing around an index into `JsRuntimeState::known_realms`
instead of a `v8::Global<v8::Context>` to identify the realm, because
async op queuing in fast calls cannot call into V8, and therefore
cannot have access to V8 globals. This also simplified the
implementation of `resolve_async_ops`.

Co-authored-by: Luis Malheiro <[email protected]>
@andreubotella
Copy link
Contributor Author

main:

test bench_op_async   ... bench:     576,647 ns/iter (+/- 35,813)

this PR:

test bench_op_async   ... bench:     613,201 ns/iter (+/- 32,075)

There seems to be a clear decrease in performance in this benchmark. Now, bench_op_async only goes through the ready and non-deferred match arm of queue_async_op, so this points finely enough at a perf issue in the change, but it also leaves the rest of branches unchecked. Are there some other specific async op benchmarks that I can use for the other codepaths?

@andreubotella
Copy link
Contributor Author

I got rid of a v8::Global clone (which was there before this PR) in the ready, non-deferred arm of queue_async_op and got the benchmark down. It's still noticeably higher than in the main branch though.

test bench_op_async   ... bench:     590,218 ns/iter (+/- 25,768)

@andreubotella
Copy link
Contributor Author

PTAL @bartlomieju @littledivy

core/runtime.rs Outdated Show resolved Hide resolved
core/runtime.rs Outdated Show resolved Hide resolved
@bartlomieju
Copy link
Member

@littledivy this looks good to me now - I think having a separate code path for non-default realm is a good idea.

@bartlomieju
Copy link
Member

Oooops, looks like #17301 causes the tests to fail, could you update them @andreubotella?

Copy link
Member

@littledivy littledivy left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM too, thanks Andreu

@bartlomieju bartlomieju merged commit 6878234 into denoland:main Jan 14, 2023
@andreubotella andreubotella deleted the realms-reland-async-ops branch January 14, 2023 13:41
bartlomieju pushed a commit that referenced this pull request Jan 16, 2023
Currently realms are supported on `deno_core`, but there was no support
for async ops anywhere other than the main realm. The main issue is that
the `js_recv_cb` callback, which resolves promises corresponding to
async ops, was only set for the main realm, so async ops in other realms
would never resolve. Furthermore, promise ID's are specific to each
realm, which meant that async ops from other realms would result in a
wrong promise from the main realm being resolved.

This change takes the `ContextState` struct added in #17050, and adds to
it a `js_recv_cb` callback for each realm. Combined with the fact that
that same PR also added a list of known realms to `JsRuntimeState`, and
that #17174 made `OpCtx` instances realm-specific and had them include
an index into that list of known realms, this makes it possible to know
the current realm in the `queue_async_op` and `queue_fast_async_op`
methods, and therefore to send the results of promises for each realm to
that realm, and prevent the ID's from getting mixed up.

Additionally, since promise ID's are no longer unique to the isolate,
having a single set of unrefed ops doesn't work. This change therefore
also moves `unrefed_ops` from `JsRuntimeState` to `ContextState`, and
adds the lengths of the unrefed op sets for all known realms to get the
total number of unrefed ops to compare in the event loop.

This PR is a reland of #14734 after it was reverted in #16366, except
that `ContextState` and `JsRuntimeState::known_realms` were previously
relanded in #17050. Another significant difference with the original PR
is passing around an index into `JsRuntimeState::known_realms` instead
of a `v8::Global<v8::Context>` to identify the realm, because async op
queuing in fast calls cannot call into V8, and therefore cannot have
access to V8 globals. This also simplified the implementation of
`resolve_async_ops`.

Co-authored-by: Luis Malheiro <[email protected]>
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.

3 participants