From 6a6b743ddf544f8d756ad24cd0978db5de2989f7 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Thu, 16 May 2024 11:32:24 -0700 Subject: [PATCH] Remove unnecessary allocations with scatter maps --- .../dev/romainguy/kotlin/explorer/code/Code.kt | 13 +++++++------ .../kotlin/explorer/code/CodeBuilder.kt | 18 +++++++++++------- .../kotlin/explorer/code/CodeTextArea.kt | 5 ++++- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/Code.kt b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/Code.kt index 0ecf2e5b..fae98482 100644 --- a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/Code.kt +++ b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/Code.kt @@ -16,6 +16,7 @@ package dev.romainguy.kotlin.explorer.code +import androidx.collection.IntIntMap /** * A data model representing disassembled code @@ -26,15 +27,15 @@ package dev.romainguy.kotlin.explorer.code */ class Code( val text: String, - private val jumps: Map, - private val sourceToCodeLine: Map, - private val codeToSourceToLine: Map, + private val jumps: IntIntMap, + private val sourceToCodeLine: IntIntMap, + private val codeToSourceToLine: IntIntMap, ) { - fun getJumpTargetOfLine(line: Int) = jumps[line] + fun getJumpTargetOfLine(line: Int) = jumps.getOrDefault(line, -1) - fun getCodeLine(sourceLine: Int) = sourceToCodeLine[sourceLine] + fun getCodeLine(sourceLine: Int) = sourceToCodeLine.getOrDefault(sourceLine, -1) - fun getSourceLine(codeLine: Int) = codeToSourceToLine[codeLine] + fun getSourceLine(codeLine: Int) = codeToSourceToLine.getOrDefault(codeLine, -1) companion object { fun fromClasses(classes: List, codeStyle: CodeStyle = CodeStyle()): Code { 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 0895efcc..6ab2e9e7 100644 --- a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeBuilder.kt +++ b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeBuilder.kt @@ -16,6 +16,9 @@ package dev.romainguy.kotlin.explorer.code +import androidx.collection.IntIntPair +import androidx.collection.mutableIntIntMapOf + fun buildCode(codeStyle: CodeStyle = CodeStyle(), builderAction: CodeBuilder.() -> Unit): CodeBuilder { return CodeBuilder(codeStyle).apply(builderAction) } @@ -29,13 +32,13 @@ fun buildCode(codeStyle: CodeStyle = CodeStyle(), builderAction: CodeBuilder.() class CodeBuilder(private val codeStyle: CodeStyle) { private var line = 0 private val sb = StringBuilder() - private val jumps = mutableMapOf() - private val sourceToCodeLine = mutableMapOf() - private val codeToSourceToLine = mutableMapOf() + private val jumps = mutableIntIntMapOf() + private val sourceToCodeLine = mutableIntIntMapOf() + private val codeToSourceToLine = mutableIntIntMapOf() // These 3 fields collect method scope data. They are reset when a method is added - private val methodAddresses = mutableMapOf() - private val methodJumps = mutableListOf>() + private val methodAddresses = mutableIntIntMapOf() + private val methodJumps = mutableListOf() private var lastMethodLineNumber: Int = -1 fun startClass(clazz: Class) { @@ -71,7 +74,8 @@ class CodeBuilder(private val codeStyle: CodeStyle) { fun endMethod() { methodJumps.forEach { (line, address) -> - val targetLine = methodAddresses[address] ?: return@forEach + val targetLine = methodAddresses.getOrDefault(address, -1) + if (targetLine == -1) return@forEach jumps[line] = targetLine } methodAddresses.clear() @@ -85,7 +89,7 @@ class CodeBuilder(private val codeStyle: CodeStyle) { sb.append(" ".repeat(codeStyle.indent)) methodAddresses[instruction.address] = line if (instruction.jumpAddress != null) { - methodJumps.add(line to instruction.jumpAddress) + methodJumps.add(IntIntPair(line, instruction.jumpAddress)) } val lineNumber = instruction.lineNumber if (lineNumber != null) { diff --git a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeTextArea.kt b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeTextArea.kt index 13cbdbe9..b37bd545 100644 --- a/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeTextArea.kt +++ b/src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/CodeTextArea.kt @@ -62,6 +62,7 @@ class CodeTextArea( override fun mouseClicked(event: MouseEvent) { val codeLine = getLineOfOffset(viewToModel2D(event.point)) val line = code?.getSourceLine(codeLine) ?: return + if (line == -1) return sourceTextArea.gotoLine(this@CodeTextArea, line - 1) } }) @@ -76,6 +77,7 @@ class CodeTextArea( fun gotoSourceLine(sourceLine: Int) { val line = code?.getCodeLine(sourceLine + 1) ?: return + if (line == -1) return caretPosition = getLineStartOffset(line.coerceIn(0 until lineCount)) centerCaretInView() } @@ -154,7 +156,8 @@ class CodeTextArea( val oldJumpOffsets = jumpOffsets try { jumpOffsets = null - val dstLine = codeModel.getJumpTargetOfLine(line) ?: return + val dstLine = codeModel.getJumpTargetOfLine(line) + if (dstLine == -1) return val srcOffset = getLineStartOffset(line) + getLine(line).countPadding() val dstOffset = getLineStartOffset(dstLine) + getLine(dstLine).countPadding()