3 ms·
Why would you compile Scala natively itself instead of compiling JVM bytecode?
by nerdponx 9y ago
Why would you compile Scala natively itself instead of compiling JVM bytecode?
- densh 9y agoJVM startup is really painful for short to medium time running apps, this is the niche we’re trying to address.
- nerdponx 9y agoI meant, "compile bytecode to machine code." The fact that nobody does it is a sign that it's not a good idea. I guess I was just wondering why.
- densh 9y agoScala is a relatively simple language in the end of the compiler pipeline. We just take lowered ASTs and emit our SSA representation from there because it’s easy it to implement it this way. You can totally do something similar from the bytecode.
- rpeden 9y agoExcelsior JET does it, and it works Scala. So you can already compile JVM targeted Scala to native code that way if you want to. JET is fairly expensive, though.
- pjmlp 9y agoLots of people do it. Besides ExcelsiorJET named in a sibling answer, the JDKs from Aicas, IBM, PTC, STMicroelectronics, Google with their ART implementation and a few smaller vendors for the embedded market. Also Oracle just added initial AOT on Java 9, with plans to extend it to other platforms, and eventually (long term plan) rewrite everything in Java as part of Project Metropolis. Oracle also had AOT compilers before Sun's acquisition, like JServe and JRockit. Why does it seem no one does it? Sun was religiously against AOT, leaving it to third party commercial vendors. And since the rise of FOSS most developers think it is outrageous to pay for compilers, only Fortune 500s buy such compilers.
- nerdponx 9y agoAre they all proprietary/closed-source? When I was first learning about Clojure I really wanted a native JVM compiler, but as a hobbyist I was not about to go all-in on an enterprise compiler. The only FOSS solution I found was GCJ. That, and "the JVM startup is slow!" seems to be a popular argument against Clojure and Scala here on the Intertubes.
- pacala 9y agoHow about generating .so, callable from other environments, for example CPython?
- densh 9y agoThis is on our roadmap, stay tuned.
- binarynate 9y agoInteresting question - I hadn't thought about compiling the JVM bytecode to machine code. It looks like there is a Java frontend for LLVM that achieves that: https://llvm.org/svn/llvm-project/java/trunk/docs/java-frontend.txt https://llvm.org/svn/llvm-project/java/trunk/docs/java-front... One of the things that appeals to me about Scala Native is its interoperability with C, which I suspect is something that the LLVM Java frontend doesn't provide.
- nerdponx 9y agoDoesn't the JVM have some kind of native FFI?
- binarynate 9y agoI suspect the built-in Java Native Interface (JNI) won't work when compiling bytecode to native code, since the JNI methods require a pointer to the JVM (passing a pointer to the JVM to C functions or calling methods on the JVM class in C++). Perhaps an FFI library like this could work, though: https://github.com/jnr/jnr-ffi https://github.com/jnr/jnr-ffi .
- dmytrish 9y agoScala may be written in functional style, which is a poor match for JVM (e.g.: uglyness of `@tailrec` annotations, absence of mutually-recursive functions, JVMs GCs are not usually optimized for that many ephemeral objects, etc). Native code is not much better as a compilation target for FP, but at least you don't have to fight through JVM constraints first (GHC compiles to pretty efficient native code).