Upgrade to Pro — share decks privately, control downloads, hide ads and more …

5 things you need to know about Scala compilation

Triplequote
December 14, 2017

5 things you need to know about Scala compilation

You love Scala, but the one aspect you have a hard time accepting are long compile times.

It’s not uncommon for a project to experience compilation times of a handful of minutes, if not worse. On top of that, compilation times are unpredictable, depending on a combination of language features, external libraries, and type annotations. A single line change may increase compilation times ten fold.

Don’t bow to the compiler and accept long compilation times! Rather, join this talk to learn how the compiler works and optimize your usage of Scala to compile faster!

Triplequote

December 14, 2017
Tweet

More Decks by Triplequote

Other Decks in Technology

Transcript

  1. #1 Build time ≠ Scala compilation time Sbt has a

    cost! • Before even starting compilation: resolution, source generators, settings evaluation, etc. • What do we need to compile? Hash classpath, hash sources (a LOT of I/O) • Zinc and incremental compiler: 15% overhead
  2. #1 Build time ≠ Scala compilation time Sbt slows you

    down! • Before evan starting compilation: resolution, source generators, settings evaluation, etc. • What do we need to compile? Hash classpath, hash sources (a LOT of I/O) • Zinc and incremental compiler: 15% overhead Use the IDE builder (or bloop).
  3. #2 Typeclasses are recomputed more than you think! Do you

    have import shapeless._ in your sources? • Imports take precedence over companion class ◦ Familiarize yourself with the implicit scope definition • ..Or be ready to recompute typeclass derivation on each use! ◦ Cost: implicit resolution ◦ Cost: additional code that needs to be compiled and generated
  4. #2 Typeclasses are recomputed more than you think! Do you

    have import shapeless._ in your sources? • Imports take precedence over companion class ◦ Familiarize yourself with the implicit scope definition • ..Or be ready to recompute typeclass derivation on each use! ◦ Cost: implicit resolution ◦ Cost: additional code that needs to be compiled and generated
  5. #3 Whitebox macros are type-checked 3x! What is the difference

    between whitebox and blackbox? Which one takes part in type inference?
  6. #3 Whitebox macros are type-checked 3x! What is the difference

    between whitebox and blackbox? Which one takes part in type inference?
  7. #3 Whitebox macros are type-checked 3x! Shapeless macros are whitebox

    What is the difference between whitebox and blackbox? Which one takes part in type inference?
  8. #4 Type-checking should be around 30% • Type-checking Scala sources

    takes time • ..but should not go above 30% of total compilation time! ◦ You can use -verbose to see times for each phase (and a lot of noise) • If more, you can bet your money on one (or more) of: ◦ Macro expansion ◦ Implicit resolution ◦ Quasiquotes, type tags, string interpolation (macros in disguise)
  9. #5 Scala compiler is single-threaded • It’s a batch compiler

    • Careful interplay of laziness and mutability ◦ Laziness required for recursive types ◦ Mutability for performance • Bigger/more powerful machines won’t make a dent in compilation times ◦ Unless you have a parallel Scala compiler! ◦ Hint: there is one that works today!
  10. #6 More sources is better than few sources • The

    Scala compiler works at the granularity of the source file • Split sources that takes a lot to compile into multiple sources • sbt/zinc does a full recompile if 50%+ of the sources are invalidated ◦ By having more sources you are lowering the chances that a full recompile is performed! • Learn more about incremental compilation: http://www.scala-sbt.org/1.x/docs/Understanding-Recompilation.html