Convert scala to native binary
Asked Answered
L

2

6

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

Levy answered 23/3, 2016 at 6:34 Comment(3)
Gcj seems to be dead. Apparently there is only commercial solution that is up-to-date.Schoolmate
Last time I tried gcj, it did work without any draw-backs. It was a veery long time ago though, like 2010, and I think I used it for java1.7 code.Flunk
To comment with the contents (questionably?) deleted answers: scala native is making some recent progress on this front.Militant
C
6

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

Ceiba answered 23/3, 2016 at 7:13 Comment(5)
bytecode to native conversion is concidered in order to gain performance, any idea about the performance of above two solutionsLevy
I haven't got any kind of benchmarks but I think that these solutions could only decrease startup time comparing to Oracle JDK, but you will not get any significant performance boost for running code. These solutions could be even slower than warmed-up Oracle Hotspot. If you have performance issues with scala/java - use profiler to find the root cause. Compilation to native code will not "magically" increase the performance.Parturient
A good part of the newer 64-bit Excelsior JET compiler is written in Scala, and comes natively compiled with itself, so compatibility is in place. As far as performance goes, no benchmark will tell you whether your particular code will be faster or slower if you switch to an alternative Java implementation.Cerebrum
Recently Scala Native was announced, but it will be a long time until it will be stable: scala-native.orgStab
Their website says Excelsior JET has been discontinued.Birdiebirdlike
P
6

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/

Pica answered 23/1, 2018 at 0:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.