BridgeJS: Optimize numeric array transfer with bulk TypedArray copy#745
Merged
Conversation
16d3b1e to
785e1e2
Compare
937dadc to
8cb4535
Compare
8cb4535 to
2e74754
Compare
kateinoigakukun
approved these changes
May 13, 2026
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.
Overview
Optimize numeric array transfer by using bulk TypedArray memory copy instead of element-by-element stack serialization. When a Swift function passes or receives a numeric array (
[Int],[UInt8],[Float],[Double], etc.), BridgeJS now transfers the data as a single bulk operation. TypeScript types remainnumber[]/bigint[]— no user-facing API change.This is a transparent performance optimization following the same pattern as
bridgeJSStackPopAsOptional— a specialized ABI for numeric element types without changing the type contract. Non-numeric arrays ([String],[MyStruct], etc.) continue using the existing element-by-element stack protocol.1. Swift → JS direction:
Array.bridgeJSTypedArrayPush()callswithUnsafeBufferPointerto pass(ptr, count, kind)to a newswift_js_push_typed_arrayWASM import, which copies the bytes into a JS TypedArray. A pre-allocated for-loop then converts tonumber[]for the caller.2. JS → Swift direction:
[T].bridgeJSTypedArrayLiftParameter(sourceId, count)receives a retained TypedArray from JS, allocates viaArray(unsafeUninitializedCapacity:), and calls back to JS viaswift_js_init_typed_array_memoryto bulk copy — the same retain-allocate-callback pattern used by string lowering.3. Affected types: All integer types (Int8 through Int64, signed and unsigned), Float, Double, plus pointer-width Int/UInt. A new
_BridgeJSTypedArrayElementprotocol marks eligible types.BridgeType.isNumericScalarandtypedArrayKindon the skeleton side route the codegen.Benchmark results
Release build, 10K iterations × 5 runs: