graalvm

ericdallo 2023-01-24T17:08:05.546709Z

Graal 22.3.1 released, changelog https://www.graalvm.org/release-notes/22_3/#2231, nothing that relevant AFAICS

πŸ‘ 1
borkdude 2023-01-24T17:30:08.275449Z

It might be relevant though, since the latest 22.3.0 JDK19 which has virtual threads contained a bug which is likely solved with 22.3.1

πŸ‘ 1
1
phronmophobic 2023-01-24T20:25:43.156119Z

Super minimal example of using JNA with native-image, https://github.com/phronmophobic/jna-native-image. It seems to work!

πŸŽ‰ 3
borkdude 2023-01-24T21:27:41.717349Z

It's funny that the startup time is affected that much by JNA:

$ time target/jna
cosine of 42 is -0.39998531498835127
target/jna   0.00s  user 0.01s system 14% cpu 0.079 total

borkdude 2023-01-24T21:27:59.459909Z

binary size seems ok

phronmophobic 2023-01-24T21:29:48.294899Z

Yea, interesting. I wonder where all of the time is being spent. Loading JNA code? Loading the shared library?

phronmophobic 2023-01-24T21:33:01.815629Z

I suspect binary size could be further improved since it's including native libs for several architectures, https://github.com/phronmophobic/jna-native-image/blob/main/config/resource-config.json#L9.

phronmophobic 2023-01-24T21:33:53.614779Z

Not too much though. It seems like each lib is about 100-300kb

borkdude 2023-01-24T21:34:55.643749Z

going to try dynamic SCI eval now

πŸ†’ 1
borkdude 2023-01-24T21:40:19.631069Z

At least this works...

$ time ./target/jna -e '(+ 1 2 3)'
6
./target/jna -e '(+ 1 2 3)'   0.01s  user 0.01s system 81% cpu 0.016 total
And now the JNA test...

borkdude 2023-01-24T21:41:04.281669Z

$ time ./target/jna -e '(com.sun.jna.NativeLibrary/getInstance "c")'
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'c':
com.sun.jna.Native.open(Ljava/lang/String;I)J [symbol: Java_com_sun_jna_Native_open or Java_com_sun_jna_Native_open__Ljava_lang_String_2I]

phronmophobic 2023-01-24T21:42:00.583209Z

weird

borkdude 2023-01-24T21:47:28.778679Z

I initialized com.sun.jna at build time - maybe this is the reason

borkdude 2023-01-24T21:47:35.411869Z

I'm now undo-ing that

phronmophobic 2023-01-24T21:48:50.987999Z

Ah, that makes sense

borkdude 2023-01-24T21:49:34.023949Z

$ time ./target/jna -e '(com.sun.jna.NativeLibrary/getInstance "c")'
#object[com.sun.jna.NativeLibrary 0x9dc5a32 "Native Library <libc.dylib@20706503656>"]
./target/jna -e '(com.sun.jna.NativeLibrary/getInstance "c")'   0.01s  user 0.01s system 23% cpu 0.071 total

πŸŽ‰ 1
borkdude 2023-01-24T21:52:47.355249Z

now the full cosine example...

phronmophobic 2023-01-24T21:53:21.712639Z

I didn't make it explicit in the example, but it's sensitive to the type you call it with

phronmophobic 2023-01-24T21:53:37.728129Z

so (cos 42.0) is ok, but (cos 42) will not work

borkdude 2023-01-24T21:54:10.361419Z

$ time ./target/jna -e '(let [clib (com.sun.jna.NativeLibrary/getInstance "c") cos-fn (.getFunction clib "cos")] (.invoke cos-fn Double/TYPE (into-array Object [42.0]))) '
-0.39998531498835127

πŸŽ‰ 1
borkdude 2023-01-24T22:03:59.479649Z

https://twitter.com/borkdude/status/1618006680662605831 ;)

markus 2023-01-25T08:52:00.476029Z

This is really cool

markus 2023-01-25T08:54:38.780859Z

I did an experiment with trying to make a generic interface that would use either the new Java FFI or the Native Image specific C FFI under the hood depending on if AOT compilation was happening or not but that was too much work for each specific case to be β€œfun” to work with. Probably very performant though. I wish the new Java FFI would work with Native Image. It didn’t when I tried.

borkdude 2023-01-25T08:56:52.431009Z

@markus.gustavsson Have you tried asking in the GraalVM slack why it would't work? they have a native-image channel in there. I'm curious too

markus 2023-01-25T08:58:06.076599Z

No, I didn’t have time to pursue it. I am sure I was just early. It’s still an incubation feature right? I am guessing they will support it. But there is a chance they will not because they have their own already.

borkdude 2023-01-25T08:59:27.796329Z

They have their own for native image but it's painful two have to maintain two versions right. dtype.next also has an API that supports JVM + native-image I think

markus 2023-01-25T09:00:26.022529Z

Yeah I hope it gets consolidated now that an official FFI is finally coming to Java.

phronmophobic 2023-01-25T09:02:58.386529Z

dtype next supports graalvm, jna, and java17's java16's flavor of ffi. I don't think dtype supports the newer versions of panama (yet). edit: it looks like it's actually jdk-16's flavor of ffi.

πŸ‘ 2
phronmophobic 2023-01-25T09:06:36.419269Z

I would also like to see these options consolidate. clojure let's you build interfaces that help isolate you from the various implementations, but there's still extra work involved.

⬆️ 1