Hey everyone. I found myself in various discussion...
# compiler
j
Hey everyone. I found myself in various discussions about the performance of Kotlin vs Java recently. Typically, the argument goes along the lines that since Kotlin generally uses older bytecode features, it is less performant than current Java versions, say Java 11. Now I have a hard time finding current information on 1) which modern bytecode features Kotlin does use and 2) how Kotlin's performance compares to Java's. Without digging through the compiler source code, can anyone point me to resources that provide an overview of which bytecode features Kotlin uses to implement certain features? Potentially also (current, Java 11+) resources that compare the performance between the two? Or, in other words, which benefits do I get (if any) by increasing the Java target version to anything above 8 when compiling Kotlin code?
m
Hi Johann! While I don't have specific numbers for you, I have found this argument about performance between languages is often misguided. Performance is highly dependent on a number of factors and language performance is usually not a concern. The overall design of a system is what folks should be looking at when talking about performance. The only reason I could see caring about performance with the versions of byte code or the languages used is if you were doing something like high-speed trading. Even then, there would be arguments about making specific optimizations around a C or C++ program (although the JVM's performance overall (even with v 8 bytecode) is pretty solid and in many cases is faster than what most C++ programmers can put together). The only reason where performance at this level makes any sense is if you're doing some kind of low-level hardware ops for a real-time system where the absolute fastest performance is necessary. This is not the case for 99% of programmers. Whenever I see people make a general sweeping statement to "not use language Y because language X has faster byte code or runtime" or some nonsense, it usually indicates an unfounded bias. If there's a specific use case they can come up with for why it's performance is not sufficient for a specific use case, then sure, if that's a concern for you, maybe think it over. However, I would rather focus on using a language that makes complex logic more easily abstracted than just using one because it might shave a millisecond off the runtime. This reminds me of video gamers who argue that you don't want to buy X hardware because Y hardware will get you another 20 frames per second. Never mind the fact that X hardware is already cranking out 200 frames per second, and adding another 20 frames doesn't matter. For some projects, I use Kotlin because it fits my needs. For others I use Scala. Sometimes I code things in C++ or even C because it makes sense for that scenario. At other times I enjoy using Haskell (though usually not for anything outside pet projects). If there's one thing I'd like to see Kotlin improve performance-wise it'd be more with the compiler. Faster compile times means I can see what's working with my project vs not that much quicker (especially for projects that are very large, which I happen to work on). But that's something JetBrains is already improving in 1.4.
j
Hey Matt, thanks for your reply! I am completely on-board with nearly all the things you wrote. I especially agree with your point that for most applications it doesn't really matter, even if Kotlin is slightly slower (or faster) than Java. You named high-speed trading as performance-sensitive application, where especially latency is the important variable. My use case is also performance-sensitive but in terms of throughput, not latency. It uses 100% of the available processing power and a lot of memory, not because the design is bad but because task is quite complex. Let's say one run of the program takes 10 minutes with Java and Kotlin takes 10% more time, then we'll be at 11 minutes. Not great, but not catastrophic. But in cases where one run takes, say, 20 hours then 2 hours is quite a bit more in absolute terms, and I would feel it. Same goes for memory, if 10% more memory was to be used, it would be a relatively large argument against Kotlin, if it uses 44 GB instead of 40. (Now I'm not saying that Kotlin is necessarily this much more inefficient, just trying to make a point). My main concern here is that Kotlin uses relatively old bytecode versions, along with their old feature sets, for some very similar features that Java has also introduced natively in the bytecode in the meantime (e.g. lambdas). This is likely going to get more as time goes on. But then - is Kotlin really less performant through-put wise than Java? I guess, in reality, it depends and is really tough to say. I just haven't really seen many efforts to investigate this. Unfortunately, sometimes it can also be really difficult to impossible to just try it out, hence I am currently trying to gather as much information, as I can. After writing my original post if found https://youtrack.jetbrains.com/issue/KT-35625#focus=streamItem-27-3880698.0-0 so I guess these potential concerns are on the roadmap anyway. Not tomorrow but the Kotlin devs are aware. It doesn't answer the question if Kotlin, right now, is significantly less performant than Java right now, but it suggests that in case that it currently is, it won't stay like that. Also, totally agree with you on the time it takes to compile 🙂