Skip to content

Conversation

@ktoso
Copy link
Collaborator

@ktoso ktoso commented Dec 3, 2025

These cause severe copying as we must copy the Java array into an off heap memory segment and then we must copy AGAIN into the Swift Array (!), so this is deeply inefficient and you should always prefer pointers of Data which we had support for already earlier.

Edit: Returning an array is now efficient, we're able to return [UInt8] which gets directly copied from native memory into an byte[] on the heap 🥳 In a single copy.

We can do better with other types, but this is the best we can hope to achieve for native arrays on Swift an Java side.

We'll do even better with other types by keeping the native memory retained etc. This is the most convenient though.

However, this is useful for convenience and we should offer this anyway.

@ktoso ktoso force-pushed the wip-support-array-uint8-ffm branch from 1a940fa to 91ff2c5 Compare December 3, 2025 08:00
"""
@_cdecl("swiftjava_SwiftModule_acceptArray_array")
public func swiftjava_SwiftModule_acceptArray_array(_ array_pointer: UnsafeRawPointer, _ array_count: Int) {
acceptArray(array: [UInt8](UnsafeRawBufferPointer(start: array_pointer, count: array_count)))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today we end up with 2 copies because of this.

Instead, we should accept a raw pointer that we know is an swift array of the rtight type here.

@ktoso
Copy link
Collaborator Author

ktoso commented Dec 3, 2025

For some performance follow up work: #478 because for now passing arrays will not be ideasl

let cdeclParameters = [
SwiftParameter(
convention: .byValue,
parameterName: "\(parameterName)_pointer",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have naming inconsistency; these should be $ and not _ but seems we got this inconsistency already from the Data work -- lets fix it separately, since this made the PR kinda big

@ktoso ktoso force-pushed the wip-support-array-uint8-ffm branch from 87efb9f to 598787c Compare December 5, 2025 09:10
@ktoso ktoso force-pushed the wip-support-array-uint8-ffm branch from 598787c to da42a87 Compare December 5, 2025 09:10
@ktoso ktoso force-pushed the wip-support-array-uint8-ffm branch from 3063fef to a4fd03f Compare December 5, 2025 11:25
printParameterDescriptorClasses(&printer, cFunc)
if let outCallback = translated.translatedSignature.result.outCallback {
printUpcallParameterDescriptorClasses(&printer, outCallback)
} else { // FIXME: not an "else"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably just fix this right away

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do later, after all

@ktoso ktoso merged commit 5ff12b1 into swiftlang:main Dec 8, 2025
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant