What is Kotlin's impact on JVM performance?

Viewed 617

I'm aware that functional programming/lambdas aren't the best option on the Java world when extreme high performance is the goal. Kotlin address to that issue with the inline keyword.

When Kotlin is compiled and lambdas are inlined, it is actually creating bigger methods and JIT has a hard cap of N bytes to inline to native code. With that in mind, doesn't Kotlin's inline hurts JIT's inlining and therefore affects performance?

Also, I noticed that Kotlin adds A LOT of nullchecks to the compiled code, those checks are really small methods and definitely are inlined by JIT, but due to the amount of calls, couldn't that also be a performance issue?

So, if you are aiming to the highest possible performance, what is Kotlin's impact on JVM?

Side node: I know, I know.. "over optimization is the evil of all roots", "you shouldn't care for performance at this level".

1 Answers

You can override the limit of what inlining value to use for bytecode. See the answer at What is the size of methods that JIT automatically inlines? for more details.

I’m surprised there’s lots of null checks in the bytecode though. Normally Java doesn’t do that and instead lets the CPU handle those automatically and with higher performance.

They may be elided by the JVM’s JIT though so perhaps won’t be an issue once warmed up, but if you want the higher performance you probably want to put your server through dummy data runs to ensure that the JIT is warmed up. There’s nothing worse than a warm JIT hitting a compilation event and having to go through it all again.

If you look out for the JIT compilation events and look out for “hot method” messages not bring compiled or online then you can tweak those methods individually.

Generally though, if you avoid lambda then you’ll find the JVM won’t need to do so much inlining, and worse won’t need to do as much allocating. Lambdas are pretty terrible at accidentally capturing state and causing allocations to occur. Ideally in a high throughput/low latency system you want to avoid as much allocation as possible.

Related