The Performance Impact of Boxing in Scala
The article explores the performance impact of boxing in Scala and discusses the differences between JVM implementations. The author challenges the common belief that a simple local expression like Box(42).value
incurs significant overhead. They argue that the JVM specification does not dictate the allocation of memory on the physical heap or the use of garbage collection. Instead, these are implementation details that can vary across JVMs. The author conducts a benchmarking experiment using Scala 3.3, OpenJDK, and GraalVM to measure the performance of different code implementations, including one that uses the Box
class. The results show that the performance impact of boxing is not as significant as previously thought, especially with optimizations implemented in GraalVM. The article provides insights for developers who want to understand the trade-offs and performance implications of using boxing in Scala.