Fork me on GitHub
#graalvm
<
2020-02-23
>
sogaiu00:02:06

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!

sogaiu00:02:30

...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:

sogaiu09:02:59

@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 🙂

borkdude09:02:38

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

borkdude09:02:15

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

sogaiu10:02:41

ah, sorry for the misunderstanding 😅

sogaiu10:02:13

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.

borkdude10:02:04

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

borkdude10:02:40

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

Crispin10:02:49

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.

Crispin10:02:17

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!

Crispin10:02:16

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

Crispin10:02:21

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

Crispin10:02:23

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

Crispin10:02:04

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

borkdude12:02:41

thanks for sharing!

borkdude12:02:27

> 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

zilti19:02:08

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

zilti21:02:10

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.

ghadi21:02:28

Clojure collections are anything but complex

zilti21:02:33

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

ghadi21:02:07

it won't be zero copy, for sure

zilti21:02:14

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

borkdude22:02:01

@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 😉

borkdude22:02:57

btw, bodil implemented the clojure persistent data structures for rust as a rust lib 🙂

borkdude22:02:02

just as an aside

zilti22:02:09

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

zilti22:02:45

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

borkdude22:02:48

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

kulminaator07:02:13

i have played with rust for about 20 hours in total. actually it is nice, the base ideas are very sane.

kulminaator07:02:31

but it is very verbose, so i wouldn't use it unless i had a real low level target to achieve.