confs

My first talk ever at a big IT conference was about my production experience with Scala.js. It nicely concluded a full year of extensive Scala.js frontend development, that my team was doing at Evolution Gaming starting from May 2016.

I tried to lay out various pros and cons of using Scala.js in a big browser SPA, based on that real experience. I also tried to go into details and provide examples for most of the points.

Talk took place on 16.05.2017 at Riga Dev Days conference in Riga, Latvia.

IDE support is far from ideal, mostly because of todays scala macro implementation limitations. Coming
Scala.meta to the rescue.

Speaker had a custom IDEA build on his laptop, with a super-awesome “expand macros” feature. Just press a magic shortcut and examine expanded macro code.
Brilliant thing by JetBrains, can’t wait to use it.

Embedding a language in string interpolator

Most of the times you don’t need such things, for example if you use a good ORM. But speaker’s case is valid. He works with two projects each using different and verbose ORM’s to access a single mongo database. So he made a decision to implement a single expressive (and importantly— well-known) language interpreter, that would generate ORM code of choice.

Even though it’s a plain string interpolator, it’s almost compeletely typesafe, thanks to extensive macros usage. Talk covers every step to implement such a thing.

To find root of some performance problems it’s useful to look at bytecode and even assembler code.
Tools here: JMH perfasm profiler, javap

Measurements

1) Pattern matchingSimple ADT match equals in speed to if-clause sequence.
Null-check is much faster then
Option-matching. In general, because of type-erasure, parametrized types pattern matching is slower (but it’s a fair price such feature).

2) Tail recursionBasically is as fast as loops

3) CollectionsFold and map combinators have significant overhead for big arrays, and especially for primitives as elements.
That’s partly because of HotSpot optimization heuristics are shaped for java.

The problem for primitives is boxing. Scala collections are generics, so specialization doesn’t work for them.

There is an alternative collection library, called Debox, which have specialized
Buffer,
Set and
Map .

Conclusion

Scala is slow:

it’s easy to write beautiful, but laggy code

collections are super-slow with primitives

scalac can generate strange code

Scala is fast:

with good internals knowledge, beautiful code can work as fast as java code