Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert scala to native binary

Tags:

scala

native

gcj

What is the best way to convert scala (or bytecode) to native binary in order to increase performance

like image 670
Rimash Mohomed Avatar asked Mar 23 '16 06:03

Rimash Mohomed


2 Answers

At this moment I see two solutions to convert jvm bytecode to sort of self-contained native binary:

  • Avian - lightweight embeddable JVM with AOT features
  • Excelsior JET - Commercial Java native compiler

Both should be compatible with Scala.

There are no direct native compilers for scala as I know. There some projects like Scala LLVM, but they are more about research and proof of concepts than ready to use tools

like image 190

Although not a fully capable tool yet, the scala-native project is starting to become usable, though it's still at an early stage, it's under active development and is becoming more capable by the month. It's based on LLVM and clang, and will compile your scala sources to binaries, if the libraries you depend on are among those implemented at this early stage. (it's not yet working in Windows or cygwin, although it does work in the WSL environment). Update: Windows support has been improving recently (fall 2021).

Whether performance is increased or not is a separate question, although most programs are likely to start up much more quickly.

Here's a link to the User's Guide

To create your own project: Minimal sbt project

The biggest limitations are that only a subset of java and scala standard libraries have been implemented so far, so you'll need to limit yourself to what's currently available, and not every project will only be feasible if you restrict yourself to 100% scala. Also, the documentation is a work in progress.

As a test, I created a command line tool for processing text files, and I was able to get it to work finally, although I did spend a bit of time figuring out how to accomplish various things, and mostly how to live with the available libraries. If necessary, you can also link to C/C++ libraries although I didn't need to for my small project.

Footnote as of June 2019: I'm having good luck with graalvm native-image. Here's the link:

https://www.graalvm.org/docs/reference-manual/aot-compilation/

like image 45
philwalk Avatar answered Oct 16 '22 13:10

philwalk