r/Clojure • • Jun 11 '19

GitHub - timothypratley/rustly: A Clojure to Rust transpiler

https://github.com/timothypratley/rustly
73 Upvotes

15 comments sorted by

11

u/dustingetz Jun 11 '19

This is a really good idea

6

u/vvvvalvalval Jun 11 '19

What's the strategy for memory management?

3

u/dustingetz Jun 12 '19

I always thought a prerequisite to immutable collections was a strong gc but i wonder how these work https://github.com/bodil/im-rs

10

u/halgari Jun 12 '19

They work just fine, but the problem comes with CAS contention. A atomic increment can take about 300 cycles on modern hardware and it flushes that cache line from some caches. With a RC system you’d end up doing a inc/dec on every read to every level of the persistent tree while you’re reading it. Rust supports this via the Arc reference type, but we’re still talking about a thousand cycles wasted just on bookkeeping while reading from a hashmap.

There are optimizations for all of this, but it’s true, the cost of persistent structures is much higher in a non GC context

1

u/therealdivs1210 Jun 11 '19

I think it'll end up using ref-counting.

5

u/[deleted] Jun 12 '19

Wondering, with Graalvm becoming closer to being an option for Clojure applictions, what features do you get with rust over compiling down to a native binary?

2

u/dustingetz Jun 12 '19

I was thinking Rust interop, i think that would mean you can have ClojureScript like semantics with platform primitives to Rust which has a much simpler memory model than C, I don't think C could work for Clojure https://pcwalton.github.io/2013/03/18/an-overview-of-memory-management-in-rust.html (but you could hit C from Rust)

11

u/halgari Jun 11 '19

There's not a whole lot implemented, which makes it hard to know what the final design approach will be. What's not clear here is how much of Clojure will be usable via this approach. It should be possible to get a CLJS-like experience through simple transpilation. But it's yet to be seen how things like collections, equality, memory management, etc. will be handled. And traditionally Clojure leans really hard on optimizing JITs so it'll be interesting to see how this performs in a statically compiled context.

0

u/[deleted] Jun 11 '19

[deleted]

4

u/halgari Jun 11 '19

Sure, but that will result in really bad performance especially if multi-threaded support is required. None of this is impossible, but there is a reason it hasn't been done before.

2

u/therealdivs1210 Jun 12 '19

Apologies, I meant to reply to another comment.

I understand that it won't be as performant, but this is probably the easiest way out and thus there's a high chance of it happening. The Unity Game Engine has been using a 2008 Boehm GC (!!!) for a long time since that's what Mono was using at that time.

I stumbled upon some interesting stuff thanks to this post:

5

u/jimmyhmiller Jun 11 '19

Definitely early days, but exciting. But I'm partial because it is using meander.

I wonder if rather than using strings it might be worth rewriting to an ast representation. I know rust has a json ast format. So you could turn it into normal clojure data structures, serialize to json, and then do code gen. Might be a bit more complicated, but perhaps more flexible.

4

u/[deleted] Jun 11 '19

The goal is to cover all the good parts.

Is this something defined by the community somewhere?

5

u/agumonkey Jun 11 '19

Lovely :)