4

In Kotlin, how costly is the casting of classes?

So for instance, let's have the following Test class

open class Test {
    open fun question() = "Basic question"
}

and 3 inheriting classes

class MathTest : Test() {
    override fun question() = "2+2=?"
}

class EnglishTest : Test() {
    override fun question() = "Who created SO?"
}

class HistoryTest: Test() {
    override fun question() = "When was SO created?"
}

How costly would it be to cast (for instance, let's say) 100 Test objects to either one of these 3, at runtime, in Android (and in general) ?

SnuKies
  • 1,578
  • 1
  • 16
  • 37
  • 1
    Not more than with Java. – Zoe Sep 12 '18 at 13:49
  • 1
    near to nothing. https://stackoverflow.com/a/26336788/4265739 – leonardkraemer Sep 12 '18 at 14:08
  • if I look at the documentation, casting could be possible for all: kotlin-js, kotlin-native and kotlin-jvm... I am only using kotlin-jvm and there a cast translates to the same as in Java (and for casting there exist already some microbenchmarks). Regarding kotlin-js and kotlin-native? Don't know how a cast is working there or whether it is even supported... would be nice if someone else could tell... – Roland Sep 12 '18 at 14:46

1 Answers1

2

I messed around a little bit with disassembling the generated bytecode, and except for one case, casting is identical to Java. The one case where it seems to be different is when using the safe cast operator, as?, like so:

val thing = "" as? Int

This generates equivalent bytecode to this Java code:

String _temp = "";
if (!(_temp instanceof Integer)) {
    _temp = null;
}
Integer thing = (Integer) _temp;

This makes it slightly more expensive than a regular cast in Java. However, there is no direct equivalent to this behavior in Java, short of writing a similar if statement anyways, so I think it's safe to say that casting in Kotlin is no more expensive than casting in Java.

apetranzilla
  • 5,331
  • 27
  • 34
  • 1
    Safe cast is separate though, it's more an extension of the existing cast. – Zoe Sep 12 '18 at 14:23
  • @Zoe, yes, I make a note of that near the end of my answer. – apetranzilla Sep 12 '18 at 14:25
  • 2
    *casting in Kotlin is no more expensive than casting in Java* -- this is not really an answer because OP did not ask about the cost relative to java – Tim Sep 12 '18 at 14:27
  • 1
    @TimCastelijns What metric would you prefer then? The exact time taken to cast would vary wildly depending on many different factors, so the best general way to quantify it in my opinion would be to compare it with Java. – apetranzilla Sep 12 '18 at 14:29
  • I don't know, you should ask OP – Tim Sep 12 '18 at 14:35
  • I would definitely also link some microbenchmarks for Java... but I wonder whether casting is supported for kotlin-js and kotlin-native too and if so: how is it solved there or how is the impact there? – Roland Sep 12 '18 at 14:49
  • The OP did specify what to compare against and it wasn't Java. It was using `test.question()` where `test` is `Test` vs using the following code `if (test is MathTest) test.question()`. So only within the realm of Kotlin, what is added cost between running that code 100 times, as specified by OP. – ryanholden8 Dec 05 '22 at 15:25