Kotlin Data Class Bytecode Generation for hashCode() - uneeded null checks?


#1

If I look at the code created for a data class hashCode() method, there appears to be uneeded null checks. Example:

data class OrientationModelInput(
    val runId: UUID,
    val hardwareVersion: String,
    val platformVersion: String,
    val businessData: BusinessData,
    val confidenceThreshold: Double = 0.0,
    val bestInClassOnly: Boolean = false
    ) : GenericResource()

Looking at the generated bytecode (partial, removed identifying package name from GETFIELD):

  public hashCode()I
    ALOAD 0
    GETFIELD com/someplace/OrientationModelInput.runId : Ljava/util/UUID;
    DUP
    IFNULL L0
    INVOKEVIRTUAL java/lang/Object.hashCode ()I
    GOTO L1
   L0

I’m not a bytecode expert, but as I understand it, the IFNULL check would be checking to see if the value of the field was null, but since it’s not marked as nullable, it can never be null.

While not a big deal, it seems like this would cause performance to be slower than necessary (and it’s also showing up as an uncovered branch in my coverage reports). And this null check is there for every field, nullable or not.

Am I misunderstanding the bytecode or is there some other reason for the null check? Could it be removed?


#2

Yes, there’s an issue for that (although a bit more general): https://youtrack.jetbrains.com/issue/KT-12330