7

I was listening to a Martin Odersky video recently where he attempts to explain the fundamental advantage of functional languages (such as Scala, but of course not necessarily Scala) over OOP or procedural languages.

To paraphrase, he explains that Moore's Law is failing us lately, and so to make processors "faster", instead of being able to double the number of transistors in the cores, CPU manufacturers are simply offering more cores. This in turn lends the CPU to being leveraged more fully by concurrent/multi-threaded applications. So that primary take away was: the more concurrent the application the more snippets of its code are running simultaneously on different cores, and with more cores on the CPU that overall faster the program executes.

So far, so good.

What he failed to explain (or more likely, what I failed to grasp) is why functional languages like Scala lend themselves to being more concurrent then other non-functional languages. Since we happen to be talking about the JVM space, let's do a quick comparison of Java and Scala. What is it about a Scala program that, if it were instead implemented in pure Java, would make it more difficult to parallelize/make concurrent, especially if its all compiling down to JVM bytecode and running on the same JVMs with the same native capabilities??

Meaning I have two JVM apps, App1 written in Java and App2 written in Scala. They both do the exact same tasks and accomplish the same things. Both get compiled down to JVM bytecode and are ran on the same computer with the same JVM installed on it. How is the Scala app able to "tap into" JVM capabilities that make it more suited for concurrency than the Java one (and thus, faster on a CPU with more cores on it)?

1
  • 3
    It doesn't "leverage more cores", and it's not "more concurrent" from a runtime perspective. It's just easier to write concurrent apps in Scala than in Java as functional approach requires less experience and work to achieve same results (in fact Scala can be slower due to immutability overhead). Commented Aug 27, 2018 at 17:46

2 Answers 2

7

You are absolutely correct when you say that both of your apps ultimately boil down to byte code on the JVM. Most of the libraries and frameworks available to Scala for concurrency are also available to Java. However, the thing that sets Scala apart is Scala’s built in support for immutability. Scala has a very rich immutable collections and this feature of embracing immutability makes it easier to write code for concurrent and parallel applications. Scala abstracts the user from writing thread level code and lets the user focus more on the business logic. The Futures API comes in very handy during this.

Scala embraces the functional programming paradigm (i.e. composing functions) and isolating mutable state within actors, by which you can eliminate all of the typical concurrency problems that you would run into by, e.g. programming threads in Java. This is why Scala was chosen as the language to design Spark.

Now coming to main part of your question as to how scala leverages more cores as compared to other non functional languages :- The Futures API in scala has an underlying execution context which is nothing but a thread pool which is configurable by a developer. Using this API you will be able to take advantage of all your cores.

5
  • Thanks @Chaitanya Waikar (+1) so it sounds like you're saying that there's nothing inherently different about Scala that allows it to run faster than a standard Java application, yes? Thanks again! Commented Aug 27, 2018 at 17:05
  • 1
    @hotmeatballsoup Scala embraces the functional programming paradigm (i.e. composing functions) and isolating mutable state within actors, you can eliminate all of the typical concurrency problems that you would run into by, e.g. programming threads in Java. This is why Scala was the language used to design Spark.
    – Chaitanya
    Commented Aug 27, 2018 at 17:07
  • Thanks again @Chaitanya Waikar (+1), but just to be clear: there's nothing inherently different about Scala that allows it to run faster than a standard Java application, right? Commented Aug 27, 2018 at 17:14
  • 2
    @hotmeatballsoup I do not have the numbers to comment about the execution times of a Java and a Scala application. However, if you sit down to write the same code to achieve parallelism and concurrency, it becomes very tricky for the developer to write it in Java as compared to Scala.
    – Chaitanya
    Commented Aug 27, 2018 at 17:22
  • 3
    @hotmeatballsoup you're missing the point. Odersky isn't claiming that Scala code is faster than the equivalent Java code, that would be ridiculous. He's making the far more tenable claim that parallel code is faster than sequential code (not very controversial) and that its easier to write parallel code in functional languages. Ergo, Scala is faster than Java because immutability and better concurrency constructs make it much easier to write fast code, not because the equivalent code is faster. Commented Aug 27, 2018 at 21:39
0

I want to add more to what is already mentioned. I believe, in general, Java applications are faster but negligible than Scala applications. However, fast execution speed alone is not enough. For example,

Can we develop Scala applications faster than Java? The point is, even if the Scala applications are slower by 10~15% (for example) in term of execution speed but the development time is ~30% faster using FP best practices then it is worth to use Scala (or other Functional Languages). FP emphasizes a lot of functions composability and type safety. Another trait of FP is to use values (immutable variables) over variables. Immutability makes multicore programming easier to manage and less error-prone. Java can do the same using some of the FP best practices. So, during development, Java programmers will encounter a lot of inconveniences because these practices are not readily available in Java.

Does compiling Java sources faster than Scala? Well, this is a big yes. Scala compiler checks whole lots of things, e.g. implicit functions, during compilation. However, Scala programmers write lesser unit tests because the compiler checks the validity of the functions' parameters and return types. The compiler will break if the types are not aligned properly.

The argument where both Java and Scala applications are compiled into JVM bytecode and executed under the same JVM, therefore, they should have the same capabilities is wrong. Both applications are limited by the JVM capabilities unless one of them uses the undocumented JVM features. Then, that language has an "unfair" advantage. Discounting that, the only way one language is faster than the other, given the same JVM runtime environment, is to arrange the bytecode more efficiently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.