From 38d40e04ce1f049acccbad0de022b470e18df052 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Thu, 16 May 2024 13:01:53 -0700 Subject: [PATCH] Use maps instead of arrays for branch lookups --- .../kotlin/explorer/code/CodeBuilder.kt | 23 +++++++++++++------ .../kotlin/explorer/code/DataModels.kt | 13 +++++++---- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeBuilder.kt b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeBuilder.kt index 6ab2e9e7..928406fb 100644 --- a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeBuilder.kt +++ b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeBuilder.kt @@ -61,12 +61,11 @@ class CodeBuilder(private val codeStyle: CodeStyle) { var count = 0 instructionSet.instructions.forEach { instruction -> val code = instruction.code - val index = code.indexOf(": ") - instructionSet.isa.branchInstructions.forEach out@ { opCode -> - if (code.startsWith(opCode, index + 2)) { - count++ - return@out - } + val start = code.indexOf(": ") + 2 + val end = code.indexOfFirst(start) { c -> !c.isLetter() } + val opCode = code.substring(start, end) + if (instructionSet.isa.branchInstructions.contains(opCode)) { + count++ } } return count @@ -115,4 +114,14 @@ class CodeBuilder(private val codeStyle: CodeStyle) { sb.append('\n') line++ } -} \ No newline at end of file +} + +private inline fun CharSequence.indexOfFirst(start: Int, predicate: (Char) -> Boolean): Int { + val end = length + for (index in start..) { - ByteCode(arrayOf("if")), - Dex(arrayOf("if-")), +import androidx.collection.ScatterSet +import androidx.collection.scatterSetOf + +enum class ISA(val branchInstructions: ScatterSet) { + ByteCode(scatterSetOf("if")), + Dex(scatterSetOf("if")), X86_64( - arrayOf( + scatterSetOf( "je", "jz", "jne", @@ -45,7 +48,7 @@ enum class ISA(val branchInstructions: Array) { "jna" ) ), - Arm64(arrayOf("b.", "b ", "bl", "cbz", "cbnz", "tbz", "tbnz")) + Arm64(scatterSetOf("b", "bl", "cbz", "cbnz", "tbz", "tbnz")) } data class Class(val header: String, val methods: List)