Kotlin and megamorphic interfaces

This is a question for general knowledge.
I stumbled over this interesting issue in Guava: https://github.com/google/guava/issues/1268
There a smart person complains that the performance of google ImmutableList is much worse then ArrayList. This was due to the fact that there were 3 types of ImmutableList. Empty, Singleton and Regular. Therefore the call sites were converted into megamorphic call sites. They dropped the EmptyImmutableList and then the call sites were converted into Bimorphic sites and the performance improved.

I am interested how kotlin addresses these issues. With kotlin stdlib 99% of the cases we program with interfaces. So how is the issue of megamorphic call sites overcome.

1 Like
  1. No JVM-language can handle this better than JVM.
  2. It does not matter if your call site is effectively monomorphic/bimorphic: The Black Magic of (Java) Method Dispatch
  3. Latest JDK (9+) contains some improvements on devirtualizing interface calls
  4. I suppose KN compiler do some devirtualization at compile time.
  5. JS runtimes are able to solve this problem in a way similar to JVM

I recommend choosing an interface over abstract class whenever possible. You should not have to worry about such things unless you face a real performance problem and carefull benchmarking shows that mega-sites is the issue.

3 Likes