Skip to content

url: rebase URLPattern speed-up onto main and fix groups prototype - #44

Open
lemire wants to merge 3 commits into
anonrig:cursor/urlpattern-performance-603efrom
lemire:urlpattern-perf-rebase
Open

lemire wants to merge 3 commits into
anonrig:cursor/urlpattern-performance-603efrom
lemire:urlpattern-perf-rebase

Conversation

@lemire

@lemire lemire commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

This builds on nodejs#65364. It merges current main into the branch,
which fixes the conflict that makes the commit queue fail, and then reworks
the change on top. The merge commit takes main's version of
src/node_url_pattern.cc; commit d01ba16 carries the actual change.
Because of the merge, the diff here also shows main's changes since
f83e7df. Only the last commit is new; feel free to squash it into yours.

Changes in src/node_url_pattern.cc

  • Rebase onto main. It reuses ParseURLPatternMatchArgs from main
    instead of adding ExtractInputAndBaseURL, and keeps the lexicographic
    member order from 20f5b0a. In URLPatternInit::FromJsObject, each key
    is paired with the field it fills in one array, so the order can't drift
    between two parallel arrays. This also drops the per-key Utf8Value of the
    key name and the string-compare dispatch.
  • String inputs. Instead of copying into a std::string, a Utf8Value
    stays alive for the call and Ada gets a string_view into it. This replaces
    CopyV8StringToBuffer and V8StringToStdString, so there's no new helper
    and no simdutf include. Latin-1 and two-byte strings go through
    Utf8Value's existing conversion once, not twice.
  • Regex provider. It uses isolate->GetCurrentContext() instead of
    Environment::GetCurrent(isolate)->context(). The provider only runs inside
    URLPattern binding calls, where the two are the same context, and this skips
    the embedder-data lookup (about 7% of test() in profiles).
  • exec() result groups. On main, every groups object was created
    with a fresh prototype object (Object::New(isolate, Object::New(isolate), ...)).
    That forced V8 to create new maps on every exec(). It's also a WebIDL
    deviation: groups is a record and should be an ordinary object whose
    prototype is Object.prototype. It's now a plain object filled with
    CreateDataProperty, so V8 reuses the same map transitions on every call.

Benchmarks

Release build, Intel Xeon Gold 6548N, benchmark/compare.js --runs 30,
pinned to one core, main vs this PR:

benchmark change
urlpattern-exec with a match (/books/:id/chapters/:chapter) +120% (8.9 µs → 4.1 µs per call)
urlpattern-exec, other configurations (no match) +1.5% to +4.5%
urlpattern-test, all configurations +3.0% to +3.6%
urlpattern-parse, init objects +5.0% to +6.7%
urlpattern-parse, constructor string +2.3%

The existing benchmarks never exercise exec() with a match, so this adds
benchmark/url/urlpattern-exec.js.

Alternatives I measured and rejected:

  • Building groups with the dictionary-mode Object::New(isolate, proto, names, values, n): 25–35% slower than CreateDataProperty.
  • A cached DictionaryTemplate for groups: V8 CHECK-fails on array-index names such as '0'.
  • Parsing integer keys by hand to use the index overload of CreateDataProperty: only 2–5%, not worth the extra code.

Note: with GCC (14 and 16), constructing a std::optional<T> zero-fills its
whole storage, so the two optional<Utf8Value> in URLPatternMatchArgs cost
a 2.5 KB memset per call; Clang doesn't do this. Even so, it measured 0–4%
faster than main's std::string copy, so I kept it.

Tests

test/parallel/test-urlpattern-fast-path.js gains:

  • Latin-1 (/café) and two-byte (/😀) string inputs to the constructor,
    test() and exec(), with and without a base URL.
  • A check that every component's groups has Object.prototype as its
    prototype. This fails on main.

test/parallel/test-urlpattern*.js and test/wpt/test-urlpattern.js pass.

One thing to consider: the port: 8080 → '*' assertion pins current behavior
that departs from WebIDL (non-string init members should be stringified).
Whoever fixes that later will have to change this test.

Not changed

  • The top-level exec() result still has a null prototype.
  • What's left of the cost of a matching exec() is split between Ada's
    match() (about 40%) and building the JS result (about 55%). The Ada side
    would be an upstream change.

anonrig and others added 3 commits August 18, 2026 12:49
Avoid extra UTF-8 copies on constructor, test, and exec; convert
regexp inputs with ToV8Value; and assign URLPatternInit fields by
interned key instead of string-comparing each component name.

Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
Assisted-by: Cursor
Signed-off-by: Daniel Lemire <daniel@lemire.me>

# Conflicts:
#	src/node_url_pattern.cc
Rework the URLPattern speed-up on top of main:

- Reuse ParseURLPatternMatchArgs and keep the lexicographic order of
  URLPatternInit members. Pair each key with the field it fills.
- Keep a Utf8Value alive for the call instead of copying string
  inputs into a std::string.
- Read the current context directly in the regex provider instead
  of going through Environment::GetCurrent().
- Build exec() `groups` as an ordinary object whose prototype is
  Object.prototype. Previously each one got a fresh prototype object,
  which forced V8 to create new maps on every call and departed from
  WebIDL.

Add tests for Latin-1 and two-byte string inputs and for the
prototype of `groups`, and a benchmark for exec() with a match.

Signed-off-by: Daniel Lemire <daniel@lemire.me>
@cursor
cursor Bot force-pushed the cursor/urlpattern-performance-603e branch 3 times, most recently from f9499d1 to 8004407 Compare November 29, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants