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

Do not invoke hashCode for UInt #230

Merged
merged 2 commits into from
Sep 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.drewhamilton.poko.ir

import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.ir.builders.IrBlockBodyBuilder
import org.jetbrains.kotlin.ir.builders.IrGeneratorContextInterface
Expand Down Expand Up @@ -33,6 +34,7 @@ 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.types.isUInt
import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.name.CallableId
Expand Down Expand Up @@ -144,6 +146,14 @@ private fun IrBlockBodyBuilder.getHashCodeOf(
if (property.type.isInt()) {
return value
}
if (property.type.isUInt()) {
val uintToInt = referenceClass(StandardNames.FqNames.uInt)!!
.functions
.single { it.owner.name.asString() == "toInt" }
return irCall(uintToInt).apply {
dispatchReceiver = value
}
}

val hasArrayContentBasedAnnotation = property.hasArrayContentBasedAnnotation()
val classifier = property.type.classifierOrNull
Expand Down
9 changes: 9 additions & 0 deletions poko-tests/performance/src/test/kotlin/JvmPerformanceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ class JvmPerformanceTest {
}
}

@Test fun `uint property does not emit hashCode method invocation`() {
val classfile = jvmOutput("performance/UIntAndLong.class")
val bytecode = bytecodeToText(classfile.readBytes())
assertThat(bytecode).all {
contains("java/lang/Long.hashCode")
doesNotContain("kotlin/UInt.hashCode-impl")
}
}

@Test fun `toString uses invokedynamic on modern JDKs`() {
val classfile = jvmOutput("performance/IntAndLong.class", version = 11)
val bytecode = bytecodeToText(classfile.readBytes())
Expand Down
9 changes: 9 additions & 0 deletions poko-tests/src/commonMain/kotlin/performance/UIntAndLong.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package performance

import dev.drewhamilton.poko.Poko

@Suppress("unused")
@Poko class UIntAndLong(
val uint: UInt,
val long: Long,
)