From 45bf24c51d6460b5b1b6ae569d009bb315291231 Mon Sep 17 00:00:00 2001 From: Major Date: Fri, 6 Mar 2020 07:46:14 +0000 Subject: [PATCH] Remove copies in InsnMatcher Signed-off-by: Major --- asm/src/main/java/dev/openrs2/asm/InsnMatcher.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/asm/src/main/java/dev/openrs2/asm/InsnMatcher.kt b/asm/src/main/java/dev/openrs2/asm/InsnMatcher.kt index 982df6ac..61a94e49 100644 --- a/asm/src/main/java/dev/openrs2/asm/InsnMatcher.kt +++ b/asm/src/main/java/dev/openrs2/asm/InsnMatcher.kt @@ -12,9 +12,17 @@ class InsnMatcher private constructor(private val regex: Regex) { } fun match(list: InsnList): Sequence> { - val insns = list.filter { it.opcode != -1 }.toList() - val codepoints = insns.map { opcodeToCodepoint(it.opcode) }.toCharArray() - return regex.findAll(String(codepoints)).map { + val insns = ArrayList(list.size()) + val builder = StringBuilder(list.size()) + + for (instruction in list) { + if (instruction.opcode != -1) { + insns += instruction + builder.append(opcodeToCodepoint(instruction.opcode)) + } + } + + return regex.findAll(builder).map { insns.subList(it.range.first, it.range.last + 1) } }