Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer fast-path for hashCode calculation #216

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -123,6 +125,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()
}