src: fix inefficient usage of v8_inspector::StringView · nodejs/node@70bb387 · GitHub
Skip to content

Commit 70bb387

Browse files
szuendaduh95
authored andcommitted
src: fix inefficient usage of v8_inspector::StringView
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>
1 parent d767cbf commit 70bb387

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/inspector_js_api.cc

Lines changed: 2 additions & 4 deletions

src/util-inl.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,32 @@ v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
343343
.FromMaybe(v8::Local<v8::String>());
344344
}
345345

346+
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
347+
v8_inspector::StringView str,
348+
v8::Isolate* isolate) {
349+
if (isolate == nullptr) isolate = context->GetIsolate();
350+
if (str.length() >= static_cast<size_t>(v8::String::kMaxLength))
351+
[[unlikely]] {
352+
// V8 only has a TODO comment about adding an exception when the maximum
353+
// string size is exceeded.
354+
ThrowErrStringTooLong(isolate);
355+
return v8::MaybeLocal<v8::Value>();
356+
}
357+
358+
if (str.is8Bit()) {
359+
return v8::String::NewFromOneByte(isolate,
360+
str.characters8(),
361+
v8::NewStringType::kNormal,
362+
str.length())
363+
.FromMaybe(v8::Local<v8::String>());
364+
}
365+
return v8::String::NewFromTwoByte(isolate,
366+
str.characters16(),
367+
v8::NewStringType::kNormal,
368+
str.length())
369+
.FromMaybe(v8::Local<v8::String>());
370+
}
371+
346372
template <typename T>
347373
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
348374
const std::vector<T>& vec,

src/util.h

Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)