Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly does Scala leverage more cores as opposed to Java or other non-functional languages?

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)?

like image 627
hotmeatballsoup Avatar asked Aug 27 '18 16:08

hotmeatballsoup


1 Answers

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.

like image 84
Chaitanya Waikar Avatar answered Sep 30 '22 14:09

Chaitanya Waikar