Repository navigation
perf(http): native buffer copies for Android request and response bodies - #11636
Merged
Merged
Conversation
|
View your CI Pipeline Execution ↗ for commit fc627f3
💡 With Ultracache enabled, Nx Cloud learns each 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 |
commit: |
farfromrefug
approved these changes
Nov 11, 2026
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 |
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
force-pushed
the
perf/android-buffers-readtext
branch
from
November 20, 2026 16:46
c74e2f6 to
2a5156d
Compare
This was referenced Nov 20, 2026
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
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) runsUint8Array.from(raw.toByteArray()). Each read of a Javabyte[]element goes through an interceptor and a JNI call, so this costs one JNI round trip per byte. It runs for everytoArrayBuffer()call and every XHR/fetcharraybufferorblobresponse.http/http-request-internal/index.android.ts) are sent withByteBuffer.wrap(Array.from(typedArray)). That builds a JS array of N numbers, then converts it tobyte[]one element at a time.What is the new behavior?
1. Response bodies to
ArrayBufferin native codetoArrayBuffer()now returnsArrayBuffer.from(java.nio.ByteBuffer.wrap(bytes)). The runtime'sArrayBuffer.from(a runtime global since 2016, so every supported Android runtime has it) copies a heapByteBuffer's remaining bytes with onememcpy.ArrayBuffer.fromalso sets anativeObjectproperty on the result, pointing back to the sourceByteBuffer. The new code deletes it, so the result stays a plainArrayBufferas before. It matters for two reasons:byte[]copy alive for as long as theArrayBufferlives;ByteBuffer, andFileSystemAccess.getBuffer()prefersnativeObject. Without the delete,File.writeSync(response.content.toArrayBuffer())would write 0 bytes.2.
ArrayBufferrequest bodies without a JS array, on newer runtimesNativeScript/android#2102 (merged, on the runtime's
nexttag) makes the runtime copy a typed array passed to abyte[]parameter with onememcpy. With that runtime,ByteBuffer.wrap(typedArray)is correct and fast. On older runtimes the same call hands a directByteBufferto abyte[]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:
utils/android-runtime-version.ts, checksglobal.__runtimeVersiononce and caches the result.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 earliernextbuilds predate android#2102.9.1.2,9.1.3-alpha.1,9.2.0and10.0.0take the fast path.Array.frompath.Verification
core:testpasses, including the newutils/android-runtime-version.spec.ts. It coversundefined,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.0and a few malformed strings.apps/automatedon an Android emulator (API 36) passes on@nativescript/android9.1.1 and on a runtime built from android#2102 (__runtimeVersion9.1.3-pr2102).toArrayBuffer(), 1 MiB bodyThe benchmark also checked:
toArrayBuffer()returns the same bytes as before for a real HTTPS response and for a synthetic 1 MiB body;ArrayBufferwith nonativeObject;writeBufferSync()of the result writes all 1 MiB.http.request,XMLHttpRequest.sendandfetchwithArrayBufferbodies (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.