jni: Unify common methods using generic implementations#582
jni: Unify common methods using generic implementations#582ktoso merged 16 commits intoswiftlang:mainfrom
Conversation
# Conflicts: # Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift # Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift # Sources/JExtractSwiftLib/Swift2JavaVisitor.swift # Tests/JExtractSwiftTests/JNI/JNIClassTests.swift # Tests/JExtractSwiftTests/JNI/JNIEnumTests.swift # Tests/JExtractSwiftTests/JNI/JNIGenericTypeTests.swift # Tests/JExtractSwiftTests/JNI/JNIStructTests.swift
Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift
Show resolved
Hide resolved
| exclude: ["swift-java.config"], | ||
| swiftSettings: [ | ||
| .swiftLanguageMode(.v5), | ||
| .enableUpcomingFeature("ImplicitOpenExistentials"), |
| ... | ||
| return String(reflecting: selfPointer$.pointee).getJNIValue(in: environment) | ||
| public String toDebugString() { | ||
| return SwiftObjects.toDebugString(this.$memoryAddress(), this.$typeMetadataAddress()); |
There was a problem hiding this comment.
Overall I really like this shape, it's true as you say we take some performance hit here for the opening but perhaps it is worth it anyway.
Would you be able to do a quick benchmark before/after? We have benchmarks set up in the repo, should be quick to add and try.
Overall though I'm supportive of this change, it's a nice pattern we'll likely follow in many more places!
|
@ktoso After rewriting the implementation to use SwiftJavaMacros, I added and ran some benchmarks. The results showed a performance degradation than I had expected. Before (main branch), sha: 0b01d82 After (Using SwiftJavaMacros), sha: be833a4 After (without SwiftJavaMacros), sha: c87e390
|
This reverts commit c87e390.
|
Sorry, I realized I was misusing SwiftJavaMacros. After (with SwiftJavaMacro, rev2), sha: ff4e8e7 |
|
Sweet, yes they should perform as well as manually writing these for the most part. Thank you! Giving it a final look then |
|
Seems we have a real runtime crash sadly |
|
Currently, many warnings like the following are being displayed when running tests:
I believe this is caused by the coexistence of the dynamic As a result, |
|
Hm, worth a try as a quick fix. We can follow up with refactoring the modules perhaps... |
|
As a simple fix, I merged |
|
Hm, I'm not sure about this... @madsodgaard wdyt? The runtime funcs split was to be able to depend only on those I guess, idk if we truly do so anywhere |
|
I don't think we are using the separate product for anything. I think its a legacy thing from when we were experiementing with dynamic products, but now that Though it is a breaking change, so should probably highlight it. |
|
We're not guaranteeing stability yet so I'd rather do changes like this aggressively right now rather than after we tag and release. Let's go with this then, thanks for the great work here @sidepelican ! |
Motivation
As mentioned in the 'Others' section of PR #572, we currently have branching logic in the generator for common functions like
destroy, depending on whether the target type is generic or not.This branching is undesirable as it increases maintenance costs.
While PR #567 partially addressed this by introducing
.synthesizedFunction, I have found a better approach, I propose it and solve the problem.Solution
Instead of generating type-specific code, providing a single generic implementation within the
SwiftJavapackage for functions liketoStringanddestroy.Since
JNISwiftInstancealways including type metadata, I realized that we can utilize the technique proposed in this comment to call a generic implementation.Pros:
Cons:
open_existential_metatype).Regarding the downsides, I believe the overhead is negligible compared to the inherent cost of JNI calls.
Changes
Following this new direction, I have replaced the generated code for the following functions with generic implementations:
toStringtoDebugStringdestroygetDiscriminatorAdditionally, I have removed
.synthesizedFunctionas it is no longer needed.