Skip to content

perf(runtime): shrink ada, parse URLs once, fix URLPattern groups - #2152

Merged
edusperoni merged 5 commits into
feat/ada-4.1.1from
perf/ada-size
Nov 25, 2026
Merged

edusperoni merged 5 commits into
feat/ada-4.1.1from
perf/ada-size

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Stacked on #2134 (Ada 4.1.1). This is the Android half of NativeScript/ios#606.

As #2134 stands, the upgrade adds about 89 KB per ABI to libNativeScript.so. This PR takes back most of that.

1. Build ada at -Os. ada was compiled at the runtime-wide -O3. At -Os, ada's code is about 50 KB smaller per ABI. Native benchmark on two phones (median of 3 rounds, ns per operation):

Galaxy S20 FE -O3 -Os Galaxy S8 -O3 -Os
absolute URL parse 192 214 (+11%) 441 519 (+18%)
relative URL parse 130 154 (+19%) 336 388 (+15%)
IDN host parse 1027 1097 (+7%) 2203 2352 (+7%)
URLSearchParams 609 599 (−2%) 1408 1360 (−3%)

The slowdown is tens of nanoseconds per new URL(). Only ada.cpp changes; the rest of the runtime stays at -O3, and local Debug builds are unaffected.

2. Parse each URL once.

  • new URL(input, base) called ada::can_parse(input, &base), which parses both, then parsed both again. It now parses each once.
  • URL.canParse uses ada::parse as well, so ada::can_parse and its second parser instantiation are no longer linked.
  • These fixes ride along:
    • canParse stringifies its arguments instead of casting them to v8::String unchecked, so URL.canParse(urlObject) no longer reads a non-string as a string;
    • canParse treats an undefined base as absent and no longer truncates at an embedded NUL;
    • new URL() without arguments throws a TypeError instead of reading undefined as a string;
    • a base object whose toString() throws now propagates the exception instead of aborting in ToLocalChecked().

3. URLPattern capture groups and hasRegExpGroups. Found while adding specs: testURLPattern.js was never required from mainpage.js, so the Android URLPattern specs never ran. Once registered, they exposed two bugs:

  • v8_regex_provider::regex_search returned RegExp.exec's whole match as the first capture. Every named group read its predecessor's value, so :id in /books/:id got /books/123. ada also received one more result than it has group names. iOS already skips element 0.
  • The hasRegExpGroups getter was registered as hasRegexpGroups.

Both now match iOS, and the specs now cover test()/exec() with capture groups.

Size. arm64 optimized build. Numbers are loaded bytes from the lld link map (code, rodata, data, unwind), because stripped .so sizes are page-rounded.

ada whole libNativeScript.so
main (ada 4.0.0) 297.7 KB 26,285,061
#2134 (ada 4.1.1) 391.8 KB 26,374,175
this PR 307.2 KB 26,290,495 (−83.7 KB vs #2134)
this PR + -fdata-sections from #2136 250.9 KB 26,233,370 (−140.8 KB vs #2134, −51.7 KB vs main)

Not ported from iOS:

  • Symbol visibility. The iOS PR's biggest saving was hiding ada's symbols. Android already builds with -fvisibility=hidden and a version script. The equivalent loss here came from -fno-data-sections: it put every ada vtable in one section, so unused code like the ada::url parser stayed linked. build: keep the NDK's default compile flags #2136 removes that flag.
  • System ICU for IDNA. The NDK's libicu.so exists only from API 31 and ships no uidna.h. The only system UTS error handling: #46 is Java's android.icu.text.IDNA (API 24+, ICU 56 / Unicode 8 at the bottom of the range), which would need JNI. Left out.
  • std::regex. The Android runtime has no std::regex use; URLPattern already runs on V8's RegExp.

Does your commit message include the wording below to reference a specific issue in this repo?

No.

Related Pull Requests

Does your pull request have unit tests?

Yes. The full suite passes on an API 36 arm64 emulator: 1,792 specs, 0 failures. testURLPattern.js now runs, and new specs cover:

  • relative URLs against string and URL bases;
  • invalid bases;
  • a throwing base toString();
  • new URL() without arguments;
  • URL.canParse with string, URL-object and undefined arguments;
  • IDN hosts (Unicode, uppercase xn--, ZWJ rejection);
  • non-ASCII URLPattern group names;
  • URLPattern test()/exec() with capture groups.

Summary by CodeRabbit

  • Bug Fixes

    • Improved URL construction and validation, including relative URLs, base handling, and internationalized hostnames.
    • Updated URL.canParse() to validate inputs and optional bases consistently.
    • Corrected URLPattern capture-group results and the exposed hasRegExpGroups property name.
  • Tests

    • Added coverage for URL parsing, validation, internationalized hostnames, and URLPattern matching and captures.

ada was built at the runtime-wide -O3. At -Os its code shrinks by about 50 KB
per ABI; on a Galaxy S20 FE and a Galaxy S8, absolute/relative/IDN parses got
7-19% slower (tens of nanoseconds each) and URLSearchParams 2-3% faster.
new URL(input, base) called ada::can_parse, which parses base and input, then
parsed both again; it now parses each once. URL.canParse uses ada::parse too,
so ada::can_parse and its second parser instantiation are no longer linked.

canParse now stringifies its arguments instead of casting them to strings
unchecked, ignores an undefined base, and keeps an embedded NUL; new URL()
without arguments throws a TypeError; and a base whose toString() throws
propagates the exception instead of aborting.
v8_regex_provider::regex_search returned RegExp.exec's whole match as the first
capture, so every named group read its predecessor's value (the first one the
whole match) and ada paired one more result than it has group names. The
hasRegExpGroups getter was registered as hasRegexpGroups. Both match iOS now.
@edusperoni
edusperoni added this pull request to stack #2153 November 25, 2026 17:31
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

URL construction and URL.canParse now use explicit string conversion and parsing for inputs and optional bases. URLPattern capture handling and property spelling change. The test suite adds URL and URLPattern cases, and optimized builds compile ada.cpp with -Os.

Changes: URL construction and parsing

Layer / File(s) Summary
URL construction and relative parsing
test-app/runtime/src/main/cpp/URLImpl.cpp, test-app/app/src/main/assets/app/tests/testURLImpl.js, test-app/runtime/CMakeLists.txt
URLImpl moves parsed state into storage and parses a URL against an optional base. Tests cover constructor arguments, relative resolution, and internationalized hostnames. Optimized builds compile ada.cpp with -Os.
URL.canParse conversion and validation
test-app/runtime/src/main/cpp/URLImpl.cpp, test-app/app/src/main/assets/app/tests/testURLImpl.js
URL.canParse converts its input and any defined base through V8 string conversion, then checks parsing with ada. Tests cover valid and invalid inputs, bases, and stringified arguments.

URLPattern matching

Layer / File(s) Summary
Capture handling and matching tests
test-app/runtime/src/main/cpp/URLPatternImpl.cpp, test-app/app/src/main/assets/app/tests/testURLPattern.js, test-app/app/src/main/assets/app/mainpage.js
URLPattern excludes the full-match element from capture results and uses the hasRegExpGroups spelling. Tests cover group names, test() matches, and exec() results. The app test entry point includes the URLPattern suite.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant JavaScript
  participant URLImpl_Ctor
  participant V8
  participant ada_parse
  JavaScript->>URLImpl_Ctor: Pass URL and optional base
  URLImpl_Ctor->>V8: Convert object base to string
  URLImpl_Ctor->>ada_parse: Parse base, then parse URL relative to base
  ada_parse-->>URLImpl_Ctor: Return parse result
  URLImpl_Ctor-->>JavaScript: Return URL or throw TypeError
Loading

Suggested reviewers: triniwiz, nathanwalker

Merge Risk: Minimal · up to 132d5

This change shrinks the Ada library, parses each URL once, and fixes URLPattern capture groups and a property name, with new specs covering the changed behavior. No actionable merge-blocking risk was found. URL parsing is intentionally somewhat slower in exchange for a smaller binary.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names three changes: shrinking Ada, parsing URLs once, and fixing URLPattern groups. The changeset covers all three. The title uses the conventional-commit prefix 'perf(runtime)' and is a si…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names three changes: shrinking Ada, parsing URLs once, and fixing URLPattern groups. The changeset covers all three. The title uses the conventional-commit prefix 'perf(runtime)' and is a single concise line. It does not cover the URL.canParse and API behavior changes, which is acceptable because the title does not need every detail.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4f45f3f and 132d5bd.

Files selected for processing: 6

  • test-app/app/src/main/assets/app/mainpage.js
  • test-app/app/src/main/assets/app/tests/testURLImpl.js
  • test-app/app/src/main/assets/app/tests/testURLPattern.js
  • test-app/runtime/CMakeLists.txt
  • test-app/runtime/src/main/cpp/URLImpl.cpp
  • test-app/runtime/src/main/cpp/URLPatternImpl.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@edusperoni edusperoni changed the title perf/ada size perf(runtime): shrink ada, parse URLs once, fix URLPattern groups Nov 25, 2026
@edusperoni
edusperoni marked this pull request as ready for review November 25, 2026 18:11
@edusperoni
edusperoni merged commit 15cd5e5 into main Nov 25, 2026
11 checks passed
@edusperoni
edusperoni deleted the perf/ada-size branch November 25, 2026 18:19
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.

1 participant