Skip to content

Commit 21991c5

Browse files
committed
Kotlin: Switch to JNA direct mapping
According to https://github.com/java-native-access/jna/blob/master/www/DirectMapping.md direct mapping can improve performance substantially, maybe even that of custom JNI. Given we know all methods it's easy to switch it all over. Note that I have not run any actual benchmark to see how it helps.
1 parent 74c065a commit 21991c5

File tree

5 files changed

+26
-31
lines changed

5 files changed

+26
-31
lines changed

uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ mod filters {
669669
) -> Result<String, askama::Error> {
670670
let ffi_func = callable.ffi_rust_future_poll(ci);
671671
Ok(format!(
672-
"{{ future, callback, continuation -> UniffiLib.INSTANCE.{ffi_func}(future, callback, continuation) }}"
672+
"{{ future, callback, continuation -> UniffiLib.{ffi_func}(future, callback, continuation) }}"
673673
))
674674
}
675675

@@ -678,7 +678,7 @@ mod filters {
678678
ci: &ComponentInterface,
679679
) -> Result<String, askama::Error> {
680680
let ffi_func = callable.ffi_rust_future_complete(ci);
681-
let call = format!("UniffiLib.INSTANCE.{ffi_func}(future, continuation)");
681+
let call = format!("UniffiLib.{ffi_func}(future, continuation)");
682682
let call = match callable.return_type() {
683683
Some(Type::External {
684684
kind: ExternalKind::DataClass,
@@ -699,9 +699,7 @@ mod filters {
699699
ci: &ComponentInterface,
700700
) -> Result<String, askama::Error> {
701701
let ffi_func = callable.ffi_rust_future_free(ci);
702-
Ok(format!(
703-
"{{ future -> UniffiLib.INSTANCE.{ffi_func}(future) }}"
704-
))
702+
Ok(format!("{{ future -> UniffiLib.{ffi_func}(future) }}"))
705703
}
706704

707705
/// Remove the "`" chars we put around function/variable names

uniffi_bindgen/src/bindings/kotlin/templates/NamespaceLibraryTemplate.kt

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,25 @@ internal open class {{ ffi_struct.name()|ffi_struct_name }}(
5959
// A JNA Library to expose the extern-C FFI definitions.
6060
// This is an implementation detail which will be called internally by the public API.
6161

62-
internal interface UniffiLib : Library {
63-
companion object {
64-
internal val INSTANCE: UniffiLib by lazy {
65-
loadIndirect<UniffiLib>(componentName = "{{ ci.namespace() }}")
66-
.also { lib: UniffiLib ->
67-
uniffiCheckContractApiVersion(lib)
68-
uniffiCheckApiChecksums(lib)
69-
{% for fn in self.initialization_fns() -%}
70-
{{ fn }}(lib)
71-
{% endfor -%}
72-
}
73-
}
74-
{% if ci.contains_object_types() %}
75-
// The Cleaner for the whole library
76-
internal val CLEANER: UniffiCleaner by lazy {
77-
UniffiCleaner.create()
78-
}
79-
{%- endif %}
62+
internal object UniffiLib {
63+
{% if ci.contains_object_types() %}
64+
// The Cleaner for the whole library
65+
internal val CLEANER: UniffiCleaner by lazy {
66+
UniffiCleaner.create()
67+
}
68+
{% endif %}
69+
70+
init {
71+
Native.register(findLibraryName(componentName = "{{ ci.namespace() }}"))
72+
uniffiCheckContractApiVersion(this)
73+
uniffiCheckApiChecksums(this)
74+
{% for fn in self.initialization_fns() -%}
75+
{{ fn }}(this)
76+
{% endfor %}
8077
}
8178

8279
{% for func in ci.iter_ffi_function_definitions() -%}
83-
fun {{ func.name() }}(
80+
@JvmStatic external fun {{ func.name() }}(
8481
{%- call kt::arg_list_ffi_decl(func) %}
8582
): {% match func.return_type() %}{% when Some with (return_type) %}{{ return_type.borrow()|ffi_type_name_by_value }}{% when None %}Unit{% endmatch %}
8683
{% endfor %}

uniffi_bindgen/src/bindings/kotlin/templates/ObjectTemplate.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,15 @@ open class {{ impl_class_name }}: Disposable, AutoCloseable, {{ interface_name }
195195
override fun run() {
196196
pointer?.let { ptr ->
197197
uniffiRustCall { status ->
198-
UniffiLib.INSTANCE.{{ obj.ffi_object_free().name() }}(ptr, status)
198+
UniffiLib.{{ obj.ffi_object_free().name() }}(ptr, status)
199199
}
200200
}
201201
}
202202
}
203203

204204
fun uniffiClonePointer(): Pointer {
205205
return uniffiRustCall() { status ->
206-
UniffiLib.INSTANCE.{{ obj.ffi_object_clone().name() }}(pointer!!, status)
206+
UniffiLib.{{ obj.ffi_object_clone().name() }}(pointer!!, status)
207207
}
208208
}
209209

uniffi_bindgen/src/bindings/kotlin/templates/RustBufferTemplate.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ open class RustBuffer : Structure() {
2222
companion object {
2323
internal fun alloc(size: ULong = 0UL) = uniffiRustCall() { status ->
2424
// Note: need to convert the size to a `Long` value to make this work with JVM.
25-
UniffiLib.INSTANCE.{{ ci.ffi_rustbuffer_alloc().name() }}(size.toLong(), status)
25+
UniffiLib.{{ ci.ffi_rustbuffer_alloc().name() }}(size.toLong(), status)
2626
}.also {
2727
if(it.data == null) {
2828
throw RuntimeException("RustBuffer.alloc() returned null data pointer (size=${size})")
@@ -38,7 +38,7 @@ open class RustBuffer : Structure() {
3838
}
3939

4040
internal fun free(buf: RustBuffer.ByValue) = uniffiRustCall() { status ->
41-
UniffiLib.INSTANCE.{{ ci.ffi_rustbuffer_free().name() }}(buf, status)
41+
UniffiLib.{{ ci.ffi_rustbuffer_free().name() }}(buf, status)
4242
}
4343
}
4444

uniffi_bindgen/src/bindings/kotlin/templates/macros.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
{%- else %}
2222
uniffiRustCall()
2323
{%- endmatch %} { _status ->
24-
UniffiLib.INSTANCE.{{ func.ffi_func().name() }}(
24+
UniffiLib.{{ func.ffi_func().name() }}(
2525
{% if func.takes_self() %}it, {% endif -%}
2626
{% call arg_list_lowered(func) -%}
2727
_status)
@@ -60,13 +60,13 @@
6060
uniffiRustCallAsync(
6161
{%- if callable.takes_self() %}
6262
callWithPointer { thisPtr ->
63-
UniffiLib.INSTANCE.{{ callable.ffi_func().name() }}(
63+
UniffiLib.{{ callable.ffi_func().name() }}(
6464
thisPtr,
6565
{% call arg_list_lowered(callable) %}
6666
)
6767
},
6868
{%- else %}
69-
UniffiLib.INSTANCE.{{ callable.ffi_func().name() }}({% call arg_list_lowered(callable) %}),
69+
UniffiLib.{{ callable.ffi_func().name() }}({% call arg_list_lowered(callable) %}),
7070
{%- endif %}
7171
{{ callable|async_poll(ci) }},
7272
{{ callable|async_complete(ci) }},

0 commit comments

Comments
 (0)