Skip to content

Commit

Permalink
Remove unnecessary allocations with scatter maps
Browse files Browse the repository at this point in the history
  • Loading branch information
romainguy committed May 16, 2024
1 parent 8fd00d7 commit 6a6b743
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/code/Code.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dev.romainguy.kotlin.explorer.code

import androidx.collection.IntIntMap

/**
* A data model representing disassembled code
Expand All @@ -26,15 +27,15 @@ package dev.romainguy.kotlin.explorer.code
*/
class Code(
val text: String,
private val jumps: Map<Int, Int>,
private val sourceToCodeLine: Map<Int, Int>,
private val codeToSourceToLine: Map<Int, Int>,
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<Class>, codeStyle: CodeStyle = CodeStyle()): Code {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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<Int, Int>()
private val sourceToCodeLine = mutableMapOf<Int, Int>()
private val codeToSourceToLine = mutableMapOf<Int, Int>()
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<Int, Int>()
private val methodJumps = mutableListOf<Pair<Int, Int>>()
private val methodAddresses = mutableIntIntMapOf()
private val methodJumps = mutableListOf<IntIntPair>()
private var lastMethodLineNumber: Int = -1

fun startClass(clazz: Class) {
Expand Down Expand Up @@ -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()
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand All @@ -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()
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 6a6b743

Please sign in to comment.