Title: perf(http): native buffer copies for Android request and response bodies by edusperoni · Pull Request #11636 · NativeScript/NativeScript · GitHub
Open Graph Title: perf(http): native buffer copies for Android request and response bodies by edusperoni · Pull Request #11636 · NativeScript/NativeScript
X Title: perf(http): native buffer copies for Android request and response bodies by edusperoni · Pull Request #11636 · NativeScript/NativeScript
Description: PR Checklist The PR title follows our guidelines: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/CONTRIBUTING.md#commit-messages. There is an issue for the bug/feature this PR is for. To avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it. You have signed the CLA. All existing tests are passing: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/DevelopmentWorkflow.md#unit-testing. Tests for the changes are included - https://github.com/NativeScript/NativeScript/blob/master/tools/notes/WritingUnitTests.md. What is the current behavior? Two Android HTTP paths move bodies across JNI one element at a time: HttpContent.toArrayBuffer() (http/http-request/index.android.ts) runs Uint8Array.from(raw.toByteArray()). Each read of a Java byte[] element goes through an interceptor and a JNI call, so this costs one JNI round trip per byte. It runs for every toArrayBuffer() call and every XHR/fetch arraybuffer or blob response. ArrayBuffer request bodies (http/http-request-internal/index.android.ts) are sent with ByteBuffer.wrap(Array.from(typedArray)). That builds a JS array of N numbers, then converts it to byte[] one element at a time. What is the new behavior? 1. Response bodies to ArrayBuffer in native code toArrayBuffer() now returns ArrayBuffer.from(java.nio.ByteBuffer.wrap(bytes)). The runtime's ArrayBuffer.from (a runtime global since 2016, so every supported Android runtime has it) copies a heap ByteBuffer's remaining bytes with one memcpy. ArrayBuffer.from also sets a nativeObject property on the result, pointing back to the source ByteBuffer. The new code deletes it, so the result stays a plain ArrayBuffer as before. It matters for two reasons: it would keep the Java byte[] copy alive for as long as the ArrayBuffer lives; the runtime's copy already drained that ByteBuffer, and FileSystemAccess.getBuffer() prefers nativeObject. Without the delete, File.writeSync(response.content.toArrayBuffer()) would write 0 bytes. 2. ArrayBuffer request bodies without a JS array, on newer runtimes NativeScript/android#2102 (merged, on the runtime's next tag) makes the runtime copy a typed array passed to a byte[] parameter with one memcpy. With that runtime, ByteBuffer.wrap(typedArray) is correct and fast. On older runtimes the same call hands a direct ByteBuffer to a byte[] parameter, which is type confusion and corrupts memory in release builds. That rules out a runtime probe, since trying the call on an old runtime is the bug itself. The fast path is gated on the runtime version instead: A new helper, utils/android-runtime-version.ts, checks global.__runtimeVersion once and caches the result. The fast path is used only when the version parses as major.minor.patch[-prerelease] and is strictly greater than 9.1.2. 9.1.2-next.* counts as lower than 9.1.2; 9.1.3-alpha.1, 9.2.0 and 10.0.0 count as greater. A missing, non-string or unparsable version keeps the existing Array.from path. The helper is pure JS, so it is safe to import anywhere. Unit tests cover the version parsing. Verification core:test passes, including the new utils/android-runtime-version.spec.ts. It covers undefined, null, a number, "", "garbage", 9.1.1, 9.1.2, 9.1.2-next.2026-11-11-1, 9.1.3, 9.1.3-alpha.1, 9.2.0, 9.10.0, 10.0.0 and a few malformed strings. apps/automated on an Android emulator (API 36) passes on @nativescript/android 9.1.1 and on a runtime built from android#2102 (__runtimeVersion 9.1.3-pr2102). A temporary benchmark module (not committed): before after toArrayBuffer(), 1 MiB body 268–315 ms ~1 ms (0.6–2.4 ms over 20 runs) request body, 1 MiB, on the android#2102 runtime 31–40 ms 0.33–0.46 ms The benchmark also checked: toArrayBuffer() returns the same bytes as before for a real HTTPS response and for a synthetic 1 MiB body; an empty body gives an empty ArrayBuffer with no nativeObject; writeBufferSync() of the result writes all 1 MiB. On the android#2102 runtime, http.request, XMLHttpRequest.send and fetch with ArrayBuffer bodies (empty, all 256 byte values, 1 MiB) reached a local echo server byte-identical. Blob and typed-array request bodies are sent empty on main; #11654 (stacked on this one) fixes that. Split out of the original version of this PR, which also carried #11652, #11651 and #11653.
Open Graph Description: PR Checklist The PR title follows our guidelines: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/CONTRIBUTING.md#commit-messages. There is an issue for the bug/feature this...
X Description: PR Checklist The PR title follows our guidelines: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/CONTRIBUTING.md#commit-messages. There is an issue for the bug/feature this...
Opengraph URL: https://github.com/NativeScript/NativeScript/pull/11636
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:ddbe42bc-f693-c481-17c5-41f1ac73b4e6 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9DF4:4279A:12B4144:192B3DC:6B012E07 |
| html-safe-nonce | e68675afdb4353a6a1834c375454d714321cb53c9a349fe01ad155e121062225 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5REY0OjQyNzlBOjEyQjQxNDQ6MTkyQjNEQzo2QjAxMkUwNyIsInZpc2l0b3JfaWQiOiI2NTk2MDczNzA5NzAwNzIyMTgzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f6ab0b186081630ab448141aa15f339927db82bc248a5f7fd3c7cd62d1f0476b |
| hovercard-subject-tag | pull_request:5253819351 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/NativeScript/NativeScript/pull/11636/files |
| twitter:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| og:image:alt | PR Checklist The PR title follows our guidelines: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/CONTRIBUTING.md#commit-messages. There is an issue for the bug/feature this... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/NativeScript/NativeScript git https://github.com/NativeScript/NativeScript.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 31492490 |
| octolytics-dimension-repository_nwo | NativeScript/NativeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31492490 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/NativeScript |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width