3 ms·
> this means you never need to scan old generation to collect the new generation Even in HotSpot you don't need to scan the old generation if references in it
by pron 11y ago
> this means you never need to scan old generation to collect the new generation
Even in HotSpot you don't need to scan the old generation if references in it don't change. You only scan (roughly) those old objects who hold references that have been mutated since the last collection.
> data is immutable which means copying can be done in parallel
This is also possible when mutation is allowed (see Red Hat's Shenandoah[1][2], A new GC for HotSpot), and immutability doesn't make the challenge significantly easier (there is added overhead for mutation only).
> mutable references are explicitly marked and isolated which means you could have a very minimal pause to update references... No need to sprinkle write barriers all over the place
Again, mutating reference fields has a special barrier in HotSpot's GCs, but they're not "sprinkled all over the place" -- they're as good as being specially marked.
To summarize, current (and future) HotSpot GCs punish you for mutation, but not for the possibility of mutation; you get all the benefits of immutability automatically when you simply don't mutate references. Immutability -- as you note -- might indeed simplify the GC (though not in the case of Clojure, which does allow quite a lot of mutability -- not everywhere, but enough that you have to account for it not as an outlier), but it doesn't make it faster.
----
> for example Clojure is pretty static for a dynamically typed language and protocols map nicely to traits I wonder if you could somehow remove the dynamic typing and just have compile time type inference figure out the types (requiring explicit types when the system can't infer the types) - this would allow you to send Clojure down to LLVM along with Rust.
A good JIT (like HotSpot) does this a lot better. It is true that Clojure is nowhere near as crazily-dispatched as, say Ruby, but it does rely a lot on Java-like interface-based dynamic dispatch. If your type inference is really, really good, you might be able to identify a great deal of the monomorphic call-sites, but a good JIT finds all of them and makes those calls virtually free, something an AOT compiler can never hope to achieve. Besides, HotSpot's optimizing JIT is a state-of-the-art compiler. You won't do any better with LLVM.
There are approaches that are good for some languages, and others that are good for others. Language's that rely on dynamic dispatch are better off using a JIT than an AOT compiler (actually, pretty much every language would get better code generation with a JIT -- except C which hardly does any dynamic dispatch -- but a JIT has some runtime overheads in warmup, RAM and energy, and languages that don't do too much dynamic dispatch can get excellent machine code generated by an AOT compiler, so that a JIT isn't worth it).
----
In short, while you could (maybe) compile Clojure down to Rust, you'll see no benefit, and probably quite a few disadvantages to that approach. If you want Clojure-Rust interoperability, you can get it today with RustJNI[3].
[1]: http://openjdk.java.net/jeps/189 http://openjdk.java.net/jeps/189
[2]: http://www.jclarity.com/2014/02/19/shenandoah-a-new-low-pause-garbage-collection-algorithm-for-the-java-hotspot-jvm/ http://www.jclarity.com/2014/02/19/shenandoah-a-new-low-paus...
[3]: https://github.com/Monnoroch/RustJni https://github.com/Monnoroch/RustJni
- moonchrome 11y ago>Even in HotSpot you don't need to scan the old generation if references in it don't change. You only scan (roughly) those old objects who hold references that have been mutated since the last collection. >This is also possible when mutation is allowed (see Red Hat's Shenandoah[1][2], A new GC for HotSpot), and immutability doesn't make the challenge significantly easier (there is added overhead for mutation only). You pay for this with barriers and implementation complexity. >To summarize, current (and future) HotSpot GCs punish you for mutation, but not for the possibility of mutation; you get all the benefits of immutability automatically when you simply don't mutate references. Immutability -- as you note -- might indeed simplify the GC (though not in the case of Clojure, which does allow quite a lot of mutability -- not everywhere, but enough that you have to account for it not as an outlier), but it doesn't make it faster. >A good JIT (like HotSpot) does this a lot better. It is true that Clojure is nowhere near as crazily-dispatched as, say Ruby, but it does rely a lot on Java-like interface-based dynamic dispatch. If your type inference is really, really good, you might be able to identify a great deal of the monomorphic call-sites, but a good JIT finds all of them and makes those calls virtually free, something an AOT compiler can never hope to achieve. Besides, HotSpot's optimizing JIT is a state-of-the-art compiler. You won't do any better with LLVM. Rust code doesn't pay for write barriers at all - my point isn't that Clojure on Rust would be faster than Clojure on JVM, it's that Rust code allows much more deterministic performance, better tools for manual optimizations and native interop than JVM and you'd write the performance sensitive code in Rust. Clojure is then a high level tool to consume Rust code. I think you're looking at this the wrong way and missing the obvious benefits - you would get a simple almost pauseless parallel GC and a high level language with trivial native/safe rust interop (which JNI isn't). Because of Rust type system you could even isolate threads that have no access to the shared heap and not collect/pause on those - so no chance at pauses at all. And AoT compilation also gives you deterministic performance. Anyway it would be a lot of work for sure, I doubt it would be a straight port of clojure (would need to make it more static to fit to AOT nicely) and you would have to modify Rust to generate metadata so that the dynamics parts could run (or just ditch the dynamic part completely and force everything to be resolved at compile time with a simplified/constrained type system that would be simpler to infer without explicit typing).
- 11y ago