Skip to content

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

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
szuend:fix-inspector-string-view-usage
Apr 3, 2025
Merged

src: Fix inefficient usage of v8_inspector::StringView#52372
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
szuend:fix-inspector-string-view-usage

Conversation

@szuend

Copy link
Copy Markdown
Contributor

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-botnodejs-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
szuendforce-pushed the fix-inspector-string-view-usage branch from 0d43fab to a4087c9CompareApril 5, 2024 05:43
Comment threadsrc/util-inl.h Outdated
@addaleaxaddaleax added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 8, 2024
@github-actionsgithub-actionsBot 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
@github-actions

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

@szuend

Copy link
Copy Markdown
ContributorAuthor

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

@panvapanva 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
@addaleaxaddaleax 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-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 10, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@szuend

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
Collaborator

@cola119cola119 added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 16, 2024
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 16, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@szuend
szuendforce-pushed the fix-inspector-string-view-usage branch from 66345c0 to f79c3cbCompareApril 22, 2024 07:31
@szuend

Copy link
Copy Markdown
ContributorAuthor

Rebased the PR after #51783 has landed.

FYI @joyeecheung

@joyeecheungjoyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 22, 2024
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 22, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@github-actions

Copy link
Copy Markdown
Contributor
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

@legendecaslegendecas 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-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2025
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@legendecas

legendecas commented Apr 3, 2025

Copy link
Copy Markdown
Member

@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
szuendforce-pushed the fix-inspector-string-view-usage branch from fe2ea75 to b144ac0CompareApril 3, 2025 10:26
@szuend

Copy link
Copy Markdown
ContributorAuthor

@legendecas done! Thanks!

@legendecaslegendecas added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2025
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2025
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@legendecaslegendecas added the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 3, 2025
@nodejs-github-botnodejs-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:mainApr 3, 2025
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 264d031

RafaelGSS pushed a commit that referenced this pull request May 1, 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: #52372
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
RafaelGSS pushed a commit that referenced this pull request May 2, 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: #52372
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
aduh95 pushed a commit that referenced this pull request May 6, 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: #52372
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
RafaelGSS pushed a commit that referenced this pull request May 14, 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: #52372
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
aduh95 pushed a commit that referenced this pull request May 17, 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: #52372
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
aduh95 pushed a commit that referenced this pull request May 19, 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: #52372
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
@ghostghost mentioned this pull request Jun 8, 2025
hubot pushed a commit to v8/v8 that referenced this pull request Jul 11, 2025
Upstream Node.js was missing a check for 'is8Bit' making it necessary
to always use 16-bit wide chars when serializing CDP messages.
We fixed this upstream (nodejs/node#52372) so
now we can remove the widening copy.
Bug: None
Change-Id: I1f0b15e0ece7c0987dfcbeeaf913e9d76cda8ba8
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6478571
Reviewed-by: Eric Leese <leese@chromium.org>
Commit-Queue: Simon Zünd <szuend@chromium.org>
Cr-Commit-Position: refs/heads/main@{#101360}
Sign up for freeto 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 / srcIssues and PRs related to general changes in the lib or src directory.needs-ciPRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@szuend@nodejs-github-bot@BridgeAR@legendecas@jasnell@addaleax@joyeecheung@JungMinu@panva@cola119