Why do Scala maps have poor performance relative to Java?
- by Mike Hanafey
I am working on a Scala app that consumes large amounts of CPU time, so performance matters. The prototype of the system was written in Python, and performance was unacceptable.
The application does a lot with inserting and manipulating data in maps. Rex Kerr's Thyme was used to look at the performance of updating and retrieving data from maps. Basically "n" random Ints were stored in maps, and retrieved from the maps, with the time relative to java.util.HashMap used as a reference.
The full results for a range of "n" are here.
Sample (n=100,000) performance relative to java, smaller is worse:
Update Read
Mutable 16.06% 76.51%
Immutable 31.30% 20.68%
I do not understand why the scala immutable map beats the scala mutable map in update performance. Using the sizeHint on the mutable map does not help (it appears to be ignored in the tested implementation, 2.10.3).
Even more surprisingly the immutable read performance is worse than the mutable read performance, more significantly so with larger maps.
The update performance of the scala mutable map is surprisingly bad, relative to both scala immutable and plain Java.
What is the explanation?