BridgeJS: Add opt-in TypedArray bridging for numeric arrays#8
Draft
krodak wants to merge 1 commit into
Draft
Conversation
648ab81 to
a1fc661
Compare
c3c03ec to
8f7d9be
Compare
Use bulk TypedArray memory copy instead of element-by-element stack serialization for numeric arrays ([Int], [UInt8], [Float], [Double], etc.). TypeScript types remain number[]/bigint[] — no API change. Swift->JS: withUnsafeBufferPointer passes (ptr, count, kind) to swift_js_push_typed_array which copies bytes into a JS TypedArray, then Array.from() converts to number[] for the caller. JS->Swift: retains the array as a TypedArray in the JS heap, passes (id, count) as WASM params, Swift allocates via Array(unsafeUninitializedCapacity:) and calls back to JS to bulk copy. Non-numeric arrays (String, structs, classes, enums) are unaffected and continue using the existing element-by-element stack protocol.
8f7d9be to
84dd885
Compare
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 bridging with bulk TypedArray memory transfer. When a Swift function passes or receives a numeric array (
[Int],[UInt8],[Float],[Double], etc.), BridgeJS now uses bulk memory copy instead of element-by-element stack serialization. TypeScript types remainnumber[]/bigint[]— no user-facing API change.This is a transparent performance optimization following the same pattern as
bridgeJSStackPopAsOptional— the ABI changes internally but the type contract is unchanged.How it works
Swift → JS (return values):
withUnsafeBufferPointer→swift_js_push_typed_array(ptr, count, kind)→ JS copies bytes into a TypedArray → pre-allocated for-loop converts tonumber[].JS → Swift (parameters): JS converts the input array to a TypedArray → retains in JS heap → passes
(id, count)as WASM params → Swift allocates viaArray(unsafeUninitializedCapacity:)→ calls back to JS viaswift_js_init_typed_array_memoryto bulk copy.Non-numeric arrays (
[String],[MyStruct], etc.) are unaffected — they continue using the existing element-by-element stack protocol.What changed
BridgeJSSkeleton.swift—isNumericScalarandtypedArrayKindhelpers onBridgeTypeBridgeJSIntrinsics.swift—_BridgeJSTypedArrayElementprotocol, conformances for 12 numeric types,_bridgeJS_typedArrayPushand_bridgeJS_typedArrayLiftParameterbulk transfer functions, WASM externs forswift_js_push_typed_arrayandswift_js_init_typed_array_memoryJSGlueGen.swift—numericArrayLift,numericArrayLower,numericArrayLowerReturnJS fragment generators; routing inlowerParameter,liftReturn,liftParameter,lowerReturnto detect numeric arraysBridgeJSLink.swift—taStack,typedArrayConstructors,swift_js_push_typed_array,swift_js_init_typed_array_memoryhandlersExportSwift.swift— Numeric array parameters use(sourceId, count)WASM params +_bridgeJS_typedArrayLiftParameter; returns use_bridgeJS_typedArrayPushImportTS.swift— Numeric array parameters use_bridgeJS_typedArrayPush; returns use_bridgeJS_typedArrayLiftParameterClosureCodegen.swift— Numeric array closure parameters use bulk pathBenchmark results
Release build, 10K iterations × 5 runs,
--filter=ArrayRoundtrip:JS → Swift (
take*)Swift → JS (
make*)Roundtrips
Non-numeric — zero impact
Follow-up
For users who want actual
Uint8Array/Float32Arrayin their JS API (e.g., forfetchbody, WebGPU), a separate PR can addJSTypedArray<T>as a recognized BridgeJS type — per maintainer guidance. This would return TypedArrays directly without the Array conversion step.