Skip to content

harpocrates/scala-java-records-benchmark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Benchmark to compare equals on Java records to Scala case classes.

  • Java uses invokedynamic with java.lang.runtime.ObjectMethods
  • Scala generates bytecode directly in equals

With JDK 16, sbt jmh:run:

...
[info] Bench.largeJavaEqualityTest     avgt  250   9.689 ± 0.140  ns/op
[info] Bench.largeJavaInequalityTest   avgt  250   2.949 ± 0.111  ns/op
[info] Bench.largeScalaEqualityTest    avgt  250  31.844 ± 0.352  ns/op
[info] Bench.largeScalaInequalityTest  avgt  250   7.203 ± 0.065  ns/op
[info] Bench.smallJavaEqualityTest     avgt  250   3.856 ± 0.043  ns/op
[info] Bench.smallJavaInequalityTest   avgt  250   3.234 ± 0.219  ns/op
[info] Bench.smallScalaEqualityTest    avgt  250   7.307 ± 0.112  ns/op
[info] Bench.smallScalaInequalityTest  avgt  250   3.473 ± 0.030  ns/op

Conclusion: Java wins in every dimension

  • it is faster

  • the bytecode for equals is much shorter (especially when you account for the fact the bootstrap method is re-used for hashCode and toString)

  • the implementation of ObjectMethods is flexible, so can be improved in future JVMs without the bytecode invokedynamic call needing to change.

About

Compare Scala case class to Java record

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published