Friday, 30 November 2012

Kotlin arrays

Let's kick the CPU today with Kotlin. If you haven't heard about kotlin, it is a relatively young JVM language developed by Jetbrains, the guys behind the popular Idea IDE, Teamcity (still my favourite continuous integration tool) and some other tools.

The obvious advantage of kotlin over other alternate JVM languages is that it does have a good IDE integration. I never really liked the groovy IDE plugins (it must be harder to write good IDE for a dynamic language) and never liked scala's eclipse plugin at all :-( It is always outdated, but never really stable. So kotlin, in it early days of creation already has some advantage over the competition.

I think kotlin language itself borrowed a lot from scala, for example everything is an object (long time debate in the java language), it supports closures and there are even more immutable objects.

One potential problem I have run into is the use of Arrays. In kotlin, you either have to pass an initializer closure to the array constructor (e.g. Array<Int>(1024, {i -> i+1})) or you can choose from the java-interoperability arrays: IntArray, FloatArray, DoubleArray, ByteArray, etc. While the java-compatible arrays are just as quick as in java (since they are compiled into java), the Array objects are rather slow and heavy to create. When I ran into this problem, I wanted to know how much slower are they.

So what you see here is that an IntArray is a lot faster than an Array<Int>, however Array<String> is not much slower than Array<Int> (probably since String does not have to be boxed/unboxed). Beware of autoboxing :-)

One day, when we will have something like findbugs for alternative languages like kotlin, probably Array<Int> will have a serious warning in the performance section.