Skip to content

Commit

Permalink
Integer fast-path for hashCode calculation (#216)
Browse files Browse the repository at this point in the history
An integer is its own hashCode, and the function is documented to simply return the input on the JVM. For JS and native the behavior is the same: https://github.com/JetBrains/kotlin/blob/1.9.0/kotlin-native/runtime/src/main/kotlin/kotlin/Primitives.kt#L1353.

Saves at least 3 bytes per property within the function bytecode, not counting any effects on things like the constant pool (for JVM).

Before:

    public int hashCode();
      Code:
         0: aload_0
         1: getfield      #23                 // Field int:I
         4: invokestatic  #50                 // Method java/lang/Integer.hashCode:(I)I
         7: istore_1
         8: iload_1
         9: bipush        31
        11: imul
         ⋮
        46: ireturn

After:

    public int hashCode();
      Code:
         0: aload_0
         1: getfield      #23                 // Field int:I
         4: istore_1
         5: iload_1
         6: bipush        31
         8: imul
         ⋮
        43: ireturn
  • Loading branch information
JakeWharton authored Aug 14, 2023
1 parent 8356520 commit 879c43e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotl
kotlin-gradleApi = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin-api", version.ref = "kotlin" }

assertk = "com.willowtreeapps.assertk:assertk:0.26.1"
asm-util = "org.ow2.asm:asm-util:9.5"

[plugins]

Expand Down
1 change: 1 addition & 0 deletions poko-compiler-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
testImplementation(libs.kotlin.compileTesting)
testImplementation(libs.junit)
testImplementation(libs.assertk)
testImplementation(libs.asm.util)
}

tasks.withType<KotlinCompile>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.classifierOrNull
import org.jetbrains.kotlin.ir.types.isInt
import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.ir.util.render
Expand Down Expand Up @@ -139,6 +140,11 @@ private fun IrBlockBodyBuilder.getHashCodeOf(
value: IrExpression,
messageCollector: MessageCollector,
): IrExpression {
// Fast path for integers which are already their own hashCode value.
if (property.type.isInt()) {
return value
}

val hasArrayContentBasedAnnotation = property.hasArrayContentBasedAnnotation()
val classifier = property.type.classifierOrNull

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.drewhamilton.poko

import assertk.all
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.doesNotContain
import assertk.assertions.isEqualTo
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.PluginOption
Expand Down Expand Up @@ -122,6 +124,21 @@ class PokoCompilerPluginTest {
}
//endregion

//region Performance optimizations
@Test fun `int property does not emit hashCode method invocation`() {
testCompilation(
"api/Primitives",
) {
val classFile = it.generatedFiles.single { it.name.endsWith(".class") }
val bytecode = bytecodeToText(classFile.readBytes())
assertThat(bytecode).all {
contains("java/lang/Long.hashCode")
doesNotContain("java/lang/Integer.hashCode")
}
}
}
//endregion

private inline fun testCompilation(
vararg sourceFileNames: String,
pokoAnnotationName: String = Poko::class.java.name,
Expand Down
16 changes: 16 additions & 0 deletions poko-compiler-plugin/src/test/kotlin/dev/drewhamilton/poko/asm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.drewhamilton.poko

import java.io.PrintWriter
import java.io.StringWriter
import org.objectweb.asm.ClassReader
import org.objectweb.asm.util.Textifier
import org.objectweb.asm.util.TraceClassVisitor

fun bytecodeToText(bytecode: ByteArray): String {
val textifier = Textifier()
ClassReader(bytecode).accept(TraceClassVisitor(null, textifier, null), 0)

val writer = StringWriter()
textifier.print(PrintWriter(writer))
return writer.toString().trim()
}

0 comments on commit 879c43e

Please sign in to comment.