Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of Scala for Android

I just started learning Scala, and I'm having the time of my life. Today I also just heard about Scala for Android, and I'm totally hooked.

However, considering that Scala is kind of like a superset of Java in that it is Java++ (I mean Java with more stuff included) I am wondering exactly how the Scala code would work in Android?

Also, would the performance of an Android application be impacted if written with Scala? That is, if there's any extra work needed to interpret Scala code.

like image 447
SamAko Avatar asked Sep 30 '13 11:09

SamAko


People also ask

Can Scala be used for Android?

Introduction. The Android platform runs on Android Runtime which is a virtual machine based on JVM and, although not identical, it's very similar to it. As a consequence, it is possible to write Android apps in Scala, and in fact it's possible to do it in more than one way.

Is Scala fast?

There was a study that Google did a number of years ago where they compared C++, Java, Scala, and Go. The study concluded that Scala was faster than Java and Go when average developers write their code without thinking about optimization too much. The study used the default, idiomatic data structures in each language.

Is kotlin better than Scala?

It's true that both languages are known for their functional programming paradigm. But, when it comes to looking into Scala vs Kotlin comparison in terms of functional programming, the former wins. Scala is more swayed by functional programming languages like Haskell than Kotlin.

Is Scala used for app development?

Scala, a statically typed language running atop the Java Virtual Machine, is emerging as a development option for building Google Android applications.


1 Answers

To explain a little bit @Aneesh answer -- Yes, there is no extra work to interpret Scala bytecode, because it is exactly the same bits and pieces as Java bytecode.

Enter image description here

Note that there is also a Java bytecode => Dalvik bytecode step when you run code in Android.

But using the same bricks one can build a bikeshed and the other guy can build a townhall. For example, due to the fact that language encourages immutability Scala generates a lot of short living objects. For mature JVMs like HotSpot it is not a big deal for about a decade. But for Dalvik it is a problem (prior to recent versions object pooling and tight reuse of already created objects was one of the most common performance tips even for Java).

Next, writing val isn't the same as writing final Foo bar = .... Internally, this code is represented as a field + getter (unless you prefix val with private [this] which will be translated to the usual final field). var is translated into field + getter + setter. Why is it important?

Old versions of Android (prior to 2.2) had no JIT at all, so this turns out to about 3x-7x penalty compared to the direct field access. And finally, while Google instructs you to avoid inner classes and prefer packages instead, Scala will create a lot of inner classes even if you don't write so. Consider this code:

object Foo extends App {
    List(1,2,3,4)
      .map(x => x * 2)
      .filter(x => x % 3 == 0)
      .foreach(print)
}

How many inner classes will be created? You could say none, but if you run scalac you will see:

Foo$$anonfun$1.class       // Map
Foo$$anonfun$2.class       // Filter
Foo$$anonfun$3.class       // Foreach
Foo$.class                 // Companion object class, because you've used `object`
Foo$delayedInit$body.class // Delayed init functionality that is used by App trait
Foo.class                  // Actual class

So there will be some penalty, especially if you write idiomatic Scala code with a lot of immutability and syntax sugar. The thing is that it highly depends on your deployment (do you target newer devices?) and actual code patterns (you always can go back to Java or at least write less idiomatic code in performance critical spots) and some of the problems mentioned there will be addressed by language itself (the last one) in the next versions.

Original picture source was Wikipedia.

See also Stack Overflow question Is using Scala on Android worth it? Is there a lot of overhead? Problems? for possible problems that you may encounter during development.

like image 161
om-nom-nom Avatar answered Oct 12 '22 09:10

om-nom-nom