Article
Kotlin @ 60FPS
Is Kotlin slow?
November 20, 2020

With a language this fun to write, is there some cost that must be paid? Will I only realize it when it’s too late?
It’s good to have heuristics for the relatively costly operations in any given language, and to back them up with measurements. Below, we’ll take a look at some samples and the context of execution that makes them interesting. If you’re writing Kotlin and have a concern for performance in some regime, your expectations may be inverted.

Firstly, it is worth noting that the vast majority of code is not on a hot code path where performance concerns override the maintainability, readability, and semantic joy in everyday Kotlin. Performance often isn’t a concern, right up until it’s the only concern.
So what’s a “hot” code path?
Hot code paths get executed near continuously, or represent a computationally expensive task that requires oodles of iterations. Even minuscule differences in efficiency inside a hot code path can quickly add up to performance gains or losses.
Examples of hot code paths include:
- UI applications with a draw() loop. Keeping a UI thread unblocked and responsive is important, and updating visible state is typically a 60fps job.
- Simulation State: When simulating, often states are computed from previous states. Games are sometimes CPU-bound for this reason.
- Light post-processing on images or video. A custom GPU shader can speed this up, but it’s nice not to have to support the openGL ecosystem if you don’t have to.
Or countless other domain specific cases. Perhaps you have a scheduled job on a back-end service and you’d like to keep it lean, or a background task on a mobile device that should complete as soon as possible, to avoid being killed by the user or OS.
Hold up. The Garbage Collector Is A Deal-Breaker, Right?
There’s some dated conventional wisdom around garbage collected languages, so first we’re going to write a few small tests to check assumptions.
Allocations.So the story goes, creating objects is the most expensive thing you can do. It’s not costless, and we’ll see that in a second, but first a quick test: