Skip to content

src: Fix inefficient usage of v8_inspector::StringView #52372

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

Merged

Conversation

szuend
Copy link
Contributor

@szuend szuend commented Apr 5, 2024

v8_inspector::StringView can either be one-byte or two-byte strings. Node has currently two places where it's unconditionally assumed that it's a two-byte StringView.

This requires the upstream V8 inspector to unnecessarily create a copy of the string:

https://source.chromium.org/chromium/chromium/src/+/main:v8/src/inspector/v8-inspector-session-impl.cc;l=192-199;drc=05bacd38e7a31e92afe0fd66081dfa2cc03b934a

This is particularly slow, especially for large CDP messages, as the serialized JSON is iterated 8-bit char by 8-bit char and each one widened to a 16-bit char.

This PR introduces a small helper that correctly converts a StringView to a v8::String instance honoring the "is8Bit" flag. This allows upstream V8 to remove the unnecessary widening copy.

Tested locally with removing the widening copy in v8-inspector-session-impl.cc: Without the PR lots of tests will fail. But I'm happy to add unit tests for the JSBindingsConnection/V8ProfilerConnection if desired.

@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 Apr 5, 2024
@szuend szuend force-pushed the fix-inspector-string-view-usage branch from 0d43fab to a4087c9 Compare April 5, 2024 05:43
@addaleax addaleax added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 8, 2024
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Apr 8, 2024
Copy link
Contributor

github-actions bot commented Apr 8, 2024

Failed to start CI
- Validating Jenkins credentials
✘  Jenkins credentials invalid
https://github.com./nodejs/node/actions/runs/8605010252

@szuend
Copy link
Contributor Author

szuend commented Apr 9, 2024

@addaleax Anything I need to address from my side for the failed CI run?

@panva panva added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. labels Apr 9, 2024
@addaleax addaleax added commit-queue Add this label to land a pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Apr 10, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 10, 2024
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@szuend
Copy link
Contributor Author

szuend commented Apr 15, 2024

I'm not too familiar with the node CI. Are the failing tests from the two CI runs generally known to be flaky or should I investigate?

@nodejs-github-bot
Copy link
Collaborator

@cola119 cola119 added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 16, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 16, 2024
@nodejs-github-bot
Copy link
Collaborator

@szuend szuend force-pushed the fix-inspector-string-view-usage branch from 66345c0 to f79c3cb Compare April 22, 2024 07:31
@szuend
Copy link
Contributor Author

szuend commented Apr 22, 2024

Rebased the PR after #51783 has landed.

FYI @joyeecheung

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

@nodejs-github-bot
Copy link
Collaborator

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

@BridgeAR
Copy link
Member

BridgeAR commented Apr 2, 2025

@szuend could you please rebase the branch? We may not have merge commits for the CI and it has to run once more for this to land. I believe it's otherwise good to land.

Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

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

After the latest update on the main branch, this needs an update to fix the build failure.

Thank you for the patch!

@szuend
Copy link
Contributor Author

szuend commented Apr 3, 2025

@szuend could you please rebase the branch? We may not have merge commits for the CI and it has to run once more for this to land. I believe it's otherwise good to land.

@BridgeAR the commit from the suggestion triggered another run. Is that enough? Otherwise I can squash + rebase this branch.

Copy link

codecov bot commented Apr 3, 2025

Codecov Report

Attention: Patch coverage is 35.29412% with 11 lines in your changes missing coverage. Please review.

Project coverage is 90.24%. Comparing base (657f818) to head (b144ac0).
Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
src/util-inl.h 40.00% 6 Missing and 3 partials ⚠️
src/inspector_js_api.cc 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #52372      +/-   ##
==========================================
+ Coverage   90.23%   90.24%   +0.01%     
==========================================
  Files         630      630              
  Lines      185017   185032      +15     
  Branches    36207    36222      +15     
==========================================
+ Hits       166948   166985      +37     
- Misses      11020    11029       +9     
+ Partials     7049     7018      -31     
Files with missing lines Coverage Δ
src/util.h 91.22% <ø> (ø)
src/inspector_js_api.cc 85.97% <0.00%> (-0.37%) ⬇️
src/util-inl.h 80.98% <40.00%> (-2.29%) ⬇️

... and 21 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@legendecas legendecas added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2025
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Apr 3, 2025
Copy link
Contributor

github-actions bot commented Apr 3, 2025

Failed to start CI
   ⚠  Commits were pushed since the last approving review:
   ⚠  - src: fix inefficient usage of v8_inspector::StringView
   ⚠  - Apply linter
   ⚠  - Rename function to ToV8Value and mirror std::string_view overload
   ⚠  - Merge branch 'main' into fix-inspector-string-view-usage
   ⚠  - Fix usage of UNLIKELY
   ✘  Refusing to run CI on potentially unsafe PR
https://github.com./nodejs/node/actions/runs/14238536161

@legendecas legendecas added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. labels Apr 3, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2025
@nodejs-github-bot
Copy link
Collaborator

@legendecas
Copy link
Member

legendecas commented Apr 3, 2025

@szuend I think the CI failed to due merge commits. Would you mind rebase onto the tip of the main branch? thanks!

v8_inspector::StringView can either be one-byte or two-byte strings.
Node has currently two places where it's unconditionally assumed that
it's a two-byte StringView.

This requires the upstream V8 inspector to unnecessarily create
a copy of the string:

https://source.chromium.org/chromium/chromium/src/+/main:v8/src/inspector/v8-inspector-session-impl.cc;l=192-199;drc=05bacd38e7a31e92afe0fd66081dfa2cc03b934a

This is particularly slow, especially for large CDP messages, as the
serialized JSON is iterated 8-bit char by 8-bit char and each one
widened to a 16-bit char.

This PR introduces a small helper that correctly converts a StringView
to a v8::String instance honoring the "is8Bit" flag. This allows
upstream V8 to remove the unnecessary widening copy.
@szuend szuend force-pushed the fix-inspector-string-view-usage branch from fe2ea75 to b144ac0 Compare April 3, 2025 10:26
@szuend
Copy link
Contributor Author

szuend commented Apr 3, 2025

@legendecas done! Thanks!

@legendecas legendecas added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2025
@nodejs-github-bot
Copy link
Collaborator

@legendecas legendecas added the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 3, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 3, 2025
@nodejs-github-bot nodejs-github-bot merged commit 264d031 into nodejs:main Apr 3, 2025
59 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 264d031

JonasBa pushed a commit to JonasBa/node that referenced this pull request Apr 11, 2025
v8_inspector::StringView can either be one-byte or two-byte strings.
Node has currently two places where it's unconditionally assumed that
it's a two-byte StringView.

This requires the upstream V8 inspector to unnecessarily create
a copy of the string:

https://source.chromium.org/chromium/chromium/src/+/main:v8/src/inspector/v8-inspector-session-impl.cc;l=192-199;drc=05bacd38e7a31e92afe0fd66081dfa2cc03b934a

This is particularly slow, especially for large CDP messages, as the
serialized JSON is iterated 8-bit char by 8-bit char and each one
widened to a 16-bit char.

This PR introduces a small helper that correctly converts a StringView
to a v8::String instance honoring the "is8Bit" flag. This allows
upstream V8 to remove the unnecessary widening copy.

PR-URL: nodejs#52372
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
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++. 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.

10 participants