graalvm

Discuss GraalVM related topics. Use clojure 1.10.2 or newer for all new projects. Contribute to https://github.com/clj-easy/graal-docs and https://github.com/BrunoBonacci/graalvm-clojure. GraalVM slack: https://www.graalvm.org/slack-invitation/.
2020-02-23T00:21:06.003500Z

ah, no, silly me...that's java.library.path not the classpath. the following works here:

$ java -Djava.library.path=. -jar target/clj-rust-0.1.0-SNAPSHOT-standalone.jar 
Hello from Clojure
Hello, from Rust via Java!

2020-02-23T00:24:30.004Z

...and we have a winner:

$ $GRAALVM_HOME/bin/native-image --initialize-at-build-time --no-server --no-fallback -jar target/clj-rust-0.1.0-SNAPSHOT-standalone.jar -H:Name=clojure-rust
[clojure-rust:4219]    classlist:   2,381.60 ms
[clojure-rust:4219]        (cap):     556.81 ms
[clojure-rust:4219]        setup:   1,474.61 ms
[clojure-rust:4219]   (typeflow):   4,127.76 ms
[clojure-rust:4219]    (objects):   3,696.51 ms
[clojure-rust:4219]   (features):     172.49 ms
[clojure-rust:4219]     analysis:   8,292.61 ms
[clojure-rust:4219]     (clinit):     147.99 ms
[clojure-rust:4219]     universe:     402.20 ms
[clojure-rust:4219]      (parse):     419.23 ms
[clojure-rust:4219]     (inline):   1,152.62 ms
[clojure-rust:4219]    (compile):   4,325.21 ms
[clojure-rust:4219]      compile:   6,282.71 ms
[clojure-rust:4219]        image:     617.56 ms
[clojure-rust:4219]        write:     121.88 ms
[clojure-rust:4219]      [total]:  19,781.71 ms

$ time ./clojure-rust 
Hello from Clojure
Hello, from Rust via Java!

real	0m0.002s
user	0m0.002s
sys	0m0.000s
:thumbsup:

Crispin 2020-02-23T03:15:58.004300Z

nice work @borkdude

2020-02-23T09:34:59.005300Z

@retrogradeorbit it looks like your earlier work was helpful in getting this going, if i am not mistaken: https://github.com/retrogradeorbit/graal-native-image-jni ๐Ÿ™‚

borkdude 2020-02-23T09:48:38.008400Z

That looks useful indeed. I was able to figure it out using the README of the Rust jni lib. A gotcha was that loading the lib in the static block wasnโ€™t working so I moved that after I found a comment on the GraalVM github

borkdude 2020-02-23T09:50:15.010Z

Next experiment: see if I can build sci as a native library and call that from Rust so have a scripting lang in Rust

2020-02-23T10:00:41.011100Z

ah, sorry for the misunderstanding ๐Ÿ˜…

2020-02-23T10:02:13.012600Z

nice idea for using sci via rust! perhaps even tree-sitter might eventually be hooked up to be used from jvm / graal using some of these bits.

borkdude 2020-02-23T10:08:04.014700Z

I had seen Crispinโ€™s repo before and I certainly would have reached for it if I would have been stuck

borkdude 2020-02-23T10:09:40.016900Z

Iโ€™m still curious how @retrogradeorbit distributes dynlinked libs along with his native apps, because having them placed in a certain path by the user is a bit weird maybe

Crispin 2020-02-23T10:43:49.017900Z

So, yes. I ended up loading them with clojure.lang.RT/loadLibrary. It was the only way I could get the same code to work in leiningen, in a jar, and as a native binary, and in the test runner.

Crispin 2020-02-23T10:45:17.019100Z

the other thing was bunding the lib in the resources and placing it on disk at runtime, and setting the java librarypath, which on some JVMs you cant reset after startup and have it stick, some you can trick the JVM into reloading, and some you can set!

Crispin 2020-02-23T10:46:16.020Z

luckily graal you can set at runtime. So I detect for it running as a graal native image, and set the value to the place I wrote my library if it is graal

Crispin 2020-02-23T10:48:21.020800Z

I have been planning to make a minimal C clojure example but never got around to it

Crispin 2020-02-23T10:48:37.021Z

so this works to setup native-image: https://github.com/epiccastle/spire/blob/master/src/clj/spire/config.clj#L42-L52

Crispin 2020-02-23T10:49:43.021400Z

this gets leiningen working: https://github.com/epiccastle/spire/blob/master/project.clj#L34-L36

Crispin 2020-02-23T10:50:37.021800Z

and this loads the library, and works across both methods: https://github.com/epiccastle/spire/blob/master/src/clj/spire/core.clj#L33

Crispin 2020-02-23T10:51:04.022300Z

haven't tested the jar build in a while... hopefully I didnt break it

Crispin 2020-02-23T10:52:23.022400Z

I build/copy my compiled lib into project root dir, btw

borkdude 2020-02-23T12:09:41.022900Z

thanks for sharing!

borkdude 2020-02-23T12:20:27.023700Z

> Entry point methods must be static and may only have non-object parameters and return types โ€“ this includes Java primitives, but also Word types (including pointers). Whoops, I guess that makes it a lot harder to make sci useful within a language like Rust, C or Go

zilti 2020-02-23T19:53:08.024500Z

If I'd need an embedded scripting language Clojure still feels like a silly choice; given the option I'd probably embed Chicken Scheme

borkdude 2020-02-23T19:58:08.025Z

why?

zilti 2020-02-23T21:07:10.027200Z

Well, first thing that comes to mind would be the collections. They are fairly complex in Clojure. How do you pass that back and forth between Clojure and Rust in a sane way? Meanwhile Chicken Scheme has straightforward ones, and since it just compiles to C anyway you basically get universally compatible arrays and C vectors which I'm sure Rust can handle out of the box. At the cost of not having immutability of course. For Clojure on the other hand, you'd have to develop an entire companion library.

ghadi 2020-02-23T21:09:28.027700Z

Clojure collections are anything but complex

zilti 2020-02-23T21:14:33.028900Z

Not to you as a user of the language, but the implementation of them. With the shared structure among multiple copies with a shared ancestor, and all that stuff going on

ghadi 2020-02-23T21:21:07.029300Z

it won't be zero copy, for sure

zilti 2020-02-23T21:41:14.030600Z

It just seems kinda silly to shoehorn a language that isn't designed with that in mind into the role of being an embedded language, is all

borkdude 2020-02-23T22:02:01.031100Z

@zilti I can see what you mean yes. When I started thinking about exposing Clojure stuff to Rust, I basically already gave up on the idea ๐Ÿ˜‰

borkdude 2020-02-23T22:02:57.032100Z

btw, bodil implemented the clojure persistent data structures for rust as a rust lib ๐Ÿ™‚

borkdude 2020-02-23T22:03:02.032400Z

just as an aside

zilti 2020-02-23T22:03:09.032600Z

I wonder though if at some point there will be a higher common ground among programming languages. C types seem to still be the smallest common denominator

zilti 2020-02-23T22:03:45.033300Z

Oh, interesting. I know barely anything about Rust. But I've also seen a few libraries for Chicken Scheme that implement Clojure features like atoms

borkdude 2020-02-23T22:04:36.033500Z

https://github.com/bodil/im-rs

borkdude 2020-02-23T22:05:48.034300Z

I also don't know Rust, basically reading through the book this weekend, I just saw this on Twitter a while ago