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

Eliminate redundant instanceof check in JS output #228

Merged
merged 1 commit into from
Sep 5, 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 @@ -7,7 +7,6 @@ import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.ir.builders.IrBlockBodyBuilder
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.IrGeneratorContextInterface
import org.jetbrains.kotlin.ir.builders.irAs
import org.jetbrains.kotlin.ir.builders.irBranch
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irElseBranch
Expand All @@ -19,6 +18,7 @@ import org.jetbrains.kotlin.ir.builders.irGetField
import org.jetbrains.kotlin.ir.builders.irIfThenElse
import org.jetbrains.kotlin.ir.builders.irIfThenReturnFalse
import org.jetbrains.kotlin.ir.builders.irIfThenReturnTrue
import org.jetbrains.kotlin.ir.builders.irImplicitCast
import org.jetbrains.kotlin.ir.builders.irIs
import org.jetbrains.kotlin.ir.builders.irNotEquals
import org.jetbrains.kotlin.ir.builders.irNotIs
Expand Down Expand Up @@ -70,7 +70,7 @@ internal fun IrBlockBodyBuilder.generateEqualsMethodBody(
+irIfThenReturnTrue(irEqeqeq(functionDeclaration.receiver(), irOther()))
+irIfThenReturnFalse(irNotIs(irOther(), irType))

val otherWithCast = irTemporary(irAs(irOther(), irType), "other_with_cast")
val otherWithCast = irTemporary(irImplicitCast(irOther(), irType), "other_with_cast")
for (property in classProperties) {
val field = property.backingField!!
val arg1 = irGetField(functionDeclaration.receiver(), field)
Expand Down
6 changes: 5 additions & 1 deletion poko-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ kotlin {
jvm()
jvm("jvmK2").applyK2()

js().nodejs()
js {
nodejs()
// Produce a JS file for performance tests.
binaries.executable()
}
js("jsK2").applyK2().nodejs()

mingwX64()
Expand Down
1 change: 1 addition & 0 deletions poko-tests/performance/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ dependencies {
}

tasks.named("test") {
dependsOn(":poko-tests:compileProductionExecutableKotlinJs")
dependsOn(":poko-tests:compileKotlinJvm")
}
15 changes: 15 additions & 0 deletions poko-tests/performance/src/test/kotlin/JsPerformanceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assertk.all
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.doesNotContain
import org.junit.Test

class JsPerformanceTest {
@Test fun `equals does not perform redundant instanceof check`() {
val javascript = jsOutput().readText()
assertThat(javascript).all {
contains("other instanceof IntAndLong")
doesNotContain("THROW_CCE")
}
}
}
1 change: 1 addition & 0 deletions poko-tests/performance/src/test/kotlin/sources.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.io.File

fun jvmOutput(relativePath: String) = File("../build/classes/kotlin/jvm/main", relativePath)
fun jsOutput() = File("../build/compileSync/js/main/productionExecutable/kotlin/Poko-poko-tests.js")
9 changes: 9 additions & 0 deletions poko-tests/src/commonMain/kotlin/performance/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package performance

/**
* An entrypoint for Kotlin/Native and Kotlin/JS.
* Should use all types on which you want to assert.
*/
fun main() {
println(IntAndLong(1, 2L) == IntAndLong(3, 4L))
}