Commit eef2449268c for nodejs
commit eef2449268cd51f9945bfaf405d72a6f606a8400
Author: dasunNimantha <dasun4@pm.me>
Date: Tue Nov 17 10:07:23 2026 +0530
url: avoid flattening strings in canParse fast path
URL.canParse() converted and flattened its arguments inside a V8 fast
API callback that returns a value. Like zlib.crc32(), that can allocate
and trigger a GC that invalidates the optimized caller, which V8 then
cannot lazily deoptimize and aborts instead.
The fast path now takes a sequential one-byte string, which V8 passes
without allocating, and returns -1 for anything it cannot handle that
way so that JavaScript falls back to the regular binding.
Refs: https://github.com/nodejs/node/issues/66870
Refs: https://github.com/nodejs/node/pull/67136
Signed-off-by: dasunNimantha <dasun4@pm.me>
PR-URL: https://github.com/nodejs/node/pull/67536
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
diff --git a/lib/internal/url.js b/lib/internal/url.js
index 1c4f45a4ea9..6c6706321db 100644
--- a/lib/internal/url.js
+++ b/lib/internal/url.js
@@ -1220,13 +1220,19 @@ class URL {
url = `${url}`;
}
+ // canParseFast() returns -1 when the regular binding is needed.
if (base !== undefined) {
- return bindingUrl.canParse(url, typeof base === 'string' ? base : `${base}`);
+ if (typeof base !== 'string') {
+ base = `${base}`;
+ }
+ const result = bindingUrl.canParseFast(url, base);
+ return result === -1 ? bindingUrl.canParse(url, base) : result === 1;
}
// It is important to differentiate the canParse call statements
// since they resolve into different v8 fast api overloads.
- return bindingUrl.canParse(url);
+ const result = bindingUrl.canParseFast(url);
+ return result === -1 ? bindingUrl.canParse(url) : result === 1;
}
}
diff --git a/src/node_url.cc b/src/node_url.cc
index d8a710b7427..621c19eadef 100644
--- a/src/node_url.cc
+++ b/src/node_url.cc
@@ -23,7 +23,6 @@ namespace url {
using v8::CFunction;
using v8::Context;
-using v8::FastApiCallbackOptions;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
@@ -318,45 +317,34 @@ void BindingData::CanParse(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(can_parse);
}
-bool BindingData::FastCanParse(
- Local<Value> receiver,
- Local<Value> input,
- // NOLINTNEXTLINE(runtime/references) This is V8 api.
- FastApiCallbackOptions& options) {
+// The fast API's slow fallback must not convert or flatten strings either: it
+// uses the same unsupported lazy-deoptimization continuation as the fast
+// callbacks. Return a Smi sentinel so JavaScript calls the regular binding.
+void BindingData::CanParseFastFallback(
+ const FunctionCallbackInfo<Value>& args) {
+ args.GetReturnValue().Set(-1);
+}
+
+int32_t BindingData::FastCanParse(Local<Value> receiver,
+ const v8::FastOneByteString& input) {
TRACK_V8_FAST_API_CALL("url.canParse");
- auto isolate = options.isolate;
- HandleScope handleScope(isolate);
- Local<String> str;
- if (!input->ToString(isolate->GetCurrentContext()).ToLocal(&str)) {
- return false;
- }
- Utf8Value utf8(isolate, str);
- return ada::can_parse(utf8.ToStringView());
+ // Latin-1 is only UTF-8 compatible for ASCII.
+ if (!simdutf::validate_ascii(input.data, input.length)) return -1;
+ return ada::can_parse(std::string_view(input.data, input.length)) ? 1 : 0;
}
-bool BindingData::FastCanParseWithBase(
- Local<Value> receiver,
- Local<Value> input,
- Local<Value> base,
- // NOLINTNEXTLINE(runtime/references) This is V8 api.
- FastApiCallbackOptions& options) {
+int32_t BindingData::FastCanParseWithBase(Local<Value> receiver,
+ const v8::FastOneByteString& input,
+ const v8::FastOneByteString& base) {
TRACK_V8_FAST_API_CALL("url.canParse.withBase");
- auto isolate = options.isolate;
- HandleScope handleScope(isolate);
- auto context = isolate->GetCurrentContext();
- Local<String> input_str;
- if (!input->ToString(context).ToLocal(&input_str)) {
- return false;
- }
- Local<String> base_str;
- if (!base->ToString(context).ToLocal(&base_str)) {
- return false;
- }
- Utf8Value input_utf8(isolate, input_str);
- Utf8Value base_utf8(isolate, base_str);
-
- auto base_view = base_utf8.ToStringView();
- return ada::can_parse(input_utf8.ToStringView(), &base_view);
+ if (!simdutf::validate_ascii(input.data, input.length) ||
+ !simdutf::validate_ascii(base.data, base.length)) {
+ return -1;
+ }
+ std::string_view base_view(base.data, base.length);
+ return ada::can_parse(std::string_view(input.data, input.length), &base_view)
+ ? 1
+ : 0;
}
CFunction BindingData::fast_can_parse_methods_[] = {
@@ -584,8 +572,12 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
SetMethod(isolate, target, "parse", Parse);
SetMethod(isolate, target, "pathToFileURL", PathToFileURL);
SetMethod(isolate, target, "update", Update);
- SetFastMethodNoSideEffect(
- isolate, target, "canParse", CanParse, {fast_can_parse_methods_, 2});
+ SetMethodNoSideEffect(isolate, target, "canParse", CanParse);
+ SetFastMethodNoSideEffect(isolate,
+ target,
+ "canParseFast",
+ CanParseFastFallback,
+ {fast_can_parse_methods_, 2});
}
void BindingData::CreatePerContextProperties(Local<Object> target,
@@ -606,6 +598,7 @@ void BindingData::RegisterExternalReferences(
registry->Register(PathToFileURL);
registry->Register(Update);
registry->Register(CanParse);
+ registry->Register(CanParseFastFallback);
for (const CFunction& method : fast_can_parse_methods_) {
registry->Register(method);
}
diff --git a/src/node_url.h b/src/node_url.h
index c41dc930433..308df226c59 100644
--- a/src/node_url.h
+++ b/src/node_url.h
@@ -50,16 +50,13 @@ class BindingData : public SnapshotableObject {
static void DomainToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);
static void CanParse(const v8::FunctionCallbackInfo<v8::Value>& args);
- static bool FastCanParse(v8::Local<v8::Value> receiver,
- v8::Local<v8::Value> input,
- // NOLINTNEXTLINE(runtime/references) This is V8 api.
- v8::FastApiCallbackOptions& options);
- static bool FastCanParseWithBase(
- v8::Local<v8::Value> receiver,
- v8::Local<v8::Value> input,
- v8::Local<v8::Value> base,
- // NOLINTNEXTLINE(runtime/references) This is V8 api.
- v8::FastApiCallbackOptions& options);
+ static void CanParseFastFallback(
+ const v8::FunctionCallbackInfo<v8::Value>& args);
+ static int32_t FastCanParse(v8::Local<v8::Value> receiver,
+ const v8::FastOneByteString& input);
+ static int32_t FastCanParseWithBase(v8::Local<v8::Value> receiver,
+ const v8::FastOneByteString& input,
+ const v8::FastOneByteString& base);
static void Format(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetOrigin(const v8::FunctionCallbackInfo<v8::Value>& args);
diff --git a/typings/internalBinding/url.d.ts b/typings/internalBinding/url.d.ts
index 5ad5932d091..4640341c2f7 100644
--- a/typings/internalBinding/url.d.ts
+++ b/typings/internalBinding/url.d.ts
@@ -7,6 +7,8 @@ export interface URLBinding {
domainToUnicode(input: string): string;
canParse(input: string): boolean;
canParse(input: string, base: string): boolean;
+ canParseFast(input: string): -1 | 0 | 1;
+ canParseFast(input: string, base: string): -1 | 0 | 1;
format(input: string, fragment?: boolean, unicode?: boolean, search?: boolean, auth?: boolean): string;
parse(input: string, base?: string): string | false;
update(input: string, actionType: typeof urlUpdateActions, value: string): string | false;