Commit 2a64032

Browse files
anonrigaduh95
authored andcommitted
url: speed up WHATWG URL parsing
Parse one-byte ASCII inputs in place instead of copying them into a UTF-8 buffer, and reuse the original V8 string when the serialized href is unchanged. Delay URLContext allocation until parse finishes and skip ToString when the input is already a string. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Cursor PR-URL: #65361 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent b71d5de commit 2a64032

3 files changed

Lines changed: 215 additions & 69 deletions

File tree

‎lib/internal/url.js‎

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -167,41 +167,62 @@ function lazyCryptoRandom() {
167167
returncryptoRandom;
168168
}
169169

170+
/**
171+
* Copy href and the latest `urlComponents` snapshot into a URLContext.
172+
* Property assignment order matches the historical URLContext fields so
173+
* `util.inspect(..., { showHidden: true })` stays stable.
174+
* @param {object} ctx
175+
* @param {string} href
176+
*/
177+
functionsetURLContextFromBinding(ctx,href){
178+
constc=bindingUrl.urlComponents;
179+
ctx.href=href;
180+
ctx.protocol_end=c[0];
181+
ctx.username_end=c[1];
182+
ctx.host_start=c[2];
183+
ctx.host_end=c[3];
184+
ctx.pathname_start=c[5];
185+
ctx.search_start=c[6];
186+
ctx.hash_start=c[7];
187+
ctx.port=c[4];
188+
ctx.scheme_type=c[8];
189+
}
190+
170191
// This class provides the internal state of a URL object. An instance of this
171192
// class is stored in every URL object and is accessed internally by setters
172193
// and getters. It roughly corresponds to the concept of a URL record in the
173194
// URL Standard, with a few differences. It is also the object transported to
174195
// the C++ binding.
175196
// Refs: https://url.spec.whatwg.org/#concept-url
197+
//
198+
// scheme_type refers to ada::scheme::type:
199+
// HTTP = 0, NOT_SPECIAL = 1, HTTPS = 2, WS = 3, FTP = 4, WSS = 5, FILE = 6
176200
classURLContext{
177201
// This is the maximum value uint32_t can get.
178202
// Ada uses uint32_t(-1) for declaring omitted values.
179203
static #omitted =4294967295;
180204

181-
href='';
182-
protocol_end=0;
183-
username_end=0;
184-
host_start=0;
185-
host_end=0;
186-
pathname_start=0;
187-
search_start=0;
188-
hash_start=0;
189-
port=0;
190205
/**
191-
* Refers to `ada::scheme::type`
192-
*
193-
* enum type : uint8_t {
194-
* HTTP = 0,
195-
* NOT_SPECIAL = 1,
196-
* HTTPS = 2,
197-
* WS = 3,
198-
* FTP = 4,
199-
* WSS = 5,
200-
* FILE = 6
201-
* };
202-
* @type {number}
206+
* @param {string} [href] Parsed href. When omitted, create an empty context
207+
* (used by `URL.parse` on invalid input). When provided, `bindingUrl.parse`
208+
* / `update` has just written `urlComponents`.
203209
*/
204-
scheme_type=1;
210+
constructor(href){
211+
if(href===undefined){
212+
this.href='';
213+
this.protocol_end=0;
214+
this.username_end=0;
215+
this.host_start=0;
216+
this.host_end=0;
217+
this.pathname_start=0;
218+
this.search_start=0;
219+
this.hash_start=0;
220+
this.port=0;
221+
this.scheme_type=1;
222+
return;
223+
}
224+
setURLContextFromBinding(this,href);
225+
}
205226

206227
gethasPort(){
207228
returnthis.port!==URLContext.#omitted;
@@ -835,7 +856,7 @@ const kCreateURLFromPosixPathSymbol = Symbol('kCreateURLFromPosixPath');
835856
constkCreateURLFromWindowsPathSymbol=Symbol('kCreateURLFromWindowsPath');
836857

837858
classURL{
838-
#context=newURLContext();
859+
#context;
839860
#searchParams;
840861
#searchParamsModified;
841862

@@ -860,16 +881,16 @@ class URL {
860881
}
861882

862883
constructor(input,base=undefined,parseSymbol=undefined){
863-
markTransferMode(this,false,false);
864-
865884
if(arguments.length===0){
866885
thrownewERR_MISSING_ARGS('url');
867886
}
868887

869888
// StringPrototypeToWellFormed is not needed.
870-
input=`${input}`;
889+
if(typeofinput!=='string'){
890+
input=`${input}`;
891+
}
871892

872-
if(base!==undefined){
893+
if(base!==undefined&&typeofbase!=='string'){
873894
base=`${base}`;
874895
}
875896

@@ -884,9 +905,12 @@ class URL {
884905
bindingUrl.pathToFileURL(input,interpretAsWindowsPath,base) :
885906
bindingUrl.parse(input,base,raiseException);
886907
}
887-
if(href){
888-
this.#updateContext(href);
889-
}
908+
909+
// Delay context allocation until parse finishes so invalid URLs that
910+
// throw do not pay for an unused URLContext. Initialize in one shot
911+
// from the binding snapshot instead of writing an empty context first.
912+
this.#context =href ? newURLContext(href) : newURLContext();
913+
markTransferMode(this,false,false);
890914
}
891915

892916
staticparse(input,base=undefined){
@@ -955,29 +979,7 @@ class URL {
955979
constpreviousSearch=shouldUpdateSearchParams&&this.#searchParams &&
956980
(this.#searchParamsModified ? this.#getSearchFromParams() : this.#getSearchFromContext());
957981

958-
this.#context.href=href;
959-
960-
const{
961-
0: protocol_end,
962-
1: username_end,
963-
2: host_start,
964-
3: host_end,
965-
4: port,
966-
5: pathname_start,
967-
6: search_start,
968-
7: hash_start,
969-
8: scheme_type,
970-
}=bindingUrl.urlComponents;
971-
972-
this.#context.protocol_end=protocol_end;
973-
this.#context.username_end=username_end;
974-
this.#context.host_start=host_start;
975-
this.#context.host_end=host_end;
976-
this.#context.port=port;
977-
this.#context.pathname_start=pathname_start;
978-
this.#context.search_start=search_start;
979-
this.#context.hash_start=hash_start;
980-
this.#context.scheme_type=scheme_type;
982+
setURLContextFromBinding(this.#context,href);
981983

982984
if(this.#searchParams){
983985
// If the search string has updated, URL becomes the source of truth, and we update URLSearchParams.
@@ -1202,10 +1204,12 @@ class URL {
12021204
thrownewERR_MISSING_ARGS('url');
12031205
}
12041206

1205-
url=`${url}`;
1207+
if(typeofurl!=='string'){
1208+
url=`${url}`;
1209+
}
12061210

12071211
if(base!==undefined){
1208-
returnbindingUrl.canParse(url,`${base}`);
1212+
returnbindingUrl.canParse(url,typeofbase==='string' ? base : `${base}`);
12091213
}
12101214

12111215
// It is important to differentiate the canParse call statements

‎src/node_url.cc‎

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include"node_metadata.h"
99
#include"node_process-inl.h"
1010
#include"path.h"
11+
#include"simdutf.h"
1112
#include"util-inl.h"
1213
#include"v8-fast-api-calls.h"
1314
#include"v8-local-handle.h"
@@ -33,6 +34,38 @@ using v8::SnapshotCreator;
3334
using v8::String;
3435
using v8::Value;
3536

37+
namespace {
38+
39+
// Parse a V8 string as a URL. One-byte ASCII inputs are parsed in place
40+
// without allocating a UTF-8 copy. `reuse_input` is set when the serialized
41+
// href is identical to that ASCII input so the caller can return the original
42+
// V8 string. Non-ASCII inputs are never reused: UTF-8 conversion may replace
43+
// unpaired surrogates, so the original string may not match href.
44+
ada::result<ada::url_aggregator> ParseUrlFromV8String(
45+
Isolate* isolate,
46+
Local<String> input,
47+
const ada::url_aggregator* base_url,
48+
bool* reuse_input) {
49+
{
50+
String::ValueView view(isolate, input);
51+
if (view.is_one_byte()) {
52+
constchar* data = reinterpret_cast<constchar*>(view.data8());
53+
constsize_t length = static_cast<size_t>(view.length());
54+
if (simdutf::validate_ascii(data, length)) [[likely]] {
55+
const std::string_view input_view(data, length);
56+
auto out = ada::parse<ada::url_aggregator>(input_view, base_url);
57+
*reuse_input = out.has_value() && out->get_href() == input_view;
58+
return out;
59+
}
60+
}
61+
}
62+
*reuse_input = false;
63+
Utf8Value utf8(isolate, input);
64+
return ada::parse<ada::url_aggregator>(utf8.ToStringView(), base_url);
65+
}
66+
67+
} // namespace
68+
3669
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
3770
tracker->TrackField("url_components_buffer", url_components_buffer_);
3871
}
@@ -392,32 +425,51 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
392425
Realm* realm = Realm::GetCurrent(args);
393426
BindingData* binding_data = realm->GetBindingData<BindingData>();
394427
Isolate* isolate = realm->isolate();
395-
std::optional<std::string> base_{};
428+
Local<String> input_string = args[0].As<String>();
396429

397-
Utf8Value input(isolate, args[0]);
398430
ada::result<ada::url_aggregator> base;
399431
ada::url_aggregator* base_pointer = nullptr;
400432
if (args[1]->IsString()) {
401-
base_ = Utf8Value(isolate, args[1]).ToString();
402-
base = ada::parse<ada::url_aggregator>(*base_);
403-
if (!base && raise_exception) {
404-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
405-
} elseif (!base) {
433+
bool unused_reuse = false;
434+
base = ParseUrlFromV8String(
435+
isolate, args[1].As<String>(), nullptr, &unused_reuse);
436+
if (!base) {
437+
if (raise_exception) {
438+
Utf8Value input(isolate, input_string);
439+
Utf8Value base_utf8(isolate, args[1]);
440+
returnThrowInvalidURL(
441+
realm->env(), input.ToStringView(), base_utf8.ToString());
442+
}
406443
return;
407444
}
408445
base_pointer = &base.value();
409446
}
410-
auto out =
411-
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);
412447

413-
if (!out && raise_exception) {
414-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
415-
} elseif (!out) {
448+
bool reuse_input = false;
449+
auto out =
450+
ParseUrlFromV8String(isolate, input_string, base_pointer, &reuse_input);
451+
if (!out) {
452+
if (raise_exception) {
453+
Utf8Value input(isolate, input_string);
454+
std::optional<std::string> base_error;
455+
if (args[1]->IsString()) {
456+
base_error = Utf8Value(isolate, args[1]).ToString();
457+
}
458+
returnThrowInvalidURL(
459+
realm->env(), input.ToStringView(), std::move(base_error));
460+
}
416461
return;
417462
}
418463

419464
binding_data->UpdateComponents(out->get_components(), out->type);
420465

466+
// Already-serialized ASCII URLs are the common case. Reuse the input
467+
// string instead of allocating an identical V8 string from href.
468+
if (reuse_input) {
469+
args.GetReturnValue().Set(args[0]);
470+
return;
471+
}
472+
421473
Local<Value> ret;
422474
if (ToV8Value(realm->context(), out->get_href(), isolate).ToLocal(&ret))
423475
[[likely]] {
@@ -439,13 +491,15 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
439491
return;
440492
}
441493
enum url_update_action action = static_cast<enum url_update_action>(val);
442-
Utf8Value input(isolate, args[0].As<String>());
443494
Utf8Value new_value(isolate, args[2].As<String>());
444495

445496
std::string_view new_value_view = new_value.ToStringView();
446497
// A serialized URL is not always reparsable: the IDNA encoder can emit a
447498
// host label that the decoder rejects. Fail the update instead of crashing.
448-
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
499+
// Existing hrefs are typically already-serialized ASCII, so parse in place.
500+
bool unused_reuse = false;
501+
auto out = ParseUrlFromV8String(
502+
isolate, args[0].As<String>(), nullptr, &unused_reuse);
449503
if (!out) {
450504
return args.GetReturnValue().Set(false);
451505
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// Covers the URL constructor parse paths that avoid a UTF-8 copy and/or
4+
// reuse the input string when it is already a serialized ASCII href.
5+
6+
const{ hasIntl }=require('../common');
7+
constassert=require('assert');
8+
9+
constalreadySerialized=[
10+
'https://nodejs.org/en/blog/',
11+
'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/'+
12+
'/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1'+
13+
'&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&'+
14+
'key=f5c65e1e98fe07e648249ad41e1cfdb0',
15+
'https://user:pass@example.com/path?search=1',
16+
'file:///foo/bar/test/node.js',
17+
'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868',
18+
];
19+
20+
for(consthrefofalreadySerialized){
21+
consturl=newURL(href);
22+
assert.strictEqual(url.href,href);
23+
assert.strictEqual(URL.parse(href).href,href);
24+
assert.strictEqual(URL.canParse(href),true);
25+
}
26+
27+
// Special-scheme URLs with an empty path gain a trailing slash.
28+
{
29+
consturl=newURL('https://example.com');
30+
assert.strictEqual(url.href,'https://example.com/');
31+
assert.strictEqual(url.pathname,'/');
32+
}
33+
34+
// Dot-segment normalization must still rewrite the path.
35+
{
36+
consturl=newURL('https://example.org/./a/../b/./c');
37+
assert.strictEqual(url.href,'https://example.org/b/c');
38+
assert.strictEqual(url.pathname,'/b/c');
39+
}
40+
41+
// Relative resolution against a base URL.
42+
{
43+
consturl=newURL('/path?x=1#h','https://example.com:8443/base');
44+
assert.strictEqual(url.href,'https://example.com:8443/path?x=1#h');
45+
assert.strictEqual(url.host,'example.com:8443');
46+
}
47+
48+
// Non-string input is still stringified.
49+
{
50+
consturl=newURL({toString: ()=>'https://example.com/from-object'});
51+
assert.strictEqual(url.href,'https://example.com/from-object');
52+
}
53+
54+
// Invalid input still throws from the constructor and is null from parse().
55+
{
56+
assert.throws(()=>newURL('not a url'),{
57+
code: 'ERR_INVALID_URL',
58+
name: 'TypeError',
59+
});
60+
assert.strictEqual(URL.parse('not a url'),null);
61+
assert.strictEqual(URL.canParse('not a url'),false);
62+
}
63+
64+
// Unpaired surrogates must not be returned as-is from href.
65+
{
66+
constinput='https://example.com/\uD800';
67+
consturl=newURL(input);
68+
assert.notStrictEqual(url.href,input);
69+
assert.ok(url.href.startsWith('https://example.com/'));
70+
}
71+
72+
if(hasIntl){
73+
consturl=newURL('http://你好你好.在线');
74+
assert.ok(url.hostname.startsWith('xn--'));
75+
assert.ok(url.href.startsWith('http://xn--'));
76+
}
77+
78+
// Setters re-parse the existing href; keep component updates correct.
79+
{
80+
consturl=newURL('https://example.com/old');
81+
url.pathname='/new';
82+
url.search='q=1';
83+
url.hash='frag';
84+
assert.strictEqual(url.href,'https://example.com/new?q=1#frag');
85+
assert.strictEqual(url.pathname,'/new');
86+
assert.strictEqual(url.search,'?q=1');
87+
assert.strictEqual(url.hash,'#frag');
88+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit 2a64032

Browse files
anonrigaduh95
authored andcommitted
url: speed up WHATWG URL parsing
Parse one-byte ASCII inputs in place instead of copying them into a UTF-8 buffer, and reuse the original V8 string when the serialized href is unchanged. Delay URLContext allocation until parse finishes and skip ToString when the input is already a string. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Cursor PR-URL: #65361 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent b71d5de commit 2a64032

3 files changed

Lines changed: 215 additions & 69 deletions

File tree

‎lib/internal/url.js‎

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -167,41 +167,62 @@ function lazyCryptoRandom() {
167167
returncryptoRandom;
168168
}
169169

170+
/**
171+
* Copy href and the latest `urlComponents` snapshot into a URLContext.
172+
* Property assignment order matches the historical URLContext fields so
173+
* `util.inspect(..., { showHidden: true })` stays stable.
174+
* @param {object} ctx
175+
* @param {string} href
176+
*/
177+
functionsetURLContextFromBinding(ctx,href){
178+
constc=bindingUrl.urlComponents;
179+
ctx.href=href;
180+
ctx.protocol_end=c[0];
181+
ctx.username_end=c[1];
182+
ctx.host_start=c[2];
183+
ctx.host_end=c[3];
184+
ctx.pathname_start=c[5];
185+
ctx.search_start=c[6];
186+
ctx.hash_start=c[7];
187+
ctx.port=c[4];
188+
ctx.scheme_type=c[8];
189+
}
190+
170191
// This class provides the internal state of a URL object. An instance of this
171192
// class is stored in every URL object and is accessed internally by setters
172193
// and getters. It roughly corresponds to the concept of a URL record in the
173194
// URL Standard, with a few differences. It is also the object transported to
174195
// the C++ binding.
175196
// Refs: https://url.spec.whatwg.org/#concept-url
197+
//
198+
// scheme_type refers to ada::scheme::type:
199+
// HTTP = 0, NOT_SPECIAL = 1, HTTPS = 2, WS = 3, FTP = 4, WSS = 5, FILE = 6
176200
classURLContext{
177201
// This is the maximum value uint32_t can get.
178202
// Ada uses uint32_t(-1) for declaring omitted values.
179203
static #omitted =4294967295;
180204

181-
href='';
182-
protocol_end=0;
183-
username_end=0;
184-
host_start=0;
185-
host_end=0;
186-
pathname_start=0;
187-
search_start=0;
188-
hash_start=0;
189-
port=0;
190205
/**
191-
* Refers to `ada::scheme::type`
192-
*
193-
* enum type : uint8_t {
194-
* HTTP = 0,
195-
* NOT_SPECIAL = 1,
196-
* HTTPS = 2,
197-
* WS = 3,
198-
* FTP = 4,
199-
* WSS = 5,
200-
* FILE = 6
201-
* };
202-
* @type {number}
206+
* @param {string} [href] Parsed href. When omitted, create an empty context
207+
* (used by `URL.parse` on invalid input). When provided, `bindingUrl.parse`
208+
* / `update` has just written `urlComponents`.
203209
*/
204-
scheme_type=1;
210+
constructor(href){
211+
if(href===undefined){
212+
this.href='';
213+
this.protocol_end=0;
214+
this.username_end=0;
215+
this.host_start=0;
216+
this.host_end=0;
217+
this.pathname_start=0;
218+
this.search_start=0;
219+
this.hash_start=0;
220+
this.port=0;
221+
this.scheme_type=1;
222+
return;
223+
}
224+
setURLContextFromBinding(this,href);
225+
}
205226

206227
gethasPort(){
207228
returnthis.port!==URLContext.#omitted;
@@ -835,7 +856,7 @@ const kCreateURLFromPosixPathSymbol = Symbol('kCreateURLFromPosixPath');
835856
constkCreateURLFromWindowsPathSymbol=Symbol('kCreateURLFromWindowsPath');
836857

837858
classURL{
838-
#context=newURLContext();
859+
#context;
839860
#searchParams;
840861
#searchParamsModified;
841862

@@ -860,16 +881,16 @@ class URL {
860881
}
861882

862883
constructor(input,base=undefined,parseSymbol=undefined){
863-
markTransferMode(this,false,false);
864-
865884
if(arguments.length===0){
866885
thrownewERR_MISSING_ARGS('url');
867886
}
868887

869888
// StringPrototypeToWellFormed is not needed.
870-
input=`${input}`;
889+
if(typeofinput!=='string'){
890+
input=`${input}`;
891+
}
871892

872-
if(base!==undefined){
893+
if(base!==undefined&&typeofbase!=='string'){
873894
base=`${base}`;
874895
}
875896

@@ -884,9 +905,12 @@ class URL {
884905
bindingUrl.pathToFileURL(input,interpretAsWindowsPath,base) :
885906
bindingUrl.parse(input,base,raiseException);
886907
}
887-
if(href){
888-
this.#updateContext(href);
889-
}
908+
909+
// Delay context allocation until parse finishes so invalid URLs that
910+
// throw do not pay for an unused URLContext. Initialize in one shot
911+
// from the binding snapshot instead of writing an empty context first.
912+
this.#context =href ? newURLContext(href) : newURLContext();
913+
markTransferMode(this,false,false);
890914
}
891915

892916
staticparse(input,base=undefined){
@@ -955,29 +979,7 @@ class URL {
955979
constpreviousSearch=shouldUpdateSearchParams&&this.#searchParams &&
956980
(this.#searchParamsModified ? this.#getSearchFromParams() : this.#getSearchFromContext());
957981

958-
this.#context.href=href;
959-
960-
const{
961-
0: protocol_end,
962-
1: username_end,
963-
2: host_start,
964-
3: host_end,
965-
4: port,
966-
5: pathname_start,
967-
6: search_start,
968-
7: hash_start,
969-
8: scheme_type,
970-
}=bindingUrl.urlComponents;
971-
972-
this.#context.protocol_end=protocol_end;
973-
this.#context.username_end=username_end;
974-
this.#context.host_start=host_start;
975-
this.#context.host_end=host_end;
976-
this.#context.port=port;
977-
this.#context.pathname_start=pathname_start;
978-
this.#context.search_start=search_start;
979-
this.#context.hash_start=hash_start;
980-
this.#context.scheme_type=scheme_type;
982+
setURLContextFromBinding(this.#context,href);
981983

982984
if(this.#searchParams){
983985
// If the search string has updated, URL becomes the source of truth, and we update URLSearchParams.
@@ -1202,10 +1204,12 @@ class URL {
12021204
thrownewERR_MISSING_ARGS('url');
12031205
}
12041206

1205-
url=`${url}`;
1207+
if(typeofurl!=='string'){
1208+
url=`${url}`;
1209+
}
12061210

12071211
if(base!==undefined){
1208-
returnbindingUrl.canParse(url,`${base}`);
1212+
returnbindingUrl.canParse(url,typeofbase==='string' ? base : `${base}`);
12091213
}
12101214

12111215
// It is important to differentiate the canParse call statements

‎src/node_url.cc‎

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include"node_metadata.h"
99
#include"node_process-inl.h"
1010
#include"path.h"
11+
#include"simdutf.h"
1112
#include"util-inl.h"
1213
#include"v8-fast-api-calls.h"
1314
#include"v8-local-handle.h"
@@ -33,6 +34,38 @@ using v8::SnapshotCreator;
3334
using v8::String;
3435
using v8::Value;
3536

37+
namespace {
38+
39+
// Parse a V8 string as a URL. One-byte ASCII inputs are parsed in place
40+
// without allocating a UTF-8 copy. `reuse_input` is set when the serialized
41+
// href is identical to that ASCII input so the caller can return the original
42+
// V8 string. Non-ASCII inputs are never reused: UTF-8 conversion may replace
43+
// unpaired surrogates, so the original string may not match href.
44+
ada::result<ada::url_aggregator> ParseUrlFromV8String(
45+
Isolate* isolate,
46+
Local<String> input,
47+
const ada::url_aggregator* base_url,
48+
bool* reuse_input) {
49+
{
50+
String::ValueView view(isolate, input);
51+
if (view.is_one_byte()) {
52+
constchar* data = reinterpret_cast<constchar*>(view.data8());
53+
constsize_t length = static_cast<size_t>(view.length());
54+
if (simdutf::validate_ascii(data, length)) [[likely]] {
55+
const std::string_view input_view(data, length);
56+
auto out = ada::parse<ada::url_aggregator>(input_view, base_url);
57+
*reuse_input = out.has_value() && out->get_href() == input_view;
58+
return out;
59+
}
60+
}
61+
}
62+
*reuse_input = false;
63+
Utf8Value utf8(isolate, input);
64+
return ada::parse<ada::url_aggregator>(utf8.ToStringView(), base_url);
65+
}
66+
67+
} // namespace
68+
3669
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
3770
tracker->TrackField("url_components_buffer", url_components_buffer_);
3871
}
@@ -392,32 +425,51 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
392425
Realm* realm = Realm::GetCurrent(args);
393426
BindingData* binding_data = realm->GetBindingData<BindingData>();
394427
Isolate* isolate = realm->isolate();
395-
std::optional<std::string> base_{};
428+
Local<String> input_string = args[0].As<String>();
396429

397-
Utf8Value input(isolate, args[0]);
398430
ada::result<ada::url_aggregator> base;
399431
ada::url_aggregator* base_pointer = nullptr;
400432
if (args[1]->IsString()) {
401-
base_ = Utf8Value(isolate, args[1]).ToString();
402-
base = ada::parse<ada::url_aggregator>(*base_);
403-
if (!base && raise_exception) {
404-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
405-
} elseif (!base) {
433+
bool unused_reuse = false;
434+
base = ParseUrlFromV8String(
435+
isolate, args[1].As<String>(), nullptr, &unused_reuse);
436+
if (!base) {
437+
if (raise_exception) {
438+
Utf8Value input(isolate, input_string);
439+
Utf8Value base_utf8(isolate, args[1]);
440+
returnThrowInvalidURL(
441+
realm->env(), input.ToStringView(), base_utf8.ToString());
442+
}
406443
return;
407444
}
408445
base_pointer = &base.value();
409446
}
410-
auto out =
411-
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);
412447

413-
if (!out && raise_exception) {
414-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
415-
} elseif (!out) {
448+
bool reuse_input = false;
449+
auto out =
450+
ParseUrlFromV8String(isolate, input_string, base_pointer, &reuse_input);
451+
if (!out) {
452+
if (raise_exception) {
453+
Utf8Value input(isolate, input_string);
454+
std::optional<std::string> base_error;
455+
if (args[1]->IsString()) {
456+
base_error = Utf8Value(isolate, args[1]).ToString();
457+
}
458+
returnThrowInvalidURL(
459+
realm->env(), input.ToStringView(), std::move(base_error));
460+
}
416461
return;
417462
}
418463

419464
binding_data->UpdateComponents(out->get_components(), out->type);
420465

466+
// Already-serialized ASCII URLs are the common case. Reuse the input
467+
// string instead of allocating an identical V8 string from href.
468+
if (reuse_input) {
469+
args.GetReturnValue().Set(args[0]);
470+
return;
471+
}
472+
421473
Local<Value> ret;
422474
if (ToV8Value(realm->context(), out->get_href(), isolate).ToLocal(&ret))
423475
[[likely]] {
@@ -439,13 +491,15 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
439491
return;
440492
}
441493
enum url_update_action action = static_cast<enum url_update_action>(val);
442-
Utf8Value input(isolate, args[0].As<String>());
443494
Utf8Value new_value(isolate, args[2].As<String>());
444495

445496
std::string_view new_value_view = new_value.ToStringView();
446497
// A serialized URL is not always reparsable: the IDNA encoder can emit a
447498
// host label that the decoder rejects. Fail the update instead of crashing.
448-
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
499+
// Existing hrefs are typically already-serialized ASCII, so parse in place.
500+
bool unused_reuse = false;
501+
auto out = ParseUrlFromV8String(
502+
isolate, args[0].As<String>(), nullptr, &unused_reuse);
449503
if (!out) {
450504
return args.GetReturnValue().Set(false);
451505
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// Covers the URL constructor parse paths that avoid a UTF-8 copy and/or
4+
// reuse the input string when it is already a serialized ASCII href.
5+
6+
const{ hasIntl }=require('../common');
7+
constassert=require('assert');
8+
9+
constalreadySerialized=[
10+
'https://nodejs.org/en/blog/',
11+
'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/'+
12+
'/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1'+
13+
'&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&'+
14+
'key=f5c65e1e98fe07e648249ad41e1cfdb0',
15+
'https://user:pass@example.com/path?search=1',
16+
'file:///foo/bar/test/node.js',
17+
'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868',
18+
];
19+
20+
for(consthrefofalreadySerialized){
21+
consturl=newURL(href);
22+
assert.strictEqual(url.href,href);
23+
assert.strictEqual(URL.parse(href).href,href);
24+
assert.strictEqual(URL.canParse(href),true);
25+
}
26+
27+
// Special-scheme URLs with an empty path gain a trailing slash.
28+
{
29+
consturl=newURL('https://example.com');
30+
assert.strictEqual(url.href,'https://example.com/');
31+
assert.strictEqual(url.pathname,'/');
32+
}
33+
34+
// Dot-segment normalization must still rewrite the path.
35+
{
36+
consturl=newURL('https://example.org/./a/../b/./c');
37+
assert.strictEqual(url.href,'https://example.org/b/c');
38+
assert.strictEqual(url.pathname,'/b/c');
39+
}
40+
41+
// Relative resolution against a base URL.
42+
{
43+
consturl=newURL('/path?x=1#h','https://example.com:8443/base');
44+
assert.strictEqual(url.href,'https://example.com:8443/path?x=1#h');
45+
assert.strictEqual(url.host,'example.com:8443');
46+
}
47+
48+
// Non-string input is still stringified.
49+
{
50+
consturl=newURL({toString: ()=>'https://example.com/from-object'});
51+
assert.strictEqual(url.href,'https://example.com/from-object');
52+
}
53+
54+
// Invalid input still throws from the constructor and is null from parse().
55+
{
56+
assert.throws(()=>newURL('not a url'),{
57+
code: 'ERR_INVALID_URL',
58+
name: 'TypeError',
59+
});
60+
assert.strictEqual(URL.parse('not a url'),null);
61+
assert.strictEqual(URL.canParse('not a url'),false);
62+
}
63+
64+
// Unpaired surrogates must not be returned as-is from href.
65+
{
66+
constinput='https://example.com/\uD800';
67+
consturl=newURL(input);
68+
assert.notStrictEqual(url.href,input);
69+
assert.ok(url.href.startsWith('https://example.com/'));
70+
}
71+
72+
if(hasIntl){
73+
consturl=newURL('http://你好你好.在线');
74+
assert.ok(url.hostname.startsWith('xn--'));
75+
assert.ok(url.href.startsWith('http://xn--'));
76+
}
77+
78+
// Setters re-parse the existing href; keep component updates correct.
79+
{
80+
consturl=newURL('https://example.com/old');
81+
url.pathname='/new';
82+
url.search='q=1';
83+
url.hash='frag';
84+
assert.strictEqual(url.href,'https://example.com/new?q=1#frag');
85+
assert.strictEqual(url.pathname,'/new');
86+
assert.strictEqual(url.search,'?q=1');
87+
assert.strictEqual(url.hash,'#frag');
88+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 2a64032

Browse files
anonrigaduh95
authored andcommitted
url: speed up WHATWG URL parsing
Parse one-byte ASCII inputs in place instead of copying them into a UTF-8 buffer, and reuse the original V8 string when the serialized href is unchanged. Delay URLContext allocation until parse finishes and skip ToString when the input is already a string. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Cursor PR-URL: #65361 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent b71d5de commit 2a64032

3 files changed

Lines changed: 215 additions & 69 deletions

File tree

‎lib/internal/url.js‎

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -167,41 +167,62 @@ function lazyCryptoRandom() {
167167
returncryptoRandom;
168168
}
169169

170+
/**
171+
* Copy href and the latest `urlComponents` snapshot into a URLContext.
172+
* Property assignment order matches the historical URLContext fields so
173+
* `util.inspect(..., { showHidden: true })` stays stable.
174+
* @param {object} ctx
175+
* @param {string} href
176+
*/
177+
functionsetURLContextFromBinding(ctx,href){
178+
constc=bindingUrl.urlComponents;
179+
ctx.href=href;
180+
ctx.protocol_end=c[0];
181+
ctx.username_end=c[1];
182+
ctx.host_start=c[2];
183+
ctx.host_end=c[3];
184+
ctx.pathname_start=c[5];
185+
ctx.search_start=c[6];
186+
ctx.hash_start=c[7];
187+
ctx.port=c[4];
188+
ctx.scheme_type=c[8];
189+
}
190+
170191
// This class provides the internal state of a URL object. An instance of this
171192
// class is stored in every URL object and is accessed internally by setters
172193
// and getters. It roughly corresponds to the concept of a URL record in the
173194
// URL Standard, with a few differences. It is also the object transported to
174195
// the C++ binding.
175196
// Refs: https://url.spec.whatwg.org/#concept-url
197+
//
198+
// scheme_type refers to ada::scheme::type:
199+
// HTTP = 0, NOT_SPECIAL = 1, HTTPS = 2, WS = 3, FTP = 4, WSS = 5, FILE = 6
176200
classURLContext{
177201
// This is the maximum value uint32_t can get.
178202
// Ada uses uint32_t(-1) for declaring omitted values.
179203
static #omitted =4294967295;
180204

181-
href='';
182-
protocol_end=0;
183-
username_end=0;
184-
host_start=0;
185-
host_end=0;
186-
pathname_start=0;
187-
search_start=0;
188-
hash_start=0;
189-
port=0;
190205
/**
191-
* Refers to `ada::scheme::type`
192-
*
193-
* enum type : uint8_t {
194-
* HTTP = 0,
195-
* NOT_SPECIAL = 1,
196-
* HTTPS = 2,
197-
* WS = 3,
198-
* FTP = 4,
199-
* WSS = 5,
200-
* FILE = 6
201-
* };
202-
* @type {number}
206+
* @param {string} [href] Parsed href. When omitted, create an empty context
207+
* (used by `URL.parse` on invalid input). When provided, `bindingUrl.parse`
208+
* / `update` has just written `urlComponents`.
203209
*/
204-
scheme_type=1;
210+
constructor(href){
211+
if(href===undefined){
212+
this.href='';
213+
this.protocol_end=0;
214+
this.username_end=0;
215+
this.host_start=0;
216+
this.host_end=0;
217+
this.pathname_start=0;
218+
this.search_start=0;
219+
this.hash_start=0;
220+
this.port=0;
221+
this.scheme_type=1;
222+
return;
223+
}
224+
setURLContextFromBinding(this,href);
225+
}
205226

206227
gethasPort(){
207228
returnthis.port!==URLContext.#omitted;
@@ -835,7 +856,7 @@ const kCreateURLFromPosixPathSymbol = Symbol('kCreateURLFromPosixPath');
835856
constkCreateURLFromWindowsPathSymbol=Symbol('kCreateURLFromWindowsPath');
836857

837858
classURL{
838-
#context=newURLContext();
859+
#context;
839860
#searchParams;
840861
#searchParamsModified;
841862

@@ -860,16 +881,16 @@ class URL {
860881
}
861882

862883
constructor(input,base=undefined,parseSymbol=undefined){
863-
markTransferMode(this,false,false);
864-
865884
if(arguments.length===0){
866885
thrownewERR_MISSING_ARGS('url');
867886
}
868887

869888
// StringPrototypeToWellFormed is not needed.
870-
input=`${input}`;
889+
if(typeofinput!=='string'){
890+
input=`${input}`;
891+
}
871892

872-
if(base!==undefined){
893+
if(base!==undefined&&typeofbase!=='string'){
873894
base=`${base}`;
874895
}
875896

@@ -884,9 +905,12 @@ class URL {
884905
bindingUrl.pathToFileURL(input,interpretAsWindowsPath,base) :
885906
bindingUrl.parse(input,base,raiseException);
886907
}
887-
if(href){
888-
this.#updateContext(href);
889-
}
908+
909+
// Delay context allocation until parse finishes so invalid URLs that
910+
// throw do not pay for an unused URLContext. Initialize in one shot
911+
// from the binding snapshot instead of writing an empty context first.
912+
this.#context =href ? newURLContext(href) : newURLContext();
913+
markTransferMode(this,false,false);
890914
}
891915

892916
staticparse(input,base=undefined){
@@ -955,29 +979,7 @@ class URL {
955979
constpreviousSearch=shouldUpdateSearchParams&&this.#searchParams &&
956980
(this.#searchParamsModified ? this.#getSearchFromParams() : this.#getSearchFromContext());
957981

958-
this.#context.href=href;
959-
960-
const{
961-
0: protocol_end,
962-
1: username_end,
963-
2: host_start,
964-
3: host_end,
965-
4: port,
966-
5: pathname_start,
967-
6: search_start,
968-
7: hash_start,
969-
8: scheme_type,
970-
}=bindingUrl.urlComponents;
971-
972-
this.#context.protocol_end=protocol_end;
973-
this.#context.username_end=username_end;
974-
this.#context.host_start=host_start;
975-
this.#context.host_end=host_end;
976-
this.#context.port=port;
977-
this.#context.pathname_start=pathname_start;
978-
this.#context.search_start=search_start;
979-
this.#context.hash_start=hash_start;
980-
this.#context.scheme_type=scheme_type;
982+
setURLContextFromBinding(this.#context,href);
981983

982984
if(this.#searchParams){
983985
// If the search string has updated, URL becomes the source of truth, and we update URLSearchParams.
@@ -1202,10 +1204,12 @@ class URL {
12021204
thrownewERR_MISSING_ARGS('url');
12031205
}
12041206

1205-
url=`${url}`;
1207+
if(typeofurl!=='string'){
1208+
url=`${url}`;
1209+
}
12061210

12071211
if(base!==undefined){
1208-
returnbindingUrl.canParse(url,`${base}`);
1212+
returnbindingUrl.canParse(url,typeofbase==='string' ? base : `${base}`);
12091213
}
12101214

12111215
// It is important to differentiate the canParse call statements

‎src/node_url.cc‎

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include"node_metadata.h"
99
#include"node_process-inl.h"
1010
#include"path.h"
11+
#include"simdutf.h"
1112
#include"util-inl.h"
1213
#include"v8-fast-api-calls.h"
1314
#include"v8-local-handle.h"
@@ -33,6 +34,38 @@ using v8::SnapshotCreator;
3334
using v8::String;
3435
using v8::Value;
3536

37+
namespace {
38+
39+
// Parse a V8 string as a URL. One-byte ASCII inputs are parsed in place
40+
// without allocating a UTF-8 copy. `reuse_input` is set when the serialized
41+
// href is identical to that ASCII input so the caller can return the original
42+
// V8 string. Non-ASCII inputs are never reused: UTF-8 conversion may replace
43+
// unpaired surrogates, so the original string may not match href.
44+
ada::result<ada::url_aggregator> ParseUrlFromV8String(
45+
Isolate* isolate,
46+
Local<String> input,
47+
const ada::url_aggregator* base_url,
48+
bool* reuse_input) {
49+
{
50+
String::ValueView view(isolate, input);
51+
if (view.is_one_byte()) {
52+
constchar* data = reinterpret_cast<constchar*>(view.data8());
53+
constsize_t length = static_cast<size_t>(view.length());
54+
if (simdutf::validate_ascii(data, length)) [[likely]] {
55+
const std::string_view input_view(data, length);
56+
auto out = ada::parse<ada::url_aggregator>(input_view, base_url);
57+
*reuse_input = out.has_value() && out->get_href() == input_view;
58+
return out;
59+
}
60+
}
61+
}
62+
*reuse_input = false;
63+
Utf8Value utf8(isolate, input);
64+
return ada::parse<ada::url_aggregator>(utf8.ToStringView(), base_url);
65+
}
66+
67+
} // namespace
68+
3669
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
3770
tracker->TrackField("url_components_buffer", url_components_buffer_);
3871
}
@@ -392,32 +425,51 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
392425
Realm* realm = Realm::GetCurrent(args);
393426
BindingData* binding_data = realm->GetBindingData<BindingData>();
394427
Isolate* isolate = realm->isolate();
395-
std::optional<std::string> base_{};
428+
Local<String> input_string = args[0].As<String>();
396429

397-
Utf8Value input(isolate, args[0]);
398430
ada::result<ada::url_aggregator> base;
399431
ada::url_aggregator* base_pointer = nullptr;
400432
if (args[1]->IsString()) {
401-
base_ = Utf8Value(isolate, args[1]).ToString();
402-
base = ada::parse<ada::url_aggregator>(*base_);
403-
if (!base && raise_exception) {
404-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
405-
} elseif (!base) {
433+
bool unused_reuse = false;
434+
base = ParseUrlFromV8String(
435+
isolate, args[1].As<String>(), nullptr, &unused_reuse);
436+
if (!base) {
437+
if (raise_exception) {
438+
Utf8Value input(isolate, input_string);
439+
Utf8Value base_utf8(isolate, args[1]);
440+
returnThrowInvalidURL(
441+
realm->env(), input.ToStringView(), base_utf8.ToString());
442+
}
406443
return;
407444
}
408445
base_pointer = &base.value();
409446
}
410-
auto out =
411-
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);
412447

413-
if (!out && raise_exception) {
414-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
415-
} elseif (!out) {
448+
bool reuse_input = false;
449+
auto out =
450+
ParseUrlFromV8String(isolate, input_string, base_pointer, &reuse_input);
451+
if (!out) {
452+
if (raise_exception) {
453+
Utf8Value input(isolate, input_string);
454+
std::optional<std::string> base_error;
455+
if (args[1]->IsString()) {
456+
base_error = Utf8Value(isolate, args[1]).ToString();
457+
}
458+
returnThrowInvalidURL(
459+
realm->env(), input.ToStringView(), std::move(base_error));
460+
}
416461
return;
417462
}
418463

419464
binding_data->UpdateComponents(out->get_components(), out->type);
420465

466+
// Already-serialized ASCII URLs are the common case. Reuse the input
467+
// string instead of allocating an identical V8 string from href.
468+
if (reuse_input) {
469+
args.GetReturnValue().Set(args[0]);
470+
return;
471+
}
472+
421473
Local<Value> ret;
422474
if (ToV8Value(realm->context(), out->get_href(), isolate).ToLocal(&ret))
423475
[[likely]] {
@@ -439,13 +491,15 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
439491
return;
440492
}
441493
enum url_update_action action = static_cast<enum url_update_action>(val);
442-
Utf8Value input(isolate, args[0].As<String>());
443494
Utf8Value new_value(isolate, args[2].As<String>());
444495

445496
std::string_view new_value_view = new_value.ToStringView();
446497
// A serialized URL is not always reparsable: the IDNA encoder can emit a
447498
// host label that the decoder rejects. Fail the update instead of crashing.
448-
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
499+
// Existing hrefs are typically already-serialized ASCII, so parse in place.
500+
bool unused_reuse = false;
501+
auto out = ParseUrlFromV8String(
502+
isolate, args[0].As<String>(), nullptr, &unused_reuse);
449503
if (!out) {
450504
return args.GetReturnValue().Set(false);
451505
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// Covers the URL constructor parse paths that avoid a UTF-8 copy and/or
4+
// reuse the input string when it is already a serialized ASCII href.
5+
6+
const{ hasIntl }=require('../common');
7+
constassert=require('assert');
8+
9+
constalreadySerialized=[
10+
'https://nodejs.org/en/blog/',
11+
'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/'+
12+
'/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1'+
13+
'&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&'+
14+
'key=f5c65e1e98fe07e648249ad41e1cfdb0',
15+
'https://user:pass@example.com/path?search=1',
16+
'file:///foo/bar/test/node.js',
17+
'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868',
18+
];
19+
20+
for(consthrefofalreadySerialized){
21+
consturl=newURL(href);
22+
assert.strictEqual(url.href,href);
23+
assert.strictEqual(URL.parse(href).href,href);
24+
assert.strictEqual(URL.canParse(href),true);
25+
}
26+
27+
// Special-scheme URLs with an empty path gain a trailing slash.
28+
{
29+
consturl=newURL('https://example.com');
30+
assert.strictEqual(url.href,'https://example.com/');
31+
assert.strictEqual(url.pathname,'/');
32+
}
33+
34+
// Dot-segment normalization must still rewrite the path.
35+
{
36+
consturl=newURL('https://example.org/./a/../b/./c');
37+
assert.strictEqual(url.href,'https://example.org/b/c');
38+
assert.strictEqual(url.pathname,'/b/c');
39+
}
40+
41+
// Relative resolution against a base URL.
42+
{
43+
consturl=newURL('/path?x=1#h','https://example.com:8443/base');
44+
assert.strictEqual(url.href,'https://example.com:8443/path?x=1#h');
45+
assert.strictEqual(url.host,'example.com:8443');
46+
}
47+
48+
// Non-string input is still stringified.
49+
{
50+
consturl=newURL({toString: ()=>'https://example.com/from-object'});
51+
assert.strictEqual(url.href,'https://example.com/from-object');
52+
}
53+
54+
// Invalid input still throws from the constructor and is null from parse().
55+
{
56+
assert.throws(()=>newURL('not a url'),{
57+
code: 'ERR_INVALID_URL',
58+
name: 'TypeError',
59+
});
60+
assert.strictEqual(URL.parse('not a url'),null);
61+
assert.strictEqual(URL.canParse('not a url'),false);
62+
}
63+
64+
// Unpaired surrogates must not be returned as-is from href.
65+
{
66+
constinput='https://example.com/\uD800';
67+
consturl=newURL(input);
68+
assert.notStrictEqual(url.href,input);
69+
assert.ok(url.href.startsWith('https://example.com/'));
70+
}
71+
72+
if(hasIntl){
73+
consturl=newURL('http://你好你好.在线');
74+
assert.ok(url.hostname.startsWith('xn--'));
75+
assert.ok(url.href.startsWith('http://xn--'));
76+
}
77+
78+
// Setters re-parse the existing href; keep component updates correct.
79+
{
80+
consturl=newURL('https://example.com/old');
81+
url.pathname='/new';
82+
url.search='q=1';
83+
url.hash='frag';
84+
assert.strictEqual(url.href,'https://example.com/new?q=1#frag');
85+
assert.strictEqual(url.pathname,'/new');
86+
assert.strictEqual(url.search,'?q=1');
87+
assert.strictEqual(url.hash,'#frag');
88+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 2a64032

Browse files
anonrigaduh95
authored andcommitted
url: speed up WHATWG URL parsing
Parse one-byte ASCII inputs in place instead of copying them into a UTF-8 buffer, and reuse the original V8 string when the serialized href is unchanged. Delay URLContext allocation until parse finishes and skip ToString when the input is already a string. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Cursor PR-URL: #65361 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent b71d5de commit 2a64032

3 files changed

Lines changed: 215 additions & 69 deletions

File tree

‎lib/internal/url.js‎

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -167,41 +167,62 @@ function lazyCryptoRandom() {
167167
returncryptoRandom;
168168
}
169169

170+
/**
171+
* Copy href and the latest `urlComponents` snapshot into a URLContext.
172+
* Property assignment order matches the historical URLContext fields so
173+
* `util.inspect(..., { showHidden: true })` stays stable.
174+
* @param {object} ctx
175+
* @param {string} href
176+
*/
177+
functionsetURLContextFromBinding(ctx,href){
178+
constc=bindingUrl.urlComponents;
179+
ctx.href=href;
180+
ctx.protocol_end=c[0];
181+
ctx.username_end=c[1];
182+
ctx.host_start=c[2];
183+
ctx.host_end=c[3];
184+
ctx.pathname_start=c[5];
185+
ctx.search_start=c[6];
186+
ctx.hash_start=c[7];
187+
ctx.port=c[4];
188+
ctx.scheme_type=c[8];
189+
}
190+
170191
// This class provides the internal state of a URL object. An instance of this
171192
// class is stored in every URL object and is accessed internally by setters
172193
// and getters. It roughly corresponds to the concept of a URL record in the
173194
// URL Standard, with a few differences. It is also the object transported to
174195
// the C++ binding.
175196
// Refs: https://url.spec.whatwg.org/#concept-url
197+
//
198+
// scheme_type refers to ada::scheme::type:
199+
// HTTP = 0, NOT_SPECIAL = 1, HTTPS = 2, WS = 3, FTP = 4, WSS = 5, FILE = 6
176200
classURLContext{
177201
// This is the maximum value uint32_t can get.
178202
// Ada uses uint32_t(-1) for declaring omitted values.
179203
static #omitted =4294967295;
180204

181-
href='';
182-
protocol_end=0;
183-
username_end=0;
184-
host_start=0;
185-
host_end=0;
186-
pathname_start=0;
187-
search_start=0;
188-
hash_start=0;
189-
port=0;
190205
/**
191-
* Refers to `ada::scheme::type`
192-
*
193-
* enum type : uint8_t {
194-
* HTTP = 0,
195-
* NOT_SPECIAL = 1,
196-
* HTTPS = 2,
197-
* WS = 3,
198-
* FTP = 4,
199-
* WSS = 5,
200-
* FILE = 6
201-
* };
202-
* @type {number}
206+
* @param {string} [href] Parsed href. When omitted, create an empty context
207+
* (used by `URL.parse` on invalid input). When provided, `bindingUrl.parse`
208+
* / `update` has just written `urlComponents`.
203209
*/
204-
scheme_type=1;
210+
constructor(href){
211+
if(href===undefined){
212+
this.href='';
213+
this.protocol_end=0;
214+
this.username_end=0;
215+
this.host_start=0;
216+
this.host_end=0;
217+
this.pathname_start=0;
218+
this.search_start=0;
219+
this.hash_start=0;
220+
this.port=0;
221+
this.scheme_type=1;
222+
return;
223+
}
224+
setURLContextFromBinding(this,href);
225+
}
205226

206227
gethasPort(){
207228
returnthis.port!==URLContext.#omitted;
@@ -835,7 +856,7 @@ const kCreateURLFromPosixPathSymbol = Symbol('kCreateURLFromPosixPath');
835856
constkCreateURLFromWindowsPathSymbol=Symbol('kCreateURLFromWindowsPath');
836857

837858
classURL{
838-
#context=newURLContext();
859+
#context;
839860
#searchParams;
840861
#searchParamsModified;
841862

@@ -860,16 +881,16 @@ class URL {
860881
}
861882

862883
constructor(input,base=undefined,parseSymbol=undefined){
863-
markTransferMode(this,false,false);
864-
865884
if(arguments.length===0){
866885
thrownewERR_MISSING_ARGS('url');
867886
}
868887

869888
// StringPrototypeToWellFormed is not needed.
870-
input=`${input}`;
889+
if(typeofinput!=='string'){
890+
input=`${input}`;
891+
}
871892

872-
if(base!==undefined){
893+
if(base!==undefined&&typeofbase!=='string'){
873894
base=`${base}`;
874895
}
875896

@@ -884,9 +905,12 @@ class URL {
884905
bindingUrl.pathToFileURL(input,interpretAsWindowsPath,base) :
885906
bindingUrl.parse(input,base,raiseException);
886907
}
887-
if(href){
888-
this.#updateContext(href);
889-
}
908+
909+
// Delay context allocation until parse finishes so invalid URLs that
910+
// throw do not pay for an unused URLContext. Initialize in one shot
911+
// from the binding snapshot instead of writing an empty context first.
912+
this.#context =href ? newURLContext(href) : newURLContext();
913+
markTransferMode(this,false,false);
890914
}
891915

892916
staticparse(input,base=undefined){
@@ -955,29 +979,7 @@ class URL {
955979
constpreviousSearch=shouldUpdateSearchParams&&this.#searchParams &&
956980
(this.#searchParamsModified ? this.#getSearchFromParams() : this.#getSearchFromContext());
957981

958-
this.#context.href=href;
959-
960-
const{
961-
0: protocol_end,
962-
1: username_end,
963-
2: host_start,
964-
3: host_end,
965-
4: port,
966-
5: pathname_start,
967-
6: search_start,
968-
7: hash_start,
969-
8: scheme_type,
970-
}=bindingUrl.urlComponents;
971-
972-
this.#context.protocol_end=protocol_end;
973-
this.#context.username_end=username_end;
974-
this.#context.host_start=host_start;
975-
this.#context.host_end=host_end;
976-
this.#context.port=port;
977-
this.#context.pathname_start=pathname_start;
978-
this.#context.search_start=search_start;
979-
this.#context.hash_start=hash_start;
980-
this.#context.scheme_type=scheme_type;
982+
setURLContextFromBinding(this.#context,href);
981983

982984
if(this.#searchParams){
983985
// If the search string has updated, URL becomes the source of truth, and we update URLSearchParams.
@@ -1202,10 +1204,12 @@ class URL {
12021204
thrownewERR_MISSING_ARGS('url');
12031205
}
12041206

1205-
url=`${url}`;
1207+
if(typeofurl!=='string'){
1208+
url=`${url}`;
1209+
}
12061210

12071211
if(base!==undefined){
1208-
returnbindingUrl.canParse(url,`${base}`);
1212+
returnbindingUrl.canParse(url,typeofbase==='string' ? base : `${base}`);
12091213
}
12101214

12111215
// It is important to differentiate the canParse call statements

‎src/node_url.cc‎

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include"node_metadata.h"
99
#include"node_process-inl.h"
1010
#include"path.h"
11+
#include"simdutf.h"
1112
#include"util-inl.h"
1213
#include"v8-fast-api-calls.h"
1314
#include"v8-local-handle.h"
@@ -33,6 +34,38 @@ using v8::SnapshotCreator;
3334
using v8::String;
3435
using v8::Value;
3536

37+
namespace {
38+
39+
// Parse a V8 string as a URL. One-byte ASCII inputs are parsed in place
40+
// without allocating a UTF-8 copy. `reuse_input` is set when the serialized
41+
// href is identical to that ASCII input so the caller can return the original
42+
// V8 string. Non-ASCII inputs are never reused: UTF-8 conversion may replace
43+
// unpaired surrogates, so the original string may not match href.
44+
ada::result<ada::url_aggregator> ParseUrlFromV8String(
45+
Isolate* isolate,
46+
Local<String> input,
47+
const ada::url_aggregator* base_url,
48+
bool* reuse_input) {
49+
{
50+
String::ValueView view(isolate, input);
51+
if (view.is_one_byte()) {
52+
constchar* data = reinterpret_cast<constchar*>(view.data8());
53+
constsize_t length = static_cast<size_t>(view.length());
54+
if (simdutf::validate_ascii(data, length)) [[likely]] {
55+
const std::string_view input_view(data, length);
56+
auto out = ada::parse<ada::url_aggregator>(input_view, base_url);
57+
*reuse_input = out.has_value() && out->get_href() == input_view;
58+
return out;
59+
}
60+
}
61+
}
62+
*reuse_input = false;
63+
Utf8Value utf8(isolate, input);
64+
return ada::parse<ada::url_aggregator>(utf8.ToStringView(), base_url);
65+
}
66+
67+
} // namespace
68+
3669
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
3770
tracker->TrackField("url_components_buffer", url_components_buffer_);
3871
}
@@ -392,32 +425,51 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
392425
Realm* realm = Realm::GetCurrent(args);
393426
BindingData* binding_data = realm->GetBindingData<BindingData>();
394427
Isolate* isolate = realm->isolate();
395-
std::optional<std::string> base_{};
428+
Local<String> input_string = args[0].As<String>();
396429

397-
Utf8Value input(isolate, args[0]);
398430
ada::result<ada::url_aggregator> base;
399431
ada::url_aggregator* base_pointer = nullptr;
400432
if (args[1]->IsString()) {
401-
base_ = Utf8Value(isolate, args[1]).ToString();
402-
base = ada::parse<ada::url_aggregator>(*base_);
403-
if (!base && raise_exception) {
404-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
405-
} elseif (!base) {
433+
bool unused_reuse = false;
434+
base = ParseUrlFromV8String(
435+
isolate, args[1].As<String>(), nullptr, &unused_reuse);
436+
if (!base) {
437+
if (raise_exception) {
438+
Utf8Value input(isolate, input_string);
439+
Utf8Value base_utf8(isolate, args[1]);
440+
returnThrowInvalidURL(
441+
realm->env(), input.ToStringView(), base_utf8.ToString());
442+
}
406443
return;
407444
}
408445
base_pointer = &base.value();
409446
}
410-
auto out =
411-
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);
412447

413-
if (!out && raise_exception) {
414-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
415-
} elseif (!out) {
448+
bool reuse_input = false;
449+
auto out =
450+
ParseUrlFromV8String(isolate, input_string, base_pointer, &reuse_input);
451+
if (!out) {
452+
if (raise_exception) {
453+
Utf8Value input(isolate, input_string);
454+
std::optional<std::string> base_error;
455+
if (args[1]->IsString()) {
456+
base_error = Utf8Value(isolate, args[1]).ToString();
457+
}
458+
returnThrowInvalidURL(
459+
realm->env(), input.ToStringView(), std::move(base_error));
460+
}
416461
return;
417462
}
418463

419464
binding_data->UpdateComponents(out->get_components(), out->type);
420465

466+
// Already-serialized ASCII URLs are the common case. Reuse the input
467+
// string instead of allocating an identical V8 string from href.
468+
if (reuse_input) {
469+
args.GetReturnValue().Set(args[0]);
470+
return;
471+
}
472+
421473
Local<Value> ret;
422474
if (ToV8Value(realm->context(), out->get_href(), isolate).ToLocal(&ret))
423475
[[likely]] {
@@ -439,13 +491,15 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
439491
return;
440492
}
441493
enum url_update_action action = static_cast<enum url_update_action>(val);
442-
Utf8Value input(isolate, args[0].As<String>());
443494
Utf8Value new_value(isolate, args[2].As<String>());
444495

445496
std::string_view new_value_view = new_value.ToStringView();
446497
// A serialized URL is not always reparsable: the IDNA encoder can emit a
447498
// host label that the decoder rejects. Fail the update instead of crashing.
448-
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
499+
// Existing hrefs are typically already-serialized ASCII, so parse in place.
500+
bool unused_reuse = false;
501+
auto out = ParseUrlFromV8String(
502+
isolate, args[0].As<String>(), nullptr, &unused_reuse);
449503
if (!out) {
450504
return args.GetReturnValue().Set(false);
451505
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// Covers the URL constructor parse paths that avoid a UTF-8 copy and/or
4+
// reuse the input string when it is already a serialized ASCII href.
5+
6+
const{ hasIntl }=require('../common');
7+
constassert=require('assert');
8+
9+
constalreadySerialized=[
10+
'https://nodejs.org/en/blog/',
11+
'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/'+
12+
'/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1'+
13+
'&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&'+
14+
'key=f5c65e1e98fe07e648249ad41e1cfdb0',
15+
'https://user:pass@example.com/path?search=1',
16+
'file:///foo/bar/test/node.js',
17+
'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868',
18+
];
19+
20+
for(consthrefofalreadySerialized){
21+
consturl=newURL(href);
22+
assert.strictEqual(url.href,href);
23+
assert.strictEqual(URL.parse(href).href,href);
24+
assert.strictEqual(URL.canParse(href),true);
25+
}
26+
27+
// Special-scheme URLs with an empty path gain a trailing slash.
28+
{
29+
consturl=newURL('https://example.com');
30+
assert.strictEqual(url.href,'https://example.com/');
31+
assert.strictEqual(url.pathname,'/');
32+
}
33+
34+
// Dot-segment normalization must still rewrite the path.
35+
{
36+
consturl=newURL('https://example.org/./a/../b/./c');
37+
assert.strictEqual(url.href,'https://example.org/b/c');
38+
assert.strictEqual(url.pathname,'/b/c');
39+
}
40+
41+
// Relative resolution against a base URL.
42+
{
43+
consturl=newURL('/path?x=1#h','https://example.com:8443/base');
44+
assert.strictEqual(url.href,'https://example.com:8443/path?x=1#h');
45+
assert.strictEqual(url.host,'example.com:8443');
46+
}
47+
48+
// Non-string input is still stringified.
49+
{
50+
consturl=newURL({toString: ()=>'https://example.com/from-object'});
51+
assert.strictEqual(url.href,'https://example.com/from-object');
52+
}
53+
54+
// Invalid input still throws from the constructor and is null from parse().
55+
{
56+
assert.throws(()=>newURL('not a url'),{
57+
code: 'ERR_INVALID_URL',
58+
name: 'TypeError',
59+
});
60+
assert.strictEqual(URL.parse('not a url'),null);
61+
assert.strictEqual(URL.canParse('not a url'),false);
62+
}
63+
64+
// Unpaired surrogates must not be returned as-is from href.
65+
{
66+
constinput='https://example.com/\uD800';
67+
consturl=newURL(input);
68+
assert.notStrictEqual(url.href,input);
69+
assert.ok(url.href.startsWith('https://example.com/'));
70+
}
71+
72+
if(hasIntl){
73+
consturl=newURL('http://你好你好.在线');
74+
assert.ok(url.hostname.startsWith('xn--'));
75+
assert.ok(url.href.startsWith('http://xn--'));
76+
}
77+
78+
// Setters re-parse the existing href; keep component updates correct.
79+
{
80+
consturl=newURL('https://example.com/old');
81+
url.pathname='/new';
82+
url.search='q=1';
83+
url.hash='frag';
84+
assert.strictEqual(url.href,'https://example.com/new?q=1#frag');
85+
assert.strictEqual(url.pathname,'/new');
86+
assert.strictEqual(url.search,'?q=1');
87+
assert.strictEqual(url.hash,'#frag');
88+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit 2a64032

Browse files
anonrigaduh95
authored andcommitted
url: speed up WHATWG URL parsing
Parse one-byte ASCII inputs in place instead of copying them into a UTF-8 buffer, and reuse the original V8 string when the serialized href is unchanged. Delay URLContext allocation until parse finishes and skip ToString when the input is already a string. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Cursor PR-URL: #65361 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent b71d5de commit 2a64032

3 files changed

Lines changed: 215 additions & 69 deletions

File tree

‎lib/internal/url.js‎

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -167,41 +167,62 @@ function lazyCryptoRandom() {
167167
returncryptoRandom;
168168
}
169169

170+
/**
171+
* Copy href and the latest `urlComponents` snapshot into a URLContext.
172+
* Property assignment order matches the historical URLContext fields so
173+
* `util.inspect(..., { showHidden: true })` stays stable.
174+
* @param {object} ctx
175+
* @param {string} href
176+
*/
177+
functionsetURLContextFromBinding(ctx,href){
178+
constc=bindingUrl.urlComponents;
179+
ctx.href=href;
180+
ctx.protocol_end=c[0];
181+
ctx.username_end=c[1];
182+
ctx.host_start=c[2];
183+
ctx.host_end=c[3];
184+
ctx.pathname_start=c[5];
185+
ctx.search_start=c[6];
186+
ctx.hash_start=c[7];
187+
ctx.port=c[4];
188+
ctx.scheme_type=c[8];
189+
}
190+
170191
// This class provides the internal state of a URL object. An instance of this
171192
// class is stored in every URL object and is accessed internally by setters
172193
// and getters. It roughly corresponds to the concept of a URL record in the
173194
// URL Standard, with a few differences. It is also the object transported to
174195
// the C++ binding.
175196
// Refs: https://url.spec.whatwg.org/#concept-url
197+
//
198+
// scheme_type refers to ada::scheme::type:
199+
// HTTP = 0, NOT_SPECIAL = 1, HTTPS = 2, WS = 3, FTP = 4, WSS = 5, FILE = 6
176200
classURLContext{
177201
// This is the maximum value uint32_t can get.
178202
// Ada uses uint32_t(-1) for declaring omitted values.
179203
static #omitted =4294967295;
180204

181-
href='';
182-
protocol_end=0;
183-
username_end=0;
184-
host_start=0;
185-
host_end=0;
186-
pathname_start=0;
187-
search_start=0;
188-
hash_start=0;
189-
port=0;
190205
/**
191-
* Refers to `ada::scheme::type`
192-
*
193-
* enum type : uint8_t {
194-
* HTTP = 0,
195-
* NOT_SPECIAL = 1,
196-
* HTTPS = 2,
197-
* WS = 3,
198-
* FTP = 4,
199-
* WSS = 5,
200-
* FILE = 6
201-
* };
202-
* @type {number}
206+
* @param {string} [href] Parsed href. When omitted, create an empty context
207+
* (used by `URL.parse` on invalid input). When provided, `bindingUrl.parse`
208+
* / `update` has just written `urlComponents`.
203209
*/
204-
scheme_type=1;
210+
constructor(href){
211+
if(href===undefined){
212+
this.href='';
213+
this.protocol_end=0;
214+
this.username_end=0;
215+
this.host_start=0;
216+
this.host_end=0;
217+
this.pathname_start=0;
218+
this.search_start=0;
219+
this.hash_start=0;
220+
this.port=0;
221+
this.scheme_type=1;
222+
return;
223+
}
224+
setURLContextFromBinding(this,href);
225+
}
205226

206227
gethasPort(){
207228
returnthis.port!==URLContext.#omitted;
@@ -835,7 +856,7 @@ const kCreateURLFromPosixPathSymbol = Symbol('kCreateURLFromPosixPath');
835856
constkCreateURLFromWindowsPathSymbol=Symbol('kCreateURLFromWindowsPath');
836857

837858
classURL{
838-
#context=newURLContext();
859+
#context;
839860
#searchParams;
840861
#searchParamsModified;
841862

@@ -860,16 +881,16 @@ class URL {
860881
}
861882

862883
constructor(input,base=undefined,parseSymbol=undefined){
863-
markTransferMode(this,false,false);
864-
865884
if(arguments.length===0){
866885
thrownewERR_MISSING_ARGS('url');
867886
}
868887

869888
// StringPrototypeToWellFormed is not needed.
870-
input=`${input}`;
889+
if(typeofinput!=='string'){
890+
input=`${input}`;
891+
}
871892

872-
if(base!==undefined){
893+
if(base!==undefined&&typeofbase!=='string'){
873894
base=`${base}`;
874895
}
875896

@@ -884,9 +905,12 @@ class URL {
884905
bindingUrl.pathToFileURL(input,interpretAsWindowsPath,base) :
885906
bindingUrl.parse(input,base,raiseException);
886907
}
887-
if(href){
888-
this.#updateContext(href);
889-
}
908+
909+
// Delay context allocation until parse finishes so invalid URLs that
910+
// throw do not pay for an unused URLContext. Initialize in one shot
911+
// from the binding snapshot instead of writing an empty context first.
912+
this.#context =href ? newURLContext(href) : newURLContext();
913+
markTransferMode(this,false,false);
890914
}
891915

892916
staticparse(input,base=undefined){
@@ -955,29 +979,7 @@ class URL {
955979
constpreviousSearch=shouldUpdateSearchParams&&this.#searchParams &&
956980
(this.#searchParamsModified ? this.#getSearchFromParams() : this.#getSearchFromContext());
957981

958-
this.#context.href=href;
959-
960-
const{
961-
0: protocol_end,
962-
1: username_end,
963-
2: host_start,
964-
3: host_end,
965-
4: port,
966-
5: pathname_start,
967-
6: search_start,
968-
7: hash_start,
969-
8: scheme_type,
970-
}=bindingUrl.urlComponents;
971-
972-
this.#context.protocol_end=protocol_end;
973-
this.#context.username_end=username_end;
974-
this.#context.host_start=host_start;
975-
this.#context.host_end=host_end;
976-
this.#context.port=port;
977-
this.#context.pathname_start=pathname_start;
978-
this.#context.search_start=search_start;
979-
this.#context.hash_start=hash_start;
980-
this.#context.scheme_type=scheme_type;
982+
setURLContextFromBinding(this.#context,href);
981983

982984
if(this.#searchParams){
983985
// If the search string has updated, URL becomes the source of truth, and we update URLSearchParams.
@@ -1202,10 +1204,12 @@ class URL {
12021204
thrownewERR_MISSING_ARGS('url');
12031205
}
12041206

1205-
url=`${url}`;
1207+
if(typeofurl!=='string'){
1208+
url=`${url}`;
1209+
}
12061210

12071211
if(base!==undefined){
1208-
returnbindingUrl.canParse(url,`${base}`);
1212+
returnbindingUrl.canParse(url,typeofbase==='string' ? base : `${base}`);
12091213
}
12101214

12111215
// It is important to differentiate the canParse call statements

‎src/node_url.cc‎

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include"node_metadata.h"
99
#include"node_process-inl.h"
1010
#include"path.h"
11+
#include"simdutf.h"
1112
#include"util-inl.h"
1213
#include"v8-fast-api-calls.h"
1314
#include"v8-local-handle.h"
@@ -33,6 +34,38 @@ using v8::SnapshotCreator;
3334
using v8::String;
3435
using v8::Value;
3536

37+
namespace {
38+
39+
// Parse a V8 string as a URL. One-byte ASCII inputs are parsed in place
40+
// without allocating a UTF-8 copy. `reuse_input` is set when the serialized
41+
// href is identical to that ASCII input so the caller can return the original
42+
// V8 string. Non-ASCII inputs are never reused: UTF-8 conversion may replace
43+
// unpaired surrogates, so the original string may not match href.
44+
ada::result<ada::url_aggregator> ParseUrlFromV8String(
45+
Isolate* isolate,
46+
Local<String> input,
47+
const ada::url_aggregator* base_url,
48+
bool* reuse_input) {
49+
{
50+
String::ValueView view(isolate, input);
51+
if (view.is_one_byte()) {
52+
constchar* data = reinterpret_cast<constchar*>(view.data8());
53+
constsize_t length = static_cast<size_t>(view.length());
54+
if (simdutf::validate_ascii(data, length)) [[likely]] {
55+
const std::string_view input_view(data, length);
56+
auto out = ada::parse<ada::url_aggregator>(input_view, base_url);
57+
*reuse_input = out.has_value() && out->get_href() == input_view;
58+
return out;
59+
}
60+
}
61+
}
62+
*reuse_input = false;
63+
Utf8Value utf8(isolate, input);
64+
return ada::parse<ada::url_aggregator>(utf8.ToStringView(), base_url);
65+
}
66+
67+
} // namespace
68+
3669
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
3770
tracker->TrackField("url_components_buffer", url_components_buffer_);
3871
}
@@ -392,32 +425,51 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
392425
Realm* realm = Realm::GetCurrent(args);
393426
BindingData* binding_data = realm->GetBindingData<BindingData>();
394427
Isolate* isolate = realm->isolate();
395-
std::optional<std::string> base_{};
428+
Local<String> input_string = args[0].As<String>();
396429

397-
Utf8Value input(isolate, args[0]);
398430
ada::result<ada::url_aggregator> base;
399431
ada::url_aggregator* base_pointer = nullptr;
400432
if (args[1]->IsString()) {
401-
base_ = Utf8Value(isolate, args[1]).ToString();
402-
base = ada::parse<ada::url_aggregator>(*base_);
403-
if (!base && raise_exception) {
404-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
405-
} elseif (!base) {
433+
bool unused_reuse = false;
434+
base = ParseUrlFromV8String(
435+
isolate, args[1].As<String>(), nullptr, &unused_reuse);
436+
if (!base) {
437+
if (raise_exception) {
438+
Utf8Value input(isolate, input_string);
439+
Utf8Value base_utf8(isolate, args[1]);
440+
returnThrowInvalidURL(
441+
realm->env(), input.ToStringView(), base_utf8.ToString());
442+
}
406443
return;
407444
}
408445
base_pointer = &base.value();
409446
}
410-
auto out =
411-
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);
412447

413-
if (!out && raise_exception) {
414-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
415-
} elseif (!out) {
448+
bool reuse_input = false;
449+
auto out =
450+
ParseUrlFromV8String(isolate, input_string, base_pointer, &reuse_input);
451+
if (!out) {
452+
if (raise_exception) {
453+
Utf8Value input(isolate, input_string);
454+
std::optional<std::string> base_error;
455+
if (args[1]->IsString()) {
456+
base_error = Utf8Value(isolate, args[1]).ToString();
457+
}
458+
returnThrowInvalidURL(
459+
realm->env(), input.ToStringView(), std::move(base_error));
460+
}
416461
return;
417462
}
418463

419464
binding_data->UpdateComponents(out->get_components(), out->type);
420465

466+
// Already-serialized ASCII URLs are the common case. Reuse the input
467+
// string instead of allocating an identical V8 string from href.
468+
if (reuse_input) {
469+
args.GetReturnValue().Set(args[0]);
470+
return;
471+
}
472+
421473
Local<Value> ret;
422474
if (ToV8Value(realm->context(), out->get_href(), isolate).ToLocal(&ret))
423475
[[likely]] {
@@ -439,13 +491,15 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
439491
return;
440492
}
441493
enum url_update_action action = static_cast<enum url_update_action>(val);
442-
Utf8Value input(isolate, args[0].As<String>());
443494
Utf8Value new_value(isolate, args[2].As<String>());
444495

445496
std::string_view new_value_view = new_value.ToStringView();
446497
// A serialized URL is not always reparsable: the IDNA encoder can emit a
447498
// host label that the decoder rejects. Fail the update instead of crashing.
448-
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
499+
// Existing hrefs are typically already-serialized ASCII, so parse in place.
500+
bool unused_reuse = false;
501+
auto out = ParseUrlFromV8String(
502+
isolate, args[0].As<String>(), nullptr, &unused_reuse);
449503
if (!out) {
450504
return args.GetReturnValue().Set(false);
451505
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// Covers the URL constructor parse paths that avoid a UTF-8 copy and/or
4+
// reuse the input string when it is already a serialized ASCII href.
5+
6+
const{ hasIntl }=require('../common');
7+
constassert=require('assert');
8+
9+
constalreadySerialized=[
10+
'https://nodejs.org/en/blog/',
11+
'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/'+
12+
'/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1'+
13+
'&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&'+
14+
'key=f5c65e1e98fe07e648249ad41e1cfdb0',
15+
'https://user:pass@example.com/path?search=1',
16+
'file:///foo/bar/test/node.js',
17+
'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868',
18+
];
19+
20+
for(consthrefofalreadySerialized){
21+
consturl=newURL(href);
22+
assert.strictEqual(url.href,href);
23+
assert.strictEqual(URL.parse(href).href,href);
24+
assert.strictEqual(URL.canParse(href),true);
25+
}
26+
27+
// Special-scheme URLs with an empty path gain a trailing slash.
28+
{
29+
consturl=newURL('https://example.com');
30+
assert.strictEqual(url.href,'https://example.com/');
31+
assert.strictEqual(url.pathname,'/');
32+
}
33+
34+
// Dot-segment normalization must still rewrite the path.
35+
{
36+
consturl=newURL('https://example.org/./a/../b/./c');
37+
assert.strictEqual(url.href,'https://example.org/b/c');
38+
assert.strictEqual(url.pathname,'/b/c');
39+
}
40+
41+
// Relative resolution against a base URL.
42+
{
43+
consturl=newURL('/path?x=1#h','https://example.com:8443/base');
44+
assert.strictEqual(url.href,'https://example.com:8443/path?x=1#h');
45+
assert.strictEqual(url.host,'example.com:8443');
46+
}
47+
48+
// Non-string input is still stringified.
49+
{
50+
consturl=newURL({toString: ()=>'https://example.com/from-object'});
51+
assert.strictEqual(url.href,'https://example.com/from-object');
52+
}
53+
54+
// Invalid input still throws from the constructor and is null from parse().
55+
{
56+
assert.throws(()=>newURL('not a url'),{
57+
code: 'ERR_INVALID_URL',
58+
name: 'TypeError',
59+
});
60+
assert.strictEqual(URL.parse('not a url'),null);
61+
assert.strictEqual(URL.canParse('not a url'),false);
62+
}
63+
64+
// Unpaired surrogates must not be returned as-is from href.
65+
{
66+
constinput='https://example.com/\uD800';
67+
consturl=newURL(input);
68+
assert.notStrictEqual(url.href,input);
69+
assert.ok(url.href.startsWith('https://example.com/'));
70+
}
71+
72+
if(hasIntl){
73+
consturl=newURL('http://你好你好.在线');
74+
assert.ok(url.hostname.startsWith('xn--'));
75+
assert.ok(url.href.startsWith('http://xn--'));
76+
}
77+
78+
// Setters re-parse the existing href; keep component updates correct.
79+
{
80+
consturl=newURL('https://example.com/old');
81+
url.pathname='/new';
82+
url.search='q=1';
83+
url.hash='frag';
84+
assert.strictEqual(url.href,'https://example.com/new?q=1#frag');
85+
assert.strictEqual(url.pathname,'/new');
86+
assert.strictEqual(url.search,'?q=1');
87+
assert.strictEqual(url.hash,'#frag');
88+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 2a64032

Browse files
anonrigaduh95
authored andcommitted
url: speed up WHATWG URL parsing
Parse one-byte ASCII inputs in place instead of copying them into a UTF-8 buffer, and reuse the original V8 string when the serialized href is unchanged. Delay URLContext allocation until parse finishes and skip ToString when the input is already a string. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Cursor PR-URL: #65361 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent b71d5de commit 2a64032

3 files changed

Lines changed: 215 additions & 69 deletions

File tree

‎lib/internal/url.js‎

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -167,41 +167,62 @@ function lazyCryptoRandom() {
167167
returncryptoRandom;
168168
}
169169

170+
/**
171+
* Copy href and the latest `urlComponents` snapshot into a URLContext.
172+
* Property assignment order matches the historical URLContext fields so
173+
* `util.inspect(..., { showHidden: true })` stays stable.
174+
* @param {object} ctx
175+
* @param {string} href
176+
*/
177+
functionsetURLContextFromBinding(ctx,href){
178+
constc=bindingUrl.urlComponents;
179+
ctx.href=href;
180+
ctx.protocol_end=c[0];
181+
ctx.username_end=c[1];
182+
ctx.host_start=c[2];
183+
ctx.host_end=c[3];
184+
ctx.pathname_start=c[5];
185+
ctx.search_start=c[6];
186+
ctx.hash_start=c[7];
187+
ctx.port=c[4];
188+
ctx.scheme_type=c[8];
189+
}
190+
170191
// This class provides the internal state of a URL object. An instance of this
171192
// class is stored in every URL object and is accessed internally by setters
172193
// and getters. It roughly corresponds to the concept of a URL record in the
173194
// URL Standard, with a few differences. It is also the object transported to
174195
// the C++ binding.
175196
// Refs: https://url.spec.whatwg.org/#concept-url
197+
//
198+
// scheme_type refers to ada::scheme::type:
199+
// HTTP = 0, NOT_SPECIAL = 1, HTTPS = 2, WS = 3, FTP = 4, WSS = 5, FILE = 6
176200
classURLContext{
177201
// This is the maximum value uint32_t can get.
178202
// Ada uses uint32_t(-1) for declaring omitted values.
179203
static #omitted =4294967295;
180204

181-
href='';
182-
protocol_end=0;
183-
username_end=0;
184-
host_start=0;
185-
host_end=0;
186-
pathname_start=0;
187-
search_start=0;
188-
hash_start=0;
189-
port=0;
190205
/**
191-
* Refers to `ada::scheme::type`
192-
*
193-
* enum type : uint8_t {
194-
* HTTP = 0,
195-
* NOT_SPECIAL = 1,
196-
* HTTPS = 2,
197-
* WS = 3,
198-
* FTP = 4,
199-
* WSS = 5,
200-
* FILE = 6
201-
* };
202-
* @type {number}
206+
* @param {string} [href] Parsed href. When omitted, create an empty context
207+
* (used by `URL.parse` on invalid input). When provided, `bindingUrl.parse`
208+
* / `update` has just written `urlComponents`.
203209
*/
204-
scheme_type=1;
210+
constructor(href){
211+
if(href===undefined){
212+
this.href='';
213+
this.protocol_end=0;
214+
this.username_end=0;
215+
this.host_start=0;
216+
this.host_end=0;
217+
this.pathname_start=0;
218+
this.search_start=0;
219+
this.hash_start=0;
220+
this.port=0;
221+
this.scheme_type=1;
222+
return;
223+
}
224+
setURLContextFromBinding(this,href);
225+
}
205226

206227
gethasPort(){
207228
returnthis.port!==URLContext.#omitted;
@@ -835,7 +856,7 @@ const kCreateURLFromPosixPathSymbol = Symbol('kCreateURLFromPosixPath');
835856
constkCreateURLFromWindowsPathSymbol=Symbol('kCreateURLFromWindowsPath');
836857

837858
classURL{
838-
#context=newURLContext();
859+
#context;
839860
#searchParams;
840861
#searchParamsModified;
841862

@@ -860,16 +881,16 @@ class URL {
860881
}
861882

862883
constructor(input,base=undefined,parseSymbol=undefined){
863-
markTransferMode(this,false,false);
864-
865884
if(arguments.length===0){
866885
thrownewERR_MISSING_ARGS('url');
867886
}
868887

869888
// StringPrototypeToWellFormed is not needed.
870-
input=`${input}`;
889+
if(typeofinput!=='string'){
890+
input=`${input}`;
891+
}
871892

872-
if(base!==undefined){
893+
if(base!==undefined&&typeofbase!=='string'){
873894
base=`${base}`;
874895
}
875896

@@ -884,9 +905,12 @@ class URL {
884905
bindingUrl.pathToFileURL(input,interpretAsWindowsPath,base) :
885906
bindingUrl.parse(input,base,raiseException);
886907
}
887-
if(href){
888-
this.#updateContext(href);
889-
}
908+
909+
// Delay context allocation until parse finishes so invalid URLs that
910+
// throw do not pay for an unused URLContext. Initialize in one shot
911+
// from the binding snapshot instead of writing an empty context first.
912+
this.#context =href ? newURLContext(href) : newURLContext();
913+
markTransferMode(this,false,false);
890914
}
891915

892916
staticparse(input,base=undefined){
@@ -955,29 +979,7 @@ class URL {
955979
constpreviousSearch=shouldUpdateSearchParams&&this.#searchParams &&
956980
(this.#searchParamsModified ? this.#getSearchFromParams() : this.#getSearchFromContext());
957981

958-
this.#context.href=href;
959-
960-
const{
961-
0: protocol_end,
962-
1: username_end,
963-
2: host_start,
964-
3: host_end,
965-
4: port,
966-
5: pathname_start,
967-
6: search_start,
968-
7: hash_start,
969-
8: scheme_type,
970-
}=bindingUrl.urlComponents;
971-
972-
this.#context.protocol_end=protocol_end;
973-
this.#context.username_end=username_end;
974-
this.#context.host_start=host_start;
975-
this.#context.host_end=host_end;
976-
this.#context.port=port;
977-
this.#context.pathname_start=pathname_start;
978-
this.#context.search_start=search_start;
979-
this.#context.hash_start=hash_start;
980-
this.#context.scheme_type=scheme_type;
982+
setURLContextFromBinding(this.#context,href);
981983

982984
if(this.#searchParams){
983985
// If the search string has updated, URL becomes the source of truth, and we update URLSearchParams.
@@ -1202,10 +1204,12 @@ class URL {
12021204
thrownewERR_MISSING_ARGS('url');
12031205
}
12041206

1205-
url=`${url}`;
1207+
if(typeofurl!=='string'){
1208+
url=`${url}`;
1209+
}
12061210

12071211
if(base!==undefined){
1208-
returnbindingUrl.canParse(url,`${base}`);
1212+
returnbindingUrl.canParse(url,typeofbase==='string' ? base : `${base}`);
12091213
}
12101214

12111215
// It is important to differentiate the canParse call statements

‎src/node_url.cc‎

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include"node_metadata.h"
99
#include"node_process-inl.h"
1010
#include"path.h"
11+
#include"simdutf.h"
1112
#include"util-inl.h"
1213
#include"v8-fast-api-calls.h"
1314
#include"v8-local-handle.h"
@@ -33,6 +34,38 @@ using v8::SnapshotCreator;
3334
using v8::String;
3435
using v8::Value;
3536

37+
namespace {
38+
39+
// Parse a V8 string as a URL. One-byte ASCII inputs are parsed in place
40+
// without allocating a UTF-8 copy. `reuse_input` is set when the serialized
41+
// href is identical to that ASCII input so the caller can return the original
42+
// V8 string. Non-ASCII inputs are never reused: UTF-8 conversion may replace
43+
// unpaired surrogates, so the original string may not match href.
44+
ada::result<ada::url_aggregator> ParseUrlFromV8String(
45+
Isolate* isolate,
46+
Local<String> input,
47+
const ada::url_aggregator* base_url,
48+
bool* reuse_input) {
49+
{
50+
String::ValueView view(isolate, input);
51+
if (view.is_one_byte()) {
52+
constchar* data = reinterpret_cast<constchar*>(view.data8());
53+
constsize_t length = static_cast<size_t>(view.length());
54+
if (simdutf::validate_ascii(data, length)) [[likely]] {
55+
const std::string_view input_view(data, length);
56+
auto out = ada::parse<ada::url_aggregator>(input_view, base_url);
57+
*reuse_input = out.has_value() && out->get_href() == input_view;
58+
return out;
59+
}
60+
}
61+
}
62+
*reuse_input = false;
63+
Utf8Value utf8(isolate, input);
64+
return ada::parse<ada::url_aggregator>(utf8.ToStringView(), base_url);
65+
}
66+
67+
} // namespace
68+
3669
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
3770
tracker->TrackField("url_components_buffer", url_components_buffer_);
3871
}
@@ -392,32 +425,51 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
392425
Realm* realm = Realm::GetCurrent(args);
393426
BindingData* binding_data = realm->GetBindingData<BindingData>();
394427
Isolate* isolate = realm->isolate();
395-
std::optional<std::string> base_{};
428+
Local<String> input_string = args[0].As<String>();
396429

397-
Utf8Value input(isolate, args[0]);
398430
ada::result<ada::url_aggregator> base;
399431
ada::url_aggregator* base_pointer = nullptr;
400432
if (args[1]->IsString()) {
401-
base_ = Utf8Value(isolate, args[1]).ToString();
402-
base = ada::parse<ada::url_aggregator>(*base_);
403-
if (!base && raise_exception) {
404-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
405-
} elseif (!base) {
433+
bool unused_reuse = false;
434+
base = ParseUrlFromV8String(
435+
isolate, args[1].As<String>(), nullptr, &unused_reuse);
436+
if (!base) {
437+
if (raise_exception) {
438+
Utf8Value input(isolate, input_string);
439+
Utf8Value base_utf8(isolate, args[1]);
440+
returnThrowInvalidURL(
441+
realm->env(), input.ToStringView(), base_utf8.ToString());
442+
}
406443
return;
407444
}
408445
base_pointer = &base.value();
409446
}
410-
auto out =
411-
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);
412447

413-
if (!out && raise_exception) {
414-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
415-
} elseif (!out) {
448+
bool reuse_input = false;
449+
auto out =
450+
ParseUrlFromV8String(isolate, input_string, base_pointer, &reuse_input);
451+
if (!out) {
452+
if (raise_exception) {
453+
Utf8Value input(isolate, input_string);
454+
std::optional<std::string> base_error;
455+
if (args[1]->IsString()) {
456+
base_error = Utf8Value(isolate, args[1]).ToString();
457+
}
458+
returnThrowInvalidURL(
459+
realm->env(), input.ToStringView(), std::move(base_error));
460+
}
416461
return;
417462
}
418463

419464
binding_data->UpdateComponents(out->get_components(), out->type);
420465

466+
// Already-serialized ASCII URLs are the common case. Reuse the input
467+
// string instead of allocating an identical V8 string from href.
468+
if (reuse_input) {
469+
args.GetReturnValue().Set(args[0]);
470+
return;
471+
}
472+
421473
Local<Value> ret;
422474
if (ToV8Value(realm->context(), out->get_href(), isolate).ToLocal(&ret))
423475
[[likely]] {
@@ -439,13 +491,15 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
439491
return;
440492
}
441493
enum url_update_action action = static_cast<enum url_update_action>(val);
442-
Utf8Value input(isolate, args[0].As<String>());
443494
Utf8Value new_value(isolate, args[2].As<String>());
444495

445496
std::string_view new_value_view = new_value.ToStringView();
446497
// A serialized URL is not always reparsable: the IDNA encoder can emit a
447498
// host label that the decoder rejects. Fail the update instead of crashing.
448-
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
499+
// Existing hrefs are typically already-serialized ASCII, so parse in place.
500+
bool unused_reuse = false;
501+
auto out = ParseUrlFromV8String(
502+
isolate, args[0].As<String>(), nullptr, &unused_reuse);
449503
if (!out) {
450504
return args.GetReturnValue().Set(false);
451505
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// Covers the URL constructor parse paths that avoid a UTF-8 copy and/or
4+
// reuse the input string when it is already a serialized ASCII href.
5+
6+
const{ hasIntl }=require('../common');
7+
constassert=require('assert');
8+
9+
constalreadySerialized=[
10+
'https://nodejs.org/en/blog/',
11+
'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/'+
12+
'/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1'+
13+
'&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&'+
14+
'key=f5c65e1e98fe07e648249ad41e1cfdb0',
15+
'https://user:pass@example.com/path?search=1',
16+
'file:///foo/bar/test/node.js',
17+
'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868',
18+
];
19+
20+
for(consthrefofalreadySerialized){
21+
consturl=newURL(href);
22+
assert.strictEqual(url.href,href);
23+
assert.strictEqual(URL.parse(href).href,href);
24+
assert.strictEqual(URL.canParse(href),true);
25+
}
26+
27+
// Special-scheme URLs with an empty path gain a trailing slash.
28+
{
29+
consturl=newURL('https://example.com');
30+
assert.strictEqual(url.href,'https://example.com/');
31+
assert.strictEqual(url.pathname,'/');
32+
}
33+
34+
// Dot-segment normalization must still rewrite the path.
35+
{
36+
consturl=newURL('https://example.org/./a/../b/./c');
37+
assert.strictEqual(url.href,'https://example.org/b/c');
38+
assert.strictEqual(url.pathname,'/b/c');
39+
}
40+
41+
// Relative resolution against a base URL.
42+
{
43+
consturl=newURL('/path?x=1#h','https://example.com:8443/base');
44+
assert.strictEqual(url.href,'https://example.com:8443/path?x=1#h');
45+
assert.strictEqual(url.host,'example.com:8443');
46+
}
47+
48+
// Non-string input is still stringified.
49+
{
50+
consturl=newURL({toString: ()=>'https://example.com/from-object'});
51+
assert.strictEqual(url.href,'https://example.com/from-object');
52+
}
53+
54+
// Invalid input still throws from the constructor and is null from parse().
55+
{
56+
assert.throws(()=>newURL('not a url'),{
57+
code: 'ERR_INVALID_URL',
58+
name: 'TypeError',
59+
});
60+
assert.strictEqual(URL.parse('not a url'),null);
61+
assert.strictEqual(URL.canParse('not a url'),false);
62+
}
63+
64+
// Unpaired surrogates must not be returned as-is from href.
65+
{
66+
constinput='https://example.com/\uD800';
67+
consturl=newURL(input);
68+
assert.notStrictEqual(url.href,input);
69+
assert.ok(url.href.startsWith('https://example.com/'));
70+
}
71+
72+
if(hasIntl){
73+
consturl=newURL('http://你好你好.在线');
74+
assert.ok(url.hostname.startsWith('xn--'));
75+
assert.ok(url.href.startsWith('http://xn--'));
76+
}
77+
78+
// Setters re-parse the existing href; keep component updates correct.
79+
{
80+
consturl=newURL('https://example.com/old');
81+
url.pathname='/new';
82+
url.search='q=1';
83+
url.hash='frag';
84+
assert.strictEqual(url.href,'https://example.com/new?q=1#frag');
85+
assert.strictEqual(url.pathname,'/new');
86+
assert.strictEqual(url.search,'?q=1');
87+
assert.strictEqual(url.hash,'#frag');
88+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 2a64032

Browse files
anonrigaduh95
authored andcommitted
url: speed up WHATWG URL parsing
Parse one-byte ASCII inputs in place instead of copying them into a UTF-8 buffer, and reuse the original V8 string when the serialized href is unchanged. Delay URLContext allocation until parse finishes and skip ToString when the input is already a string. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Cursor PR-URL: #65361 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent b71d5de commit 2a64032

3 files changed

Lines changed: 215 additions & 69 deletions

File tree

‎lib/internal/url.js‎

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -167,41 +167,62 @@ function lazyCryptoRandom() {
167167
returncryptoRandom;
168168
}
169169

170+
/**
171+
* Copy href and the latest `urlComponents` snapshot into a URLContext.
172+
* Property assignment order matches the historical URLContext fields so
173+
* `util.inspect(..., { showHidden: true })` stays stable.
174+
* @param {object} ctx
175+
* @param {string} href
176+
*/
177+
functionsetURLContextFromBinding(ctx,href){
178+
constc=bindingUrl.urlComponents;
179+
ctx.href=href;
180+
ctx.protocol_end=c[0];
181+
ctx.username_end=c[1];
182+
ctx.host_start=c[2];
183+
ctx.host_end=c[3];
184+
ctx.pathname_start=c[5];
185+
ctx.search_start=c[6];
186+
ctx.hash_start=c[7];
187+
ctx.port=c[4];
188+
ctx.scheme_type=c[8];
189+
}
190+
170191
// This class provides the internal state of a URL object. An instance of this
171192
// class is stored in every URL object and is accessed internally by setters
172193
// and getters. It roughly corresponds to the concept of a URL record in the
173194
// URL Standard, with a few differences. It is also the object transported to
174195
// the C++ binding.
175196
// Refs: https://url.spec.whatwg.org/#concept-url
197+
//
198+
// scheme_type refers to ada::scheme::type:
199+
// HTTP = 0, NOT_SPECIAL = 1, HTTPS = 2, WS = 3, FTP = 4, WSS = 5, FILE = 6
176200
classURLContext{
177201
// This is the maximum value uint32_t can get.
178202
// Ada uses uint32_t(-1) for declaring omitted values.
179203
static #omitted =4294967295;
180204

181-
href='';
182-
protocol_end=0;
183-
username_end=0;
184-
host_start=0;
185-
host_end=0;
186-
pathname_start=0;
187-
search_start=0;
188-
hash_start=0;
189-
port=0;
190205
/**
191-
* Refers to `ada::scheme::type`
192-
*
193-
* enum type : uint8_t {
194-
* HTTP = 0,
195-
* NOT_SPECIAL = 1,
196-
* HTTPS = 2,
197-
* WS = 3,
198-
* FTP = 4,
199-
* WSS = 5,
200-
* FILE = 6
201-
* };
202-
* @type {number}
206+
* @param {string} [href] Parsed href. When omitted, create an empty context
207+
* (used by `URL.parse` on invalid input). When provided, `bindingUrl.parse`
208+
* / `update` has just written `urlComponents`.
203209
*/
204-
scheme_type=1;
210+
constructor(href){
211+
if(href===undefined){
212+
this.href='';
213+
this.protocol_end=0;
214+
this.username_end=0;
215+
this.host_start=0;
216+
this.host_end=0;
217+
this.pathname_start=0;
218+
this.search_start=0;
219+
this.hash_start=0;
220+
this.port=0;
221+
this.scheme_type=1;
222+
return;
223+
}
224+
setURLContextFromBinding(this,href);
225+
}
205226

206227
gethasPort(){
207228
returnthis.port!==URLContext.#omitted;
@@ -835,7 +856,7 @@ const kCreateURLFromPosixPathSymbol = Symbol('kCreateURLFromPosixPath');
835856
constkCreateURLFromWindowsPathSymbol=Symbol('kCreateURLFromWindowsPath');
836857

837858
classURL{
838-
#context=newURLContext();
859+
#context;
839860
#searchParams;
840861
#searchParamsModified;
841862

@@ -860,16 +881,16 @@ class URL {
860881
}
861882

862883
constructor(input,base=undefined,parseSymbol=undefined){
863-
markTransferMode(this,false,false);
864-
865884
if(arguments.length===0){
866885
thrownewERR_MISSING_ARGS('url');
867886
}
868887

869888
// StringPrototypeToWellFormed is not needed.
870-
input=`${input}`;
889+
if(typeofinput!=='string'){
890+
input=`${input}`;
891+
}
871892

872-
if(base!==undefined){
893+
if(base!==undefined&&typeofbase!=='string'){
873894
base=`${base}`;
874895
}
875896

@@ -884,9 +905,12 @@ class URL {
884905
bindingUrl.pathToFileURL(input,interpretAsWindowsPath,base) :
885906
bindingUrl.parse(input,base,raiseException);
886907
}
887-
if(href){
888-
this.#updateContext(href);
889-
}
908+
909+
// Delay context allocation until parse finishes so invalid URLs that
910+
// throw do not pay for an unused URLContext. Initialize in one shot
911+
// from the binding snapshot instead of writing an empty context first.
912+
this.#context =href ? newURLContext(href) : newURLContext();
913+
markTransferMode(this,false,false);
890914
}
891915

892916
staticparse(input,base=undefined){
@@ -955,29 +979,7 @@ class URL {
955979
constpreviousSearch=shouldUpdateSearchParams&&this.#searchParams &&
956980
(this.#searchParamsModified ? this.#getSearchFromParams() : this.#getSearchFromContext());
957981

958-
this.#context.href=href;
959-
960-
const{
961-
0: protocol_end,
962-
1: username_end,
963-
2: host_start,
964-
3: host_end,
965-
4: port,
966-
5: pathname_start,
967-
6: search_start,
968-
7: hash_start,
969-
8: scheme_type,
970-
}=bindingUrl.urlComponents;
971-
972-
this.#context.protocol_end=protocol_end;
973-
this.#context.username_end=username_end;
974-
this.#context.host_start=host_start;
975-
this.#context.host_end=host_end;
976-
this.#context.port=port;
977-
this.#context.pathname_start=pathname_start;
978-
this.#context.search_start=search_start;
979-
this.#context.hash_start=hash_start;
980-
this.#context.scheme_type=scheme_type;
982+
setURLContextFromBinding(this.#context,href);
981983

982984
if(this.#searchParams){
983985
// If the search string has updated, URL becomes the source of truth, and we update URLSearchParams.
@@ -1202,10 +1204,12 @@ class URL {
12021204
thrownewERR_MISSING_ARGS('url');
12031205
}
12041206

1205-
url=`${url}`;
1207+
if(typeofurl!=='string'){
1208+
url=`${url}`;
1209+
}
12061210

12071211
if(base!==undefined){
1208-
returnbindingUrl.canParse(url,`${base}`);
1212+
returnbindingUrl.canParse(url,typeofbase==='string' ? base : `${base}`);
12091213
}
12101214

12111215
// It is important to differentiate the canParse call statements

‎src/node_url.cc‎

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include"node_metadata.h"
99
#include"node_process-inl.h"
1010
#include"path.h"
11+
#include"simdutf.h"
1112
#include"util-inl.h"
1213
#include"v8-fast-api-calls.h"
1314
#include"v8-local-handle.h"
@@ -33,6 +34,38 @@ using v8::SnapshotCreator;
3334
using v8::String;
3435
using v8::Value;
3536

37+
namespace {
38+
39+
// Parse a V8 string as a URL. One-byte ASCII inputs are parsed in place
40+
// without allocating a UTF-8 copy. `reuse_input` is set when the serialized
41+
// href is identical to that ASCII input so the caller can return the original
42+
// V8 string. Non-ASCII inputs are never reused: UTF-8 conversion may replace
43+
// unpaired surrogates, so the original string may not match href.
44+
ada::result<ada::url_aggregator> ParseUrlFromV8String(
45+
Isolate* isolate,
46+
Local<String> input,
47+
const ada::url_aggregator* base_url,
48+
bool* reuse_input) {
49+
{
50+
String::ValueView view(isolate, input);
51+
if (view.is_one_byte()) {
52+
constchar* data = reinterpret_cast<constchar*>(view.data8());
53+
constsize_t length = static_cast<size_t>(view.length());
54+
if (simdutf::validate_ascii(data, length)) [[likely]] {
55+
const std::string_view input_view(data, length);
56+
auto out = ada::parse<ada::url_aggregator>(input_view, base_url);
57+
*reuse_input = out.has_value() && out->get_href() == input_view;
58+
return out;
59+
}
60+
}
61+
}
62+
*reuse_input = false;
63+
Utf8Value utf8(isolate, input);
64+
return ada::parse<ada::url_aggregator>(utf8.ToStringView(), base_url);
65+
}
66+
67+
} // namespace
68+
3669
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
3770
tracker->TrackField("url_components_buffer", url_components_buffer_);
3871
}
@@ -392,32 +425,51 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
392425
Realm* realm = Realm::GetCurrent(args);
393426
BindingData* binding_data = realm->GetBindingData<BindingData>();
394427
Isolate* isolate = realm->isolate();
395-
std::optional<std::string> base_{};
428+
Local<String> input_string = args[0].As<String>();
396429

397-
Utf8Value input(isolate, args[0]);
398430
ada::result<ada::url_aggregator> base;
399431
ada::url_aggregator* base_pointer = nullptr;
400432
if (args[1]->IsString()) {
401-
base_ = Utf8Value(isolate, args[1]).ToString();
402-
base = ada::parse<ada::url_aggregator>(*base_);
403-
if (!base && raise_exception) {
404-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
405-
} elseif (!base) {
433+
bool unused_reuse = false;
434+
base = ParseUrlFromV8String(
435+
isolate, args[1].As<String>(), nullptr, &unused_reuse);
436+
if (!base) {
437+
if (raise_exception) {
438+
Utf8Value input(isolate, input_string);
439+
Utf8Value base_utf8(isolate, args[1]);
440+
returnThrowInvalidURL(
441+
realm->env(), input.ToStringView(), base_utf8.ToString());
442+
}
406443
return;
407444
}
408445
base_pointer = &base.value();
409446
}
410-
auto out =
411-
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);
412447

413-
if (!out && raise_exception) {
414-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
415-
} elseif (!out) {
448+
bool reuse_input = false;
449+
auto out =
450+
ParseUrlFromV8String(isolate, input_string, base_pointer, &reuse_input);
451+
if (!out) {
452+
if (raise_exception) {
453+
Utf8Value input(isolate, input_string);
454+
std::optional<std::string> base_error;
455+
if (args[1]->IsString()) {
456+
base_error = Utf8Value(isolate, args[1]).ToString();
457+
}
458+
returnThrowInvalidURL(
459+
realm->env(), input.ToStringView(), std::move(base_error));
460+
}
416461
return;
417462
}
418463

419464
binding_data->UpdateComponents(out->get_components(), out->type);
420465

466+
// Already-serialized ASCII URLs are the common case. Reuse the input
467+
// string instead of allocating an identical V8 string from href.
468+
if (reuse_input) {
469+
args.GetReturnValue().Set(args[0]);
470+
return;
471+
}
472+
421473
Local<Value> ret;
422474
if (ToV8Value(realm->context(), out->get_href(), isolate).ToLocal(&ret))
423475
[[likely]] {
@@ -439,13 +491,15 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
439491
return;
440492
}
441493
enum url_update_action action = static_cast<enum url_update_action>(val);
442-
Utf8Value input(isolate, args[0].As<String>());
443494
Utf8Value new_value(isolate, args[2].As<String>());
444495

445496
std::string_view new_value_view = new_value.ToStringView();
446497
// A serialized URL is not always reparsable: the IDNA encoder can emit a
447498
// host label that the decoder rejects. Fail the update instead of crashing.
448-
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
499+
// Existing hrefs are typically already-serialized ASCII, so parse in place.
500+
bool unused_reuse = false;
501+
auto out = ParseUrlFromV8String(
502+
isolate, args[0].As<String>(), nullptr, &unused_reuse);
449503
if (!out) {
450504
return args.GetReturnValue().Set(false);
451505
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// Covers the URL constructor parse paths that avoid a UTF-8 copy and/or
4+
// reuse the input string when it is already a serialized ASCII href.
5+
6+
const{ hasIntl }=require('../common');
7+
constassert=require('assert');
8+
9+
constalreadySerialized=[
10+
'https://nodejs.org/en/blog/',
11+
'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/'+
12+
'/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1'+
13+
'&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&'+
14+
'key=f5c65e1e98fe07e648249ad41e1cfdb0',
15+
'https://user:pass@example.com/path?search=1',
16+
'file:///foo/bar/test/node.js',
17+
'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868',
18+
];
19+
20+
for(consthrefofalreadySerialized){
21+
consturl=newURL(href);
22+
assert.strictEqual(url.href,href);
23+
assert.strictEqual(URL.parse(href).href,href);
24+
assert.strictEqual(URL.canParse(href),true);
25+
}
26+
27+
// Special-scheme URLs with an empty path gain a trailing slash.
28+
{
29+
consturl=newURL('https://example.com');
30+
assert.strictEqual(url.href,'https://example.com/');
31+
assert.strictEqual(url.pathname,'/');
32+
}
33+
34+
// Dot-segment normalization must still rewrite the path.
35+
{
36+
consturl=newURL('https://example.org/./a/../b/./c');
37+
assert.strictEqual(url.href,'https://example.org/b/c');
38+
assert.strictEqual(url.pathname,'/b/c');
39+
}
40+
41+
// Relative resolution against a base URL.
42+
{
43+
consturl=newURL('/path?x=1#h','https://example.com:8443/base');
44+
assert.strictEqual(url.href,'https://example.com:8443/path?x=1#h');
45+
assert.strictEqual(url.host,'example.com:8443');
46+
}
47+
48+
// Non-string input is still stringified.
49+
{
50+
consturl=newURL({toString: ()=>'https://example.com/from-object'});
51+
assert.strictEqual(url.href,'https://example.com/from-object');
52+
}
53+
54+
// Invalid input still throws from the constructor and is null from parse().
55+
{
56+
assert.throws(()=>newURL('not a url'),{
57+
code: 'ERR_INVALID_URL',
58+
name: 'TypeError',
59+
});
60+
assert.strictEqual(URL.parse('not a url'),null);
61+
assert.strictEqual(URL.canParse('not a url'),false);
62+
}
63+
64+
// Unpaired surrogates must not be returned as-is from href.
65+
{
66+
constinput='https://example.com/\uD800';
67+
consturl=newURL(input);
68+
assert.notStrictEqual(url.href,input);
69+
assert.ok(url.href.startsWith('https://example.com/'));
70+
}
71+
72+
if(hasIntl){
73+
consturl=newURL('http://你好你好.在线');
74+
assert.ok(url.hostname.startsWith('xn--'));
75+
assert.ok(url.href.startsWith('http://xn--'));
76+
}
77+
78+
// Setters re-parse the existing href; keep component updates correct.
79+
{
80+
consturl=newURL('https://example.com/old');
81+
url.pathname='/new';
82+
url.search='q=1';
83+
url.hash='frag';
84+
assert.strictEqual(url.href,'https://example.com/new?q=1#frag');
85+
assert.strictEqual(url.pathname,'/new');
86+
assert.strictEqual(url.search,'?q=1');
87+
assert.strictEqual(url.hash,'#frag');
88+
}

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit 2a64032

Browse files
anonrigaduh95
authored andcommitted
url: speed up WHATWG URL parsing
Parse one-byte ASCII inputs in place instead of copying them into a UTF-8 buffer, and reuse the original V8 string when the serialized href is unchanged. Delay URLContext allocation until parse finishes and skip ToString when the input is already a string. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Cursor PR-URL: #65361 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent b71d5de commit 2a64032

3 files changed

Lines changed: 215 additions & 69 deletions

File tree

‎lib/internal/url.js‎

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -167,41 +167,62 @@ function lazyCryptoRandom() {
167167
returncryptoRandom;
168168
}
169169

170+
/**
171+
* Copy href and the latest `urlComponents` snapshot into a URLContext.
172+
* Property assignment order matches the historical URLContext fields so
173+
* `util.inspect(..., { showHidden: true })` stays stable.
174+
* @param {object} ctx
175+
* @param {string} href
176+
*/
177+
functionsetURLContextFromBinding(ctx,href){
178+
constc=bindingUrl.urlComponents;
179+
ctx.href=href;
180+
ctx.protocol_end=c[0];
181+
ctx.username_end=c[1];
182+
ctx.host_start=c[2];
183+
ctx.host_end=c[3];
184+
ctx.pathname_start=c[5];
185+
ctx.search_start=c[6];
186+
ctx.hash_start=c[7];
187+
ctx.port=c[4];
188+
ctx.scheme_type=c[8];
189+
}
190+
170191
// This class provides the internal state of a URL object. An instance of this
171192
// class is stored in every URL object and is accessed internally by setters
172193
// and getters. It roughly corresponds to the concept of a URL record in the
173194
// URL Standard, with a few differences. It is also the object transported to
174195
// the C++ binding.
175196
// Refs: https://url.spec.whatwg.org/#concept-url
197+
//
198+
// scheme_type refers to ada::scheme::type:
199+
// HTTP = 0, NOT_SPECIAL = 1, HTTPS = 2, WS = 3, FTP = 4, WSS = 5, FILE = 6
176200
classURLContext{
177201
// This is the maximum value uint32_t can get.
178202
// Ada uses uint32_t(-1) for declaring omitted values.
179203
static #omitted =4294967295;
180204

181-
href='';
182-
protocol_end=0;
183-
username_end=0;
184-
host_start=0;
185-
host_end=0;
186-
pathname_start=0;
187-
search_start=0;
188-
hash_start=0;
189-
port=0;
190205
/**
191-
* Refers to `ada::scheme::type`
192-
*
193-
* enum type : uint8_t {
194-
* HTTP = 0,
195-
* NOT_SPECIAL = 1,
196-
* HTTPS = 2,
197-
* WS = 3,
198-
* FTP = 4,
199-
* WSS = 5,
200-
* FILE = 6
201-
* };
202-
* @type {number}
206+
* @param {string} [href] Parsed href. When omitted, create an empty context
207+
* (used by `URL.parse` on invalid input). When provided, `bindingUrl.parse`
208+
* / `update` has just written `urlComponents`.
203209
*/
204-
scheme_type=1;
210+
constructor(href){
211+
if(href===undefined){
212+
this.href='';
213+
this.protocol_end=0;
214+
this.username_end=0;
215+
this.host_start=0;
216+
this.host_end=0;
217+
this.pathname_start=0;
218+
this.search_start=0;
219+
this.hash_start=0;
220+
this.port=0;
221+
this.scheme_type=1;
222+
return;
223+
}
224+
setURLContextFromBinding(this,href);
225+
}
205226

206227
gethasPort(){
207228
returnthis.port!==URLContext.#omitted;
@@ -835,7 +856,7 @@ const kCreateURLFromPosixPathSymbol = Symbol('kCreateURLFromPosixPath');
835856
constkCreateURLFromWindowsPathSymbol=Symbol('kCreateURLFromWindowsPath');
836857

837858
classURL{
838-
#context=newURLContext();
859+
#context;
839860
#searchParams;
840861
#searchParamsModified;
841862

@@ -860,16 +881,16 @@ class URL {
860881
}
861882

862883
constructor(input,base=undefined,parseSymbol=undefined){
863-
markTransferMode(this,false,false);
864-
865884
if(arguments.length===0){
866885
thrownewERR_MISSING_ARGS('url');
867886
}
868887

869888
// StringPrototypeToWellFormed is not needed.
870-
input=`${input}`;
889+
if(typeofinput!=='string'){
890+
input=`${input}`;
891+
}
871892

872-
if(base!==undefined){
893+
if(base!==undefined&&typeofbase!=='string'){
873894
base=`${base}`;
874895
}
875896

@@ -884,9 +905,12 @@ class URL {
884905
bindingUrl.pathToFileURL(input,interpretAsWindowsPath,base) :
885906
bindingUrl.parse(input,base,raiseException);
886907
}
887-
if(href){
888-
this.#updateContext(href);
889-
}
908+
909+
// Delay context allocation until parse finishes so invalid URLs that
910+
// throw do not pay for an unused URLContext. Initialize in one shot
911+
// from the binding snapshot instead of writing an empty context first.
912+
this.#context =href ? newURLContext(href) : newURLContext();
913+
markTransferMode(this,false,false);
890914
}
891915

892916
staticparse(input,base=undefined){
@@ -955,29 +979,7 @@ class URL {
955979
constpreviousSearch=shouldUpdateSearchParams&&this.#searchParams &&
956980
(this.#searchParamsModified ? this.#getSearchFromParams() : this.#getSearchFromContext());
957981

958-
this.#context.href=href;
959-
960-
const{
961-
0: protocol_end,
962-
1: username_end,
963-
2: host_start,
964-
3: host_end,
965-
4: port,
966-
5: pathname_start,
967-
6: search_start,
968-
7: hash_start,
969-
8: scheme_type,
970-
}=bindingUrl.urlComponents;
971-
972-
this.#context.protocol_end=protocol_end;
973-
this.#context.username_end=username_end;
974-
this.#context.host_start=host_start;
975-
this.#context.host_end=host_end;
976-
this.#context.port=port;
977-
this.#context.pathname_start=pathname_start;
978-
this.#context.search_start=search_start;
979-
this.#context.hash_start=hash_start;
980-
this.#context.scheme_type=scheme_type;
982+
setURLContextFromBinding(this.#context,href);
981983

982984
if(this.#searchParams){
983985
// If the search string has updated, URL becomes the source of truth, and we update URLSearchParams.
@@ -1202,10 +1204,12 @@ class URL {
12021204
thrownewERR_MISSING_ARGS('url');
12031205
}
12041206

1205-
url=`${url}`;
1207+
if(typeofurl!=='string'){
1208+
url=`${url}`;
1209+
}
12061210

12071211
if(base!==undefined){
1208-
returnbindingUrl.canParse(url,`${base}`);
1212+
returnbindingUrl.canParse(url,typeofbase==='string' ? base : `${base}`);
12091213
}
12101214

12111215
// It is important to differentiate the canParse call statements

‎src/node_url.cc‎

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include"node_metadata.h"
99
#include"node_process-inl.h"
1010
#include"path.h"
11+
#include"simdutf.h"
1112
#include"util-inl.h"
1213
#include"v8-fast-api-calls.h"
1314
#include"v8-local-handle.h"
@@ -33,6 +34,38 @@ using v8::SnapshotCreator;
3334
using v8::String;
3435
using v8::Value;
3536

37+
namespace {
38+
39+
// Parse a V8 string as a URL. One-byte ASCII inputs are parsed in place
40+
// without allocating a UTF-8 copy. `reuse_input` is set when the serialized
41+
// href is identical to that ASCII input so the caller can return the original
42+
// V8 string. Non-ASCII inputs are never reused: UTF-8 conversion may replace
43+
// unpaired surrogates, so the original string may not match href.
44+
ada::result<ada::url_aggregator> ParseUrlFromV8String(
45+
Isolate* isolate,
46+
Local<String> input,
47+
const ada::url_aggregator* base_url,
48+
bool* reuse_input) {
49+
{
50+
String::ValueView view(isolate, input);
51+
if (view.is_one_byte()) {
52+
constchar* data = reinterpret_cast<constchar*>(view.data8());
53+
constsize_t length = static_cast<size_t>(view.length());
54+
if (simdutf::validate_ascii(data, length)) [[likely]] {
55+
const std::string_view input_view(data, length);
56+
auto out = ada::parse<ada::url_aggregator>(input_view, base_url);
57+
*reuse_input = out.has_value() && out->get_href() == input_view;
58+
return out;
59+
}
60+
}
61+
}
62+
*reuse_input = false;
63+
Utf8Value utf8(isolate, input);
64+
return ada::parse<ada::url_aggregator>(utf8.ToStringView(), base_url);
65+
}
66+
67+
} // namespace
68+
3669
voidBindingData::MemoryInfo(MemoryTracker* tracker) const {
3770
tracker->TrackField("url_components_buffer", url_components_buffer_);
3871
}
@@ -392,32 +425,51 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
392425
Realm* realm = Realm::GetCurrent(args);
393426
BindingData* binding_data = realm->GetBindingData<BindingData>();
394427
Isolate* isolate = realm->isolate();
395-
std::optional<std::string> base_{};
428+
Local<String> input_string = args[0].As<String>();
396429

397-
Utf8Value input(isolate, args[0]);
398430
ada::result<ada::url_aggregator> base;
399431
ada::url_aggregator* base_pointer = nullptr;
400432
if (args[1]->IsString()) {
401-
base_ = Utf8Value(isolate, args[1]).ToString();
402-
base = ada::parse<ada::url_aggregator>(*base_);
403-
if (!base && raise_exception) {
404-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
405-
} elseif (!base) {
433+
bool unused_reuse = false;
434+
base = ParseUrlFromV8String(
435+
isolate, args[1].As<String>(), nullptr, &unused_reuse);
436+
if (!base) {
437+
if (raise_exception) {
438+
Utf8Value input(isolate, input_string);
439+
Utf8Value base_utf8(isolate, args[1]);
440+
returnThrowInvalidURL(
441+
realm->env(), input.ToStringView(), base_utf8.ToString());
442+
}
406443
return;
407444
}
408445
base_pointer = &base.value();
409446
}
410-
auto out =
411-
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);
412447

413-
if (!out && raise_exception) {
414-
returnThrowInvalidURL(realm->env(), input.ToStringView(), base_);
415-
} elseif (!out) {
448+
bool reuse_input = false;
449+
auto out =
450+
ParseUrlFromV8String(isolate, input_string, base_pointer, &reuse_input);
451+
if (!out) {
452+
if (raise_exception) {
453+
Utf8Value input(isolate, input_string);
454+
std::optional<std::string> base_error;
455+
if (args[1]->IsString()) {
456+
base_error = Utf8Value(isolate, args[1]).ToString();
457+
}
458+
returnThrowInvalidURL(
459+
realm->env(), input.ToStringView(), std::move(base_error));
460+
}
416461
return;
417462
}
418463

419464
binding_data->UpdateComponents(out->get_components(), out->type);
420465

466+
// Already-serialized ASCII URLs are the common case. Reuse the input
467+
// string instead of allocating an identical V8 string from href.
468+
if (reuse_input) {
469+
args.GetReturnValue().Set(args[0]);
470+
return;
471+
}
472+
421473
Local<Value> ret;
422474
if (ToV8Value(realm->context(), out->get_href(), isolate).ToLocal(&ret))
423475
[[likely]] {
@@ -439,13 +491,15 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
439491
return;
440492
}
441493
enum url_update_action action = static_cast<enum url_update_action>(val);
442-
Utf8Value input(isolate, args[0].As<String>());
443494
Utf8Value new_value(isolate, args[2].As<String>());
444495

445496
std::string_view new_value_view = new_value.ToStringView();
446497
// A serialized URL is not always reparsable: the IDNA encoder can emit a
447498
// host label that the decoder rejects. Fail the update instead of crashing.
448-
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
499+
// Existing hrefs are typically already-serialized ASCII, so parse in place.
500+
bool unused_reuse = false;
501+
auto out = ParseUrlFromV8String(
502+
isolate, args[0].As<String>(), nullptr, &unused_reuse);
449503
if (!out) {
450504
return args.GetReturnValue().Set(false);
451505
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// Covers the URL constructor parse paths that avoid a UTF-8 copy and/or
4+
// reuse the input string when it is already a serialized ASCII href.
5+
6+
const{ hasIntl }=require('../common');
7+
constassert=require('assert');
8+
9+
constalreadySerialized=[
10+
'https://nodejs.org/en/blog/',
11+
'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/'+
12+
'/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1'+
13+
'&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&'+
14+
'key=f5c65e1e98fe07e648249ad41e1cfdb0',
15+
'https://user:pass@example.com/path?search=1',
16+
'file:///foo/bar/test/node.js',
17+
'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868',
18+
];
19+
20+
for(consthrefofalreadySerialized){
21+
consturl=newURL(href);
22+
assert.strictEqual(url.href,href);
23+
assert.strictEqual(URL.parse(href).href,href);
24+
assert.strictEqual(URL.canParse(href),true);
25+
}
26+
27+
// Special-scheme URLs with an empty path gain a trailing slash.
28+
{
29+
consturl=newURL('https://example.com');
30+
assert.strictEqual(url.href,'https://example.com/');
31+
assert.strictEqual(url.pathname,'/');
32+
}
33+
34+
// Dot-segment normalization must still rewrite the path.
35+
{
36+
consturl=newURL('https://example.org/./a/../b/./c');
37+
assert.strictEqual(url.href,'https://example.org/b/c');
38+
assert.strictEqual(url.pathname,'/b/c');
39+
}
40+
41+
// Relative resolution against a base URL.
42+
{
43+
consturl=newURL('/path?x=1#h','https://example.com:8443/base');
44+
assert.strictEqual(url.href,'https://example.com:8443/path?x=1#h');
45+
assert.strictEqual(url.host,'example.com:8443');
46+
}
47+
48+
// Non-string input is still stringified.
49+
{
50+
consturl=newURL({toString: ()=>'https://example.com/from-object'});
51+
assert.strictEqual(url.href,'https://example.com/from-object');
52+
}
53+
54+
// Invalid input still throws from the constructor and is null from parse().
55+
{
56+
assert.throws(()=>newURL('not a url'),{
57+
code: 'ERR_INVALID_URL',
58+
name: 'TypeError',
59+
});
60+
assert.strictEqual(URL.parse('not a url'),null);
61+
assert.strictEqual(URL.canParse('not a url'),false);
62+
}
63+
64+
// Unpaired surrogates must not be returned as-is from href.
65+
{
66+
constinput='https://example.com/\uD800';
67+
consturl=newURL(input);
68+
assert.notStrictEqual(url.href,input);
69+
assert.ok(url.href.startsWith('https://example.com/'));
70+
}
71+
72+
if(hasIntl){
73+
consturl=newURL('http://你好你好.在线');
74+
assert.ok(url.hostname.startsWith('xn--'));
75+
assert.ok(url.href.startsWith('http://xn--'));
76+
}
77+
78+
// Setters re-parse the existing href; keep component updates correct.
79+
{
80+
consturl=newURL('https://example.com/old');
81+
url.pathname='/new';
82+
url.search='q=1';
83+
url.hash='frag';
84+
assert.strictEqual(url.href,'https://example.com/new?q=1#frag');
85+
assert.strictEqual(url.pathname,'/new');
86+
assert.strictEqual(url.search,'?q=1');
87+
assert.strictEqual(url.hash,'#frag');
88+
}

0 commit comments

Comments
 (0)