Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin for game dev

Background:

I'm always searching for a language to replace Java for game development. Kotlin looks promising with a good IDE support and Java interop. But one of the FPS killers for a game (on Android especially) is GC usage. So, some libraries (like libgdx) are using pools of objects, custom collections and other tricks to avoid frequent GC run. For Java that can be done in a clear way. Some other JVM languages espesially with functional support using a lot of GC by it's nature, so it is hard to avoid.

Questions:

  1. Does Kotlin creates any invisible GC overhead in comparison to Java?
  2. Which features of Kotlin is better to avoid to have less GC work?
like image 992
alex.dorokhov Avatar asked Dec 14 '22 13:12

alex.dorokhov


2 Answers

You can write Kotlin Code for the JVM which causes the same allocations than the Java corresponding logic. In both cases you have to carefully check if a library call allocates new memory on the heap, or not. Using Kotlin in combination with LibGDX doesn't introduce any invisible GC overhead. It's an effective way and works well (especially with the ktx extension.

But there are Kotlin language features which may help you to write your code with fewer allocations.

  1. Singletons are a language feature. (Object declarations, companion object )

  2. You can create wrapper classes for primitive types which compile to primitives. But you get the power of type safety and rich domain models (Inline classes).

  3. With the combination of Operator overloading and Inline Functions you can build nice APIs which modify objects without allocating new ones. (Example: Allocation-free Vectorial operations using custom operators)

  4. If you use any kind of dependency injection mechanism or object pooling to connect your game logic and reuse objects, then Reified type parameters may help to use it in a very elegant an short way. You can skip a class as type parameter, if the compiler knows the actual type.

But there is also another option which indeed gives you a different behavior in memory management. Thanks to Kotlin Multiplatform, you can write your game logic as Kotlin common module and cross compile it to native code or to Javascript.
I did this in a sample Game project Candy Crush Clone. It works with Korge a Modern Multiplatform Game Engine for Kotlin. The game runs on the JVM, as HTML web app and as Native binary in Win, Linux, Mac, Android or IOS.
The native compiled code has its own simpler garbage collection and can run faster. So the speed-increase and the different memory management may give you the power reserve to bother even less with the GC.

In conclusion I can recommend Kotlin for Game dev, also for GC critical scenarios. In my projects I tend to create more classes and allocate more memory when I write Kotlin code. But this is a question of programming style, not a technical one.

like image 128
Tobse Avatar answered Dec 29 '22 00:12

Tobse


As a rule of thumb, Kotlin generates bytecode as close as possible to the one generated by Java. So, for example, if you use a function as a value, an inner class will be created, like in Java, but no more. There are also some optimization tricks like IntArray and inline to perform even better.

And as @Peter-Lawrey said, it's always a better idea to measure the values for your specific case.

like image 28
voddan Avatar answered Dec 28 '22 23:12

voddan