Convert scala to native binary

Viewed 2120

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

2 Answers

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/

Related