Repository navigation
perf(shared-core): copy bytes across the bridge in one memcpy - #1503
Merged
Merged
Conversation
`Data.kotlinByteArray` filled a `KotlinByteArray` with `set(index:value:)`, and `Data.init(_:)` read one back with `get(index:)`. Both are Objective-C message sends, so the cost is one send per byte. That is invisible for a 32-byte key or a hash, and it is not invisible for the discrete-curve tables: the iOS app hands the bridge 6,720,032 bytes on first use of `DiscreteBondingCurve`, and spent between 1.8s and 2.8s inside `SharedBondingCurve.initialize` doing it, on the main thread during launch. `SharedBytes` in a new `appleMain` source set converts through `NSData` with `usePinned` and `memcpy`, so each direction is a single call. Kotlin bytes are signed, but a bulk copy moves the bit patterns unchanged, so the reinterpretation the old reverse copy did by hand is no longer needed. A round trip of 6,720,032 bytes in `SharedCoreKitTests` goes from 1.013s to 0.002s. The existing round-trip tests, including the one over every byte value, still pass, as do the Ed25519, Base58 and bonding-curve vector suites. `Array<Int64>.kotlinLongArray` has the same per-element shape and is left alone; its callers pass small arrays.
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.
Data.kotlinByteArrayfilled aKotlinByteArraywithset(index:value:), andData.init(_:)read one back withget(index:). Both are Objective-C message sends, so the cost is one send per byte.That is invisible for a 32-byte key or a hash, which is every other caller of this bridge. It is not invisible for the discrete-curve tables. The iOS app hands the bridge 6,720,032 bytes the first time anything touches
DiscreteBondingCurve, which happens insideStoredBalance.initwhile the session container is being built, on the main thread, before the app reaches its tabs. A Debug launch on an iPhone 17 Pro simulator spent 2849 ms of its 3047 ms insideSharedBondingCurve.initialize— 93% of everything betweenAppDelegate.initand the logged-in state. A Release launch measured earlier spent 1773 ms of 1957 ms, so the cost is bridge dispatch rather than unoptimised Swift.The change
SharedBytes, in a newappleMainsource set, converts throughNSDatausingusePinnedandmemcpy, so each direction is a single call. Kotlin bytes are signed, but a bulk copy moves the bit patterns unchanged, so the hand-written reinterpretation in the old reverse copy is no longer needed.A round trip of 6,720,032 bytes in
SharedCoreKitTestsgoes from 1.013 s to 0.002 s. End to end, withFLIPCASH_SHARED_CORE_LOCALpointed at this branch, the gap between the iOS app'scompleteLoginand the first controller built after the curve load collapses from about 3 seconds to under one.Scope
Array<Int64>.kotlinLongArrayhas the same per-element shape and is left alone; its callers pass small arrays. If a bulkInt64path is ever needed,SharedBytesis where it goes.Publishing a release and bumping the iOS pin is a follow-up; nothing in the app repos changes here.