GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — url: avoid flattening strings in canParse fast path · dasunNimantha/node@84c4ae6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal
    • url.js
  • src
    • node_url.cc
    • node_url.h
  • typings/internalBinding
    • url.d.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/url.js‎

Copy file name to clipboardExpand all lines: lib/internal/url.js
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,13 +1220,20 @@ class URL {
12201220
url = `${url}`;
12211221
}
12221222

1223+
// canParseFast() returns -1 when the input is not a sequential ASCII
1224+
// string; the regular binding handles everything else. It is important to
1225+
// differentiate the call statements since they resolve into different v8
1226+
// fast api overloads.
12231227
if (base !== undefined) {
1224-
return bindingUrl.canParse(url, typeof base === 'string' ? base : `${base}`);
1228+
if (typeof base !== 'string') {
1229+
base = `${base}`;
1230+
}
1231+
const result = bindingUrl.canParseFast(url, base);
1232+
return result === -1 ? bindingUrl.canParse(url, base) : result === 1;
12251233
}
12261234

1227-
// It is important to differentiate the canParse call statements
1228-
// since they resolve into different v8 fast api overloads.
1229-
return bindingUrl.canParse(url);
1235+
const result = bindingUrl.canParseFast(url);
1236+
return result === -1 ? bindingUrl.canParse(url) : result === 1;
12301237
}
12311238
}
12321239

Collapse file

‎src/node_url.cc‎

Copy file name to clipboardExpand all lines: src/node_url.cc
+35-38Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ namespace url {
2323

2424
using v8::CFunction;
2525
using v8::Context;
26-
using v8::FastApiCallbackOptions;
2726
using v8::FunctionCallbackInfo;
2827
using v8::HandleScope;
2928
using v8::Isolate;
@@ -323,45 +322,38 @@ void BindingData::CanParse(const FunctionCallbackInfo<Value>& args) {
323322
args.GetReturnValue().Set(can_parse);
324323
}
325324

326-
bool BindingData::FastCanParse(
327-
Local<Value> receiver,
328-
Local<Value> input,
329-
// NOLINTNEXTLINE(runtime/references) This is V8 api.
330-
FastApiCallbackOptions& options) {
325+
// canParseFast() is a V8 fast API call that returns a value. V8 cannot lazily
326+
// deoptimize the optimized caller of such a call (it aborts instead), so
327+
// neither the fast callbacks nor their slow fallback may allocate on the JS
328+
// heap, flatten a string, call into JavaScript or otherwise trigger a GC.
329+
// They only accept sequential one-byte strings and return -1 when the input
330+
// needs the regular canParse() binding (non-ASCII input, cons/two-byte
331+
// strings, ...); JavaScript then calls that binding separately.
332+
void BindingData::CanParseFastFallback(
333+
const FunctionCallbackInfo<Value>& args) {
334+
args.GetReturnValue().Set(-1);
335+
}
336+
337+
int32_t BindingData::FastCanParse(Local<Value> receiver,
338+
const v8::FastOneByteString& input) {
331339
TRACK_V8_FAST_API_CALL("url.canParse");
332-
auto isolate = options.isolate;
333-
HandleScope handleScope(isolate);
334-
Local<String> str;
335-
if (!input->ToString(isolate->GetCurrentContext()).ToLocal(&str)) {
336-
return false;
337-
}
338-
Utf8Value utf8(isolate, str);
339-
return ada::can_parse(utf8.ToStringView());
340+
// Latin-1 is only UTF-8 compatible for ASCII.
341+
if (!simdutf::validate_ascii(input.data, input.length)) return -1;
342+
return ada::can_parse(std::string_view(input.data, input.length)) ? 1 : 0;
340343
}
341344

342-
bool BindingData::FastCanParseWithBase(
343-
Local<Value> receiver,
344-
Local<Value> input,
345-
Local<Value> base,
346-
// NOLINTNEXTLINE(runtime/references) This is V8 api.
347-
FastApiCallbackOptions& options) {
345+
int32_t BindingData::FastCanParseWithBase(Local<Value> receiver,
346+
const v8::FastOneByteString& input,
347+
const v8::FastOneByteString& base) {
348348
TRACK_V8_FAST_API_CALL("url.canParse.withBase");
349-
auto isolate = options.isolate;
350-
HandleScope handleScope(isolate);
351-
auto context = isolate->GetCurrentContext();
352-
Local<String> input_str;
353-
if (!input->ToString(context).ToLocal(&input_str)) {
354-
return false;
355-
}
356-
Local<String> base_str;
357-
if (!base->ToString(context).ToLocal(&base_str)) {
358-
return false;
359-
}
360-
Utf8Value input_utf8(isolate, input_str);
361-
Utf8Value base_utf8(isolate, base_str);
362-
363-
auto base_view = base_utf8.ToStringView();
364-
return ada::can_parse(input_utf8.ToStringView(), &base_view);
349+
if (!simdutf::validate_ascii(input.data, input.length) ||
350+
!simdutf::validate_ascii(base.data, base.length)) {
351+
return -1;
352+
}
353+
std::string_view base_view(base.data, base.length);
354+
return ada::can_parse(std::string_view(input.data, input.length), &base_view)
355+
? 1
356+
: 0;
365357
}
366358

367359
CFunction BindingData::fast_can_parse_methods_[] = {
@@ -589,8 +581,12 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
589581
SetMethod(isolate, target, "parse", Parse);
590582
SetMethod(isolate, target, "pathToFileURL", PathToFileURL);
591583
SetMethod(isolate, target, "update", Update);
592-
SetFastMethodNoSideEffect(
593-
isolate, target, "canParse", CanParse, {fast_can_parse_methods_, 2});
584+
SetMethodNoSideEffect(isolate, target, "canParse", CanParse);
585+
SetFastMethodNoSideEffect(isolate,
586+
target,
587+
"canParseFast",
588+
CanParseFastFallback,
589+
{fast_can_parse_methods_, 2});
594590
}
595591

596592
void BindingData::CreatePerContextProperties(Local<Object> target,
@@ -611,6 +607,7 @@ void BindingData::RegisterExternalReferences(
611607
registry->Register(PathToFileURL);
612608
registry->Register(Update);
613609
registry->Register(CanParse);
610+
registry->Register(CanParseFastFallback);
614611
for (const CFunction& method : fast_can_parse_methods_) {
615612
registry->Register(method);
616613
}
Collapse file

‎src/node_url.h‎

Copy file name to clipboardExpand all lines: src/node_url.h
+7-10Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,13 @@ class BindingData : public SnapshotableObject {
5050
static void DomainToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);
5151

5252
static void CanParse(const v8::FunctionCallbackInfo<v8::Value>& args);
53-
static bool FastCanParse(v8::Local<v8::Value> receiver,
54-
v8::Local<v8::Value> input,
55-
// NOLINTNEXTLINE(runtime/references) This is V8 api.
56-
v8::FastApiCallbackOptions& options);
57-
static bool FastCanParseWithBase(
58-
v8::Local<v8::Value> receiver,
59-
v8::Local<v8::Value> input,
60-
v8::Local<v8::Value> base,
61-
// NOLINTNEXTLINE(runtime/references) This is V8 api.
62-
v8::FastApiCallbackOptions& options);
53+
static void CanParseFastFallback(
54+
const v8::FunctionCallbackInfo<v8::Value>& args);
55+
static int32_t FastCanParse(v8::Local<v8::Value> receiver,
56+
const v8::FastOneByteString& input);
57+
static int32_t FastCanParseWithBase(v8::Local<v8::Value> receiver,
58+
const v8::FastOneByteString& input,
59+
const v8::FastOneByteString& base);
6360

6461
static void Format(const v8::FunctionCallbackInfo<v8::Value>& args);
6562
static void GetOrigin(const v8::FunctionCallbackInfo<v8::Value>& args);
Collapse file

‎typings/internalBinding/url.d.ts‎

Copy file name to clipboardExpand all lines: typings/internalBinding/url.d.ts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export interface URLBinding {
77
domainToUnicode(input: string): string;
88
canParse(input: string): boolean;
99
canParse(input: string, base: string): boolean;
10+
canParseFast(input: string): -1 | 0 | 1;
11+
canParseFast(input: string, base: string): -1 | 0 | 1;
1012
format(input: string, fragment?: boolean, unicode?: boolean, search?: boolean, auth?: boolean): string;
1113
parse(input: string, base?: string): string | false;
1214
update(input: string, actionType: typeof urlUpdateActions, value: string): string | false;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.