Skip to content

Commit

Permalink
compare latency of javac/kotlinc
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed May 25, 2023
1 parent f440ad1 commit 8abeb58
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ tasks {
"localizedSyntaxRepair" to "edu.mcgill.cstk.experiments.repair.LocalizedSyntaxRepairKt",
"syntheticSyntaxRepair" to "edu.mcgill.cstk.experiments.repair.SyntheticSyntaxRepairKt",
"organicSyntaxRepair" to "edu.mcgill.cstk.experiments.repair.OrganicSyntaxRepairKt",
"javaSemanticRepair" to "edu.mcgill.cstk.experiments.repair.JavaSemanticRepairKt",
"kotlinSemanticRepair" to "edu.mcgill.cstk.experiments.repair.KotlinSemanticRepairKt",
"kotlinStatementRepair" to "edu.mcgill.cstk.experiments.repair.KotlinStatementRepairKt",
"pythonStatementRepair" to "edu.mcgill.cstk.experiments.repair.PythonStatementRepairKt",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package edu.mcgill.cstk.experiments.repair

import javax.tools.*
import org.jetbrains.kotlin.cli.common.messages.*
import org.jetbrains.kotlin.config.*
import java.io.*
import java.net.URI
import kotlin.system.measureTimeMillis
import javax.tools.JavaCompiler
import javax.tools.ToolProvider
import java.io.StringWriter
import javax.tools.JavaFileObject
import javax.tools.SimpleJavaFileObject
import javax.tools.JavaFileObject.Kind
import java.nio.charset.StandardCharsets

/*
./gradlew javaSemanticRepair
*/

fun main() {
measureTimeMillis {
for (i in 1..100) println(sourceCode.isCompileableJava())
}.also { println("Total time: ${it/1000.0}s") } // About ~157ms / statement :(
}

val javac: JavaCompiler = ToolProvider.getSystemJavaCompiler()

val sourceCode = """
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
""".trimIndent()

// Maybe we can get this down to about ~10ms by avoiding the write to disk?
fun String.isCompileableJava(): Boolean {
val sourceFile = object : SimpleJavaFileObject(URI.create("string:///Hello.java"), Kind.SOURCE) {
override fun getCharContent(ignoreEncodingErrors: Boolean) = this@isCompileableJava
}

val task =
javac.getTask(null, null, null, null, null, listOf(sourceFile))
val success = task.call()
return success
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.mcgill.cstk.experiments.repair

import org.apache.commons.io.output.NullPrintStream
import javax.tools.*
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.*
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer.WITHOUT_PATHS
Expand All @@ -10,8 +9,6 @@ import org.jetbrains.kotlin.config.*
import java.io.*
import kotlin.system.measureTimeMillis

val javaCompiler: JavaCompiler = ToolProvider.getSystemJavaCompiler()

/*
./gradlew kotlinSemanticRepair
*/
Expand All @@ -27,7 +24,7 @@ fun main() {
}.also { println("Total time: ${it/1000.0}s") } // About ~173ms / statement :(
}

val compiler = K2JVMCompiler()
val kotlinc = K2JVMCompiler()

val compilerArgs =
K2JVMCompilerArguments().apply {
Expand All @@ -41,8 +38,9 @@ val compilerArgs =
reportPerf = false
suppressWarnings = true
}

val msgCollector = PrintingMessageCollector(NullPrintStream(), WITHOUT_PATHS, true)

fun String.isCompilableKotlin(): Boolean =
File("temp.kt").apply { delete(); writeText(this@isCompilableKotlin) }
.run { compiler.execImpl(msgCollector, Services.EMPTY, compilerArgs) }.code == 0
.run { kotlinc.execImpl(msgCollector, Services.EMPTY, compilerArgs) }.code == 0

0 comments on commit 8abeb58

Please sign in to comment.