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

src: implement structuredClone in native #50330

Merged
merged 4 commits into from
Oct 25, 2023

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Oct 22, 2023

Simplify the implementation by implementing it directly in C++. This improves performance and also makes structuredClone supported in custom snapshots.

Local benchmark numbers:

                                                    confidence improvement accuracy (*)   (**)  (***)
misc/structured-clone.js n=10000 type='arraybuffer'        ***     12.79 %       ±1.72% ±2.29% ±2.99%
misc/structured-clone.js n=10000 type='object'             ***      7.65 %       ±2.22% ±2.96% ±3.85%
misc/structured-clone.js n=10000 type='string'             ***     75.01 %       ±3.84% ±5.14% ±6.75%

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/net
  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Oct 22, 2023
@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 22, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 22, 2023
@nodejs-github-bot
Copy link
Collaborator

@KhafraDev
Copy link
Member

will this fix #49940 too?

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

Am I correct in assuming this will improve all messaging through worker threads?

@benjamingr
Copy link
Member

@mcollina no, if you look at the changes this instead of creating a MessageChannel object in JS land creates it in C++ land - basically moving that part from JS to C++, see https://github.com/nodejs/node/pull/50330/files#diff-512ab8348327408c9a5ba3f7c60d8f8977a672a22b96345b8cfc2562936062f5R1582-R1583

@joyeecheung
Copy link
Member Author

joyeecheung commented Oct 24, 2023

FYI this is not yet ready because I want to see if we can just avoid the message ports all together without doing a lot of refactoring (there are a lot of code in place to check that the port isn't in the arguments, which then rely on having a port instance to check on) - if not, I'll just try getting this in first before following up with a PR to get rid of the message ports

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 24, 2023
@joyeecheung
Copy link
Member Author

joyeecheung commented Oct 24, 2023

From what I can tell simply using the existing Message::Serialize/Message::Deserialize should yield equivalent results. This should also get rid of the previous peculiar behavior of emitting deserialization errors as messageerror events instead of throwing them directly.

@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 24, 2023
@nodejs-github-bot
Copy link
Collaborator

@joyeecheung
Copy link
Member Author

will this fix #49940 too?

I don't think so, the assertion comes from the ReadIterable implementation and that's still unfixed - on the other hand it might be better to just implement that method in JS land and do a glue of structuredClone with that method. This calls into JS so many times that doing it in C++ would be slower. I can do that as a follow-up.

@joyeecheung joyeecheung marked this pull request as ready for review October 24, 2023 13:24
@joyeecheung
Copy link
Member Author

I removed the messaging namespace rename (this should still be done in another PR though) and the BindingData because it now no longer relies on MessagePorts. The benchmark numbers are improved a bit too due to the removal of unnecessary hoops:

                                                    confidence improvement accuracy (*)   (**)  (***)
misc/structured-clone.js n=10000 type='arraybuffer'        ***     12.79 %       ±1.72% ±2.29% ±2.99%
misc/structured-clone.js n=10000 type='object'             ***      7.65 %       ±2.22% ±2.96% ±3.85%
misc/structured-clone.js n=10000 type='string'             ***     75.01 %       ±3.84% ±5.14% ±6.75%

This should be ready for review now - I'd appreciate another pair of eyeballs to confirm that this still yields equivalent results.

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 24, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 24, 2023
@nodejs-github-bot
Copy link
Collaborator

Simplify the implementation by implementing it directly in C++.
This improves performance and also makes structuredClone supported
in custom snapshots.
Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

Can you please update the message in the linter error:

node/lib/.eslintrc.yaml

Lines 174 to 175 in fd21429

- name: structuredClone
message: Use `const { structuredClone } = require('internal/structured_clone');` instead of the global.

lib/.eslintrc.yaml Outdated Show resolved Hide resolved
@aduh95 aduh95 removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 25, 2023
@nodejs-github-bot
Copy link
Collaborator

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 25, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 25, 2023
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@joyeecheung joyeecheung added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Oct 25, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 25, 2023
@nodejs-github-bot nodejs-github-bot merged commit c3a41d8 into nodejs:main Oct 25, 2023
56 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in c3a41d8

alexfernandez pushed a commit to alexfernandez/node that referenced this pull request Nov 1, 2023
Simplify the implementation by implementing it directly in C++.
This improves performance and also makes structuredClone supported
in custom snapshots.

PR-URL: nodejs#50330
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
targos pushed a commit that referenced this pull request Nov 11, 2023
Simplify the implementation by implementing it directly in C++.
This improves performance and also makes structuredClone supported
in custom snapshots.

PR-URL: #50330
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
UlisesGascon pushed a commit that referenced this pull request Dec 11, 2023
Simplify the implementation by implementing it directly in C++.
This improves performance and also makes structuredClone supported
in custom snapshots.

PR-URL: #50330
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
@UlisesGascon UlisesGascon mentioned this pull request Dec 12, 2023
nodejs-github-bot pushed a commit that referenced this pull request Oct 13, 2024
This commit lets `tranfer` passed to `structuredClone` get validated at
JS layer by doing webidl conversion. This avoids the C++ to JS function
call overhead in the native implementaiton of `structuredClone`

PR-URL: #55317
Fixes: #55280
Refs: #50330
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
marco-ippolito pushed a commit to marco-ippolito/node that referenced this pull request Oct 14, 2024
This commit lets `tranfer` passed to `structuredClone` get validated at
JS layer by doing webidl conversion. This avoids the C++ to JS function
call overhead in the native implementaiton of `structuredClone`

PR-URL: nodejs#55317
Fixes: nodejs#55280
Refs: nodejs#50330
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
aduh95 pushed a commit that referenced this pull request Oct 19, 2024
This commit lets `tranfer` passed to `structuredClone` get validated at
JS layer by doing webidl conversion. This avoids the C++ to JS function
call overhead in the native implementaiton of `structuredClone`

PR-URL: #55317
Fixes: #55280
Refs: #50330
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
louwers pushed a commit to louwers/node that referenced this pull request Nov 2, 2024
This commit lets `tranfer` passed to `structuredClone` get validated at
JS layer by doing webidl conversion. This avoids the C++ to JS function
call overhead in the native implementaiton of `structuredClone`

PR-URL: nodejs#55317
Fixes: nodejs#55280
Refs: nodejs#50330
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
tpoisseau pushed a commit to tpoisseau/node that referenced this pull request Nov 21, 2024
This commit lets `tranfer` passed to `structuredClone` get validated at
JS layer by doing webidl conversion. This avoids the C++ to JS function
call overhead in the native implementaiton of `structuredClone`

PR-URL: nodejs#55317
Fixes: nodejs#55280
Refs: nodejs#50330
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants