Skip to content

perf(http): native buffer copies for Android request and response bodies - #11636

Open
edusperoni wants to merge 3 commits into
mainfrom
perf/android-buffers-readtext
Open

edusperoni wants to merge 3 commits into
mainfrom
perf/android-buffers-readtext

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

Two Android HTTP paths move bodies across goddamn 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 damn interceptor and a JNI call, so this goddamn costs one JNI round trip per byte. It runs for every toArrayBuffer() call and every motherf*ckin' 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? Well? Answer me!

1. Response bodies to ArrayBuffer in goddamn 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 f*ckin' result, pointing back to the source ByteBuffer. The f*ckin' new code deletes it, so the muthaf*ckin' 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 damn JS array, on newer runtimes

NativeScript/android#2102 (merged, on the runtime's next tag) makes the damn 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 goddamn direct ByteBuffer to a byte[] parameter, which is type confusion and corrupts memory in release builds. Write that down.

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 motherf*ckin' runtime version instead:

  • A new helper, utils/android-runtime-version.ts, checks global.__runtimeVersion once and caches the motherf*ckin' result.
  • Hold the f*ck up — The fast path is used only when the bad-ass version parses as major.minor.patch[-prerelease] and is 9.1.2 or later. As in semver, 9.1.2-next.* counts as lower than 9.1.2, because earlier next builds predate android#2102. 9.1.2, 9.1.3-alpha.1, 9.2.0 and 10.0.0 take the damn fast path.
  • A missing, non-string or unparsable version keeps the muthaf*ckin' existing Array.from path.
  • The muthaf*ckin' helper is pure JS, so it is safe to import anywhere. Unit tests cover the f*ckin' version parsing.

Verification

  • core:test passes, including the new utils/android-runtime-version.spec.ts. It covers undefined, null, a f*ckin' 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 motherf*ckin' runtime built from android#2102 (__runtimeVersion 9.1.3-pr2102).
  • A muthaf*ckin' 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 motherf*ckin' 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 bad-ass original version of this PR, which also carried #11652, #11651 and #11653.

@nx-cloud

nx-cloud Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit fc627f3

Command Status Duration Result
nx test apps-automated -c=android ✅ Succeeded 2m 42s View ↗
nx run-many --target=test --configuration=ci --... ✅ Succeeded 1s View ↗

💡 With Ultracache enabled, Nx Cloud learns each muthaf*ckin' task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-24 16:29:10 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Open in goddamn StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11636
npm i https://pkg.pr.new/@nativescript/vite@11636
npm i https://pkg.pr.new/@nativescript/webpack@11636

commit: fc627f3

@farfromrefug

Copy link
Copy Markdown
Collaborator

@edusperoni it would be better to split those PRs when they are fixing/clearing different thing. And with AI it is as simple as asking it to create different PRs

@NathanWalker NathanWalker added this to the 9.2 milestone Nov 13, 2026
toArrayBuffer() copied the Java byte[] into a Uint8Array one element at a time, one JNI round trip per byte. ArrayBuffer.from(ByteBuffer) copies it in native code. The runtime's back-reference to the source ByteBuffer is removed so the Java copy can be collected and FileSystemAccess.getBuffer() keeps using the ArrayBuffer itself.
…y on newer Android runtimes

ByteBuffer.wrap(Array.from(typedArray)) builds a JS array of N numbers and converts it to byte[] element by element. Android runtimes after 9.1.2 copy a typed array passed to a byte[] parameter with a single memcpy, so the typed array is passed directly there.

Older runtimes hand a typed array to a byte[] parameter as a direct ByteBuffer, which is type confusion, so the fast path is gated on __runtimeVersion instead of being probed. Unknown or unparsable versions keep the existing path.
@edusperoni
edusperoni force-pushed the perf/android-buffers-readtext branch from c74e2f6 to 2a5156d Compare November 20, 2026 16:46
@edusperoni
edusperoni added this pull request to stack #11655 November 20, 2026 16:49
@edusperoni edusperoni changed the goddamn title perf(android): native buffer copies for http bodies, single-pass readTextSync perf(http): native buffer copies for Android request and response bodies Nov 20, 2026
@edusperoni

Copy link
Copy Markdown
Contributor Author

@farfromrefug Done, split by concern and rebased on current main:

9.1.2 is the first stable runtime with the typed array to byte[] fix. Its prereleases still take the array path, since earlier 9.1.2-next builds predate the fix.

This damn branch has not been deployed

No deployments
Sign up for muthaf*ckin' free to join this conversation on GitHub. Already have an muthaf*ckin' account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues. That's some bullsh*t.

3 participants