Repository navigation
build: keep the NDK's default compile flags - #2136
Conversation
CMakeLists.txt assigned CMAKE_CXX_FLAGS outright in both the release and Debug branches, discarding everything the NDK toolchain had put there: -DANDROID, -fdata-sections, -funwind-tables, -Wformat -Werror=format-security, the hardening defaults and, on armeabi-v7a, -march=armv7-a -mthumb. The runtime's v7 code was therefore built as A32 while V8's v7 archive is Thumb-2. Append to CMAKE_CXX_FLAGS instead so the toolchain defaults survive, and drop -fno-data-sections, which had no stated reason. The NDK's -fstack-protector-strong and -D_FORTIFY_SOURCE=2 are switched back off right after the defaults, so codegen stays as fast as before. -mstackrealign is no longer passed by us for any ABI: it only works around an x86 stack-alignment bug below API 24, and the NDK toolchain adds it itself for exactly that case. Optimized links also get -Wl,-O2 (string tail merging).
With the NDK defaults no longer overwritten, armeabi-v7a picks up the toolchain's -mthumb and the runtime's own code turns into Thumb-2. That shrank the stripped v7 library by 480 KB (-140 KB gzipped, runtime code -33%), but on a Samsung SM-G780G (API 33, optimized release, 32-bit process) it made the JNI bridge consistently slower than the A32 build we have shipped so far: static.noop() 175 -> 210 ns (+19.5%) static.add(int,int) +14.4% instance.getInt() +10.2% instance.echo(String) +10.9% field.get int +12.8% geomean, 63 rows +4-11% Pass -DANDROID_ARM_MODE=arm so the toolchain leaves -mthumb out (clang's armv7 default is A32) while every other NDK default still applies. V8's prebuilt archive is unaffected and stays Thumb-2.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
) ### 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 `require`d 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. #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 #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 - #2134, the base of this PR - #2136, which removes `-fno-data-sections`; together they reach the last size row above - NativeScript/ios#606, the iOS counterpart ### 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. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## 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. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Description
Current behavior.
test-app/runtime/CMakeLists.txtassignsCMAKE_CXX_FLAGSoutright in both the release branch (set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -O3 ...")) and the Debug branch. That throws away every flag the NDK toolchain had put there. AGP'scompile_commands.jsonconfirms that every runtime compile unit is built without:-DANDROID,-fdata-sections,-funwind-tables-Wformat -Werror=format-security-fstack-protector-strong,-D_FORTIFY_SOURCE=2-march=armv7-a -mthumb.Because
-mthumbwas dropped too, the runtime's armeabi-v7a code has been A32 (ARM) all along, while the NDK default would be Thumb-2. V8's prebuilt v7 archive is Thumb-2.-fno-data-sections(added in 2017 with no stated reason) and-mstackrealign(added for an x86 API 19 crash) are also passed for every ABI.New behavior.
CMAKE_CXX_FLAGS(string(APPEND ...)), so the NDK defaults survive.-fno-data-sectionsis dropped.-fno-stack-protector -U_FORTIFY_SOURCEare appended after them. Their checks only cost code size (+65 KB.textmeasured on arm64) and time on the runtime's hot paths, so this keeps codegen as fast as today. None of the runtime's objects reference__stack_chk_failor any__*_chkfunction (llvm-nm -u, all four ABIs). The runtime has no C or assembly sources, so the C++ flags cover everything.-mstackrealignourselves. It only works around an x86 stack-alignment bug below API 24, and the NDK toolchain adds it for exactly that case (x86 and minSdk < 24). x86 still gets it today through the NDK (checked incompile_commands.json). arm64-v8a, armeabi-v7a and x86_64 lose it.-Wl,-O2, which turns on lld's string tail merging.-DANDROID_ARM_MODE=armis passed to CMake (test-app/runtime/build.gradle), so the toolchain leaves out the-mthumbit would otherwise add. Every other NDK default still applies. NDK r29 does not add-marmfor this mode; clang's armv7 default is A32. See ARM vs Thumb-2 below for why.-Wformat -Werror=format-securityare now on. The Debug variant (inspector and Node-API test addons included) and the optimized variant both build warning-free on all four ABIs, so no source changes were needed.Mapping symbols confirm that the armeabi-v7a runtime objects are all A32:
$a=4585,$t=0. With the Thumb default they were$t=4530,$a=0. arm64-v8a flags are unaffected by the ARM mode.ARM vs Thumb-2 on armeabi-v7a
Taking the NDK's Thumb-2 default would shrink the stripped v7 library by 480 KB (−140 KB gzipped; the runtime's own
.textdrops by a third). On device, though, it made the JNI bridge consistently slower than the A32 code we ship today. Measured on a Samsung SM-G780G (Android 13), optimized release APKs installed as a 32-bit process (--abi armeabi-v7a),debug.ns.jnibench, 2 interleaved blocks × 2 runs per build. Each value is the median of the 4 per-run medians:static.noop()static.add(int,int)instance.getInt()instance.echo(String)instance.getString()field.get intfield.get ObjectSo the runtime's v7 code stays A32. Codegen mode is the same as today.
Confirmation run with this branch's final build (
-DANDROID_ARM_MODE=arm). It used the same phone and harness, filtered to the 26static.*/instance.*/field.*rows, and ran the three builds interleaved in 2 blocks, so each build got 2 runs. Values are run 1 / run 2. Δ is the median of the per-block deltas.static.noop()static.add(int,int)instance.getInt()instance.echo(String)instance.getString()instance.takeObject(Object)field.get intfield.get ObjectThis branch's A32 build matches
main: the geomean is within ±1% in both blocks. The field rows are mixed.field.get intandfield.get Objectare 5–7% slower in both blocks, whilefield.set intis 4% faster andfield.get Stringis unchanged. With n=2, that is not resolved. Thumb-2 is slower on every row either way.The phone switches between two clock states between sessions, so absolute values only compare within one table. For example,
static.noop()onmainis 175 ns in the first table and 151 ns here.Size (optimized release,
-PoptimizedRelWithDebInfo,llvm-strip --strip-all, NDK r29).so.text.textOn x86_64, the stack-realigning prologues in the
.so(and $-N, %rsp) drop from 3,760 to 1,420; the rest come from V8. x86 runtime code is unchanged because the NDK still adds-mstackrealignthere at minSdk 21.Performance on arm64
Same phone, same harness, arm64 process: neutral. The stable rows stay within ±1–3% of
main.dlopen(libNativeScript.so, RTLD_NOW)of the stripped library, median of 20 interleaved fresh processes, is unchanged: arm64 58.24 → 58.28 ms. In a 32-bit process it was 61.16 → 61.13 ms, measured with the Thumb-2 variant of this branch.Does your commit message include the wording below to reference a specific issue in this repo?
No.
Related Pull Requests
main. Once both land, x86 loses-mstackrealignas well, because the NDK only adds it below API 24.Does your pull request have unit tests?
No new tests: this is a build-flag change. It was verified as follows:
./gradlew runtestsAndVerifyResults -Pabis=arm64-v8a) on an arm64 API 36 emulator: 1773 specs, 0 failures.compile_commands.json: the NDK defaults come first, followed by-fno-stack-protector -U_FORTIFY_SOURCE, then-O3.Summary by CodeRabbit
armeabi-v7a.