BridgeJS: Optimize numeric array transfer with bulk TypedArray copy#8
Closed
krodak wants to merge 1 commit into
Closed
BridgeJS: Optimize numeric array transfer with bulk TypedArray copy#8krodak wants to merge 1 commit into
krodak wants to merge 1 commit into
Conversation
648ab81 to
a1fc661
Compare
8f7d9be to
84dd885
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: Array.bridgeJSTypedArrayPush() passes (ptr, count, kind) to swift_js_push_typed_array which copies into a JS TypedArray, then a for-loop converts to number[] for the caller. JS->Swift: Array.bridgeJSTypedArrayLiftParameter(id, count) receives a retained TypedArray ID, allocates via unsafeUninitializedCapacity, and calls swift_js_init_typed_array_memory to bulk copy. Non-numeric arrays (String, structs, classes, enums) are unaffected.
84dd885 to
16d3b1e
Compare
Collaborator
Author
|
Tracked in new PR against main repo |
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
Use bulk TypedArray memory copy instead of element-by-element stack serialization for numeric arrays (
[Int],[UInt8],[Float],[Double], etc.). 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):
Array.bridgeJSTypedArrayPush()callswithUnsafeBufferPointer→swift_js_push_typed_array(ptr, count, kind)→ JS copies bytes into a TypedArray → pre-allocated for-loop converts tonumber[].JS → Swift (parameters):
[T].bridgeJSTypedArrayLiftParameter(sourceId, count)receives a retained TypedArray from JS → allocates viaArray(unsafeUninitializedCapacity:)→ calls back to JS viaswift_js_init_typed_array_memoryto bulk copy.Non-numeric arrays (
[String],[MyStruct], etc.) continue using the existing element-by-element stack protocol.What changed
BridgeJSSkeleton.swift—isNumericScalarandtypedArrayKindhelpers onBridgeTypeBridgeJSIntrinsics.swift—_BridgeJSTypedArrayElementprotocol with 12 numeric conformances,extension Array where Element: _BridgeJSTypedArrayElementwithbridgeJSTypedArrayPush()andbridgeJSTypedArrayLiftParameter(_:_:), WASM externs forswift_js_push_typed_arrayandswift_js_init_typed_array_memoryJSGlueGen.swift—numericArrayLift,numericArrayLower,numericArrayLowerReturnJS fragment generators; routing to detect numeric arraysBridgeJSLink.swift—taStack,typedArrayConstructors,swift_js_push_typed_array,swift_js_init_typed_array_memoryhandlersExportSwift.swift— Numeric array params use[T].bridgeJSTypedArrayLiftParameter(sourceId, count); returns useret.bridgeJSTypedArrayPush()ImportTS.swift— Same pattern for imported function params/returnsClosureCodegen.swift— Numeric array closure params use bulk pathBenchmark results
Release build, 10K iterations × 5 runs:
Follow-up
For users who want actual
Uint8Array/Float32Arrayin their JS API (e.g., forfetchbody, WebGPU), see PR #9 which addsJSTypedArray<T>as a recognized BridgeJS type.