From d0ef99728e2e3279d7bd1fe114880713d05bcf07 Mon Sep 17 00:00:00 2001 From: Stedoss <29103029+Stedoss@users.noreply.github.com> Date: Sun, 23 Feb 2025 21:54:50 +0000 Subject: [PATCH] Pre-allocate arrays in seperate function --- .../kotlin/com/nisemoe/nise/replays/Wtc.kt | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/nise-backend/src/main/kotlin/com/nisemoe/nise/replays/Wtc.kt b/nise-backend/src/main/kotlin/com/nisemoe/nise/replays/Wtc.kt index d81eb07..6572d7a 100644 --- a/nise-backend/src/main/kotlin/com/nisemoe/nise/replays/Wtc.kt +++ b/nise-backend/src/main/kotlin/com/nisemoe/nise/replays/Wtc.kt @@ -189,17 +189,20 @@ class WTC { } private fun seperate(stream: String): FrameLists { - val wList = mutableListOf() - val xList = mutableListOf() - val yList = mutableListOf() - val zList = mutableListOf() + val frames = stream.split(',') + val frameCount = frames.size - for (frame in stream.split(",")) { + val ws = IntArray(frameCount) + val xs = ShortArray(frameCount) + val ys = ShortArray(frameCount) + val zs = ByteArray(frameCount) + + for ((i, frame) in frames.withIndex()) { if (frame.isEmpty()) { continue } - val splitFrame = frame.split("|") + val splitFrame = frame.split('|') val w = splitFrame[0].toInt() val x = splitFrame[1].toFloat() val y = splitFrame[2].toFloat() @@ -216,17 +219,17 @@ class WTC { if (yy <= -0x8000) yy = -0x8000 else if (yy >= 0x7FFF) yy = 0x7FFF - wList.add(w) - xList.add(xx.toShort()) - yList.add(yy.toShort()) - zList.add(zz.toByte()) + ws[i] = w + xs[i] = xx.toShort() + ys[i] = yy.toShort() + zs[i] = zz.toByte() } return FrameLists( - x = xList.toShortArray(), - y = yList.toShortArray(), - z = zList.toByteArray(), - w = wList.toIntArray(), + x = xs, + y = ys, + z = zs, + w = ws, ) }