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(runtime/worker): Structured cloning worker message passing. #9323

Merged
merged 14 commits into from
May 11, 2021
Merged

feat(runtime/worker): Structured cloning worker message passing. #9323

merged 14 commits into from
May 11, 2021

Conversation

inteon
Copy link
Contributor

@inteon inteon commented Jan 29, 2021

Refactored webworkers to listen for host messages via an "async bin op" instead of directly injecting JSON into the runtime.

Fixes #3557.

@CLAassistant
Copy link

CLAassistant commented Jan 29, 2021

CLA assistant check
All committers have signed the CLA.

@bartlomieju
Copy link
Member

Refactored webworkers to listen for host messages via an "async json op" instead of directly injecting JSON into the runtime.
First steps towards #3557, main goal is to change the message passing, so instead of JSON SharedArrayBuffers can be used (not yet part of this pull request).
[WIP] because a waker still has to be re-added to poll_event_loop
↳ need some more time to figure out how this works (feel free to help)

Would be nice if some intermediate feedback could be provided already, on both the approach and the current code.

Thanks for the PR, but the current design is on purpose. I'd rather not change code before there's a working prototype of structured cloned message passing and currently we're still missing appropriate bindings in deno_core to do that (which is in my backlog).

NOTE: code also contains some unrelated fixes for linting

It's not a good idea to throw such unrelated changes into a PR. Besides current formatting and linting passes so I'm not sure how that's better.

@inteon
Copy link
Contributor Author

inteon commented Feb 2, 2021

Refactored webworkers to listen for host messages via an "async json op" instead of directly injecting JSON into the runtime.
First steps towards #3557, main goal is to change the message passing, so instead of JSON SharedArrayBuffers can be used (not yet part of this pull request).
[WIP] because a waker still has to be re-added to poll_event_loop
↳ need some more time to figure out how this works (feel free to help)
Would be nice if some intermediate feedback could be provided already, on both the approach and the current code.

Thanks for the PR, but the current design is on purpose. I'd rather not change code before there's a working prototype of structured cloned message passing and currently we're still missing appropriate bindings in deno_core to do that (which is in my backlog).

NOTE: code also contains some unrelated fixes for linting

It's not a good idea to throw such unrelated changes into a PR. Besides current formatting and linting passes so I'm not sure how that's better.

Thank you for the feedback! I reverted the linting changes. Also, I will change the goal of this PR to create a structured cloning prototype. Like you said, that will make it easier to discuss what can be done better/ differently in my approach. While still making structured cloning work.
Then, the prototype can be used as a starting point to create the needed PRs to implement this feature (probably will involve multiple PRs).

Will update soon.

@inteon inteon changed the title [WIP] refactor(runtime/worker): Use async json op to receive "Post message" sent from host to worker [WIP] feat(runtime/worker): Structured cloning worker message passing. Feb 2, 2021
@inteon
Copy link
Contributor Author

inteon commented Feb 2, 2021

@bartlomieju I pushed a full prototype, please let me know what you think.

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.

@inteon great work! There's a lot going on in this PR and it might be hard to land it in one go. Changes to core/ look great, there are a few minor comments I have.

I'm yet to properly go through changes to worker implementation; could you open a new PR with only changes to the core/ directory so we can land them first?

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

@inteon let me know if this PR is ready, I don't think we'll land it for 1.8 though

@inteon
Copy link
Contributor Author

inteon commented Feb 24, 2021

@inteon let me know if this PR is ready, I don't think we'll land it for 1.8 though

Indeed, let's not rush things. First lets make sure it works well.

@inteon inteon changed the title [WIP] feat(runtime/worker): Structured cloning worker message passing. feat(runtime/worker): Structured cloning worker message passing. Feb 28, 2021
@inteon inteon marked this pull request as ready for review February 28, 2021 17:58
@inteon inteon requested a review from bartlomieju March 20, 2021 20:52
runtime/js/01_errors.js Outdated Show resolved Hide resolved
runtime/js/99_main.js Outdated Show resolved Hide resolved
runtime/ops/web_worker.rs Outdated Show resolved Hide resolved
runtime/ops/web_worker.rs Outdated Show resolved Hide resolved
runtime/ops/web_worker.rs Outdated Show resolved Hide resolved
runtime/ops/worker_host.rs Outdated Show resolved Hide resolved
@bartlomieju bartlomieju added this to the 1.10.0 milestone Apr 16, 2021
@bartlomieju
Copy link
Member

@inteon please rebase when you get a moment, let's land this feature for 1.10

@bartlomieju bartlomieju modified the milestones: 1.10.0, 1.11.0 Apr 27, 2021
@inteon
Copy link
Contributor Author

inteon commented May 9, 2021

@bartlomieju
I finished rebasing the structured cloning message passing.
Additionally, the PR includes some worker related cleanup (changed wrong message in error & moved benchmark).
I also revised WebWorkerHandle and WebWorkerInternalHandle to give better separation between cross-tread and same-thread handles.

@inteon inteon requested a review from bartlomieju May 9, 2021 21:28
runtime/ops/web_worker.rs Outdated Show resolved Hide resolved
runtime/web_worker.rs Outdated Show resolved Hide resolved
runtime/web_worker.rs Outdated Show resolved Hide resolved
runtime/web_worker.rs Show resolved Hide resolved
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.

@inteon thank you so much for this PR, it's really great that you managed to implement all of that functionality in so little code. I have only some minor/nitpicky comments, other than that it's ready to land. Great to see this landed for 1.10!

runtime/js/11_workers.js Outdated Show resolved Hide resolved
runtime/js/11_workers.js Outdated Show resolved Hide resolved
runtime/js/99_main.js Outdated Show resolved Hide resolved
runtime/ops/web_worker.rs Outdated Show resolved Hide resolved
runtime/ops/worker_host.rs Show resolved Hide resolved
runtime/web_worker.rs Show resolved Hide resolved
runtime/web_worker.rs Show resolved Hide resolved
@inteon
Copy link
Contributor Author

inteon commented May 10, 2021

I optimized the worker <-> host message passing ops (removed all places where buffers were copied).
@AaronO I also made some small changes to serde_v8, please check if these are acceptable

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 as well ✅ thanks for seeing this through @inteon, this is a massive feature that was highly requested.

runtime/js/11_workers.js Show resolved Hide resolved
runtime/js/99_main.js Outdated Show resolved Hide resolved
runtime/js/99_main.js Outdated Show resolved Hide resolved
serde_v8/src/magic/buffer.rs Outdated Show resolved Hide resolved
serde_v8/src/magic/buffer.rs Outdated Show resolved Hide resolved
runtime/js/99_main.js Outdated Show resolved Hide resolved
runtime/web_worker.rs Outdated Show resolved Hide resolved
Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

LGTM - thank you much @inteon !

Copy link
Member

@piscisaureus piscisaureus left a comment

Choose a reason for hiding this comment

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

LGTM!

@bartlomieju bartlomieju merged commit 635253b into denoland:main May 11, 2021
@inteon inteon deleted the post_message branch March 4, 2022 10:45
hardfist pushed a commit to hardfist/deno that referenced this pull request Aug 7, 2024
…land#9323)

This commit upgrade "Worker.postMessage()" implementation to use 
structured clone algorithm instead of non-spec compliant JSON serialization.
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.

Implement proper structured cloning between Workers
8 participants