Optimizing Scala bytecode for fun and profit
Session Abstract
Can high-level code have the same perf as low-level hand-tuned code?
That’s what Scala’s optimizer is for! Your call to map on a list turns into the same bytecode as if you had written a while loop.
In this talk, I’ll introduce bytecode and optimization basics, present the Scala optimizer, and discuss fundamental and temporary limitations.
Session Description
Every developer wants to write high-level declarative code that’s easier to maintain while also getting the performance benefits of low-level imperative code that doesn’t pay the costs of abstraction.
The Scala compiler has a bytecode optimizer to help reach that goal, and the JVM also performs its own optimizations.
However, there are plenty of interesting problems and tradeoffs along the way. For instance, while inlining code can help avoid virtual calls and allocations, it can also cause slowdowns from having too much code to load and keep in the instruction cache. Even if a specific optimization is profitable, it may rely on such a specific code shape that it is too brittle to be depended upon.
Optimization has its limits, and the JVM adds its own limitations on top of those, which aren’t the same as in other targets such as Scala.js.
In this talk, I’ll discuss:
- what bytecode is, and how the JVM interprets and compiles it;
- a high-level view of optimizations, including inlining, store-load elimination, and so on;
- how the optimizer is implemented, and what consequences this has on the optimizations it performs;
- what developers can and cannot expect from the Scala compiler and the JVM; and
- how the Scala optimizer could improve in the future.