commit fbeb3d87ec4a60e9543c9ea4e923217ec7c457d6
parent 8bb2cb37d64fe6b9aaf62c88b69947228755beaa
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Fri, 10 Jan 2025 17:24:18 +0100

perf(app): log reader

Signed-off-by: rhunk <101876869+rhunk@users.noreply.github.com>

Diffstat:
Mapp/src/main/kotlin/me/rhunk/snapenhance/LogManager.kt | 32++++++++++++++++++++++----------
Mapp/src/main/kotlin/me/rhunk/snapenhance/ui/manager/pages/home/HomeLogs.kt | 24++++++++++--------------
2 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/LogManager.kt b/app/src/main/kotlin/me/rhunk/snapenhance/LogManager.kt @@ -78,18 +78,30 @@ class LogReader( } private fun queryLineCount(): Int { + val buffer = ByteArray(1024 * 1024) + synchronized(randomAccessFile) { randomAccessFile.seek(0) var lineCount = 0 - var lastPointer: Long - var line: String? - - while (randomAccessFile.also { - lastPointer = it.filePointer - }.readLine().also { line = it } != null) { - if (line?.startsWith('|') == true) { - lineCount++ - startLineIndexes.add(lastPointer + 1) + var read: Int + var lastPointer: Long = 0 + var line: StringBuilder? = null + + while (randomAccessFile.read(buffer).also { read = it } != -1) { + for (i in 0 until read) { + val char = buffer[i].toInt().toChar() + if (line == null) { + line = StringBuilder() + lastPointer = randomAccessFile.filePointer - read + i + } + line.append(char) + if (char == '\n') { + if (line.startsWith('|')) { + lineCount++ + startLineIndexes.add(lastPointer + 1) + } + line = null + } } } @@ -100,7 +112,7 @@ class LogReader( private fun getLine(index: Int): String? { if (index <= 0 || index > lineCount) return null synchronized(randomAccessFile) { - randomAccessFile.seek(startLineIndexes[index]) + randomAccessFile.seek(startLineIndexes.getOrNull(index) ?: return null) return readLogLine()?.toString() } } diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/ui/manager/pages/home/HomeLogs.kt b/app/src/main/kotlin/me/rhunk/snapenhance/ui/manager/pages/home/HomeLogs.kt @@ -156,8 +156,6 @@ class HomeLogs : Routes.Route() { logReader?.getLogLine(index) }) } - var expand by remember { mutableStateOf(false) } - logLine?.let { line -> Box(modifier = Modifier .fillMaxWidth() @@ -171,20 +169,18 @@ class HomeLogs : Routes.Route() { ) ) } - }, - onTap = { - expand = !expand } ) }) { - Row( + Column( modifier = Modifier .padding(4.dp) .fillMaxWidth() .defaultMinSize(minHeight = 30.dp), - verticalAlignment = Alignment.CenterVertically ) { - if (!expand) { + Row( + verticalAlignment = Alignment.CenterVertically, + ) { Icon( imageVector = when (line.logLevel) { LogLevel.DEBUG -> Icons.Outlined.BugReport @@ -193,14 +189,15 @@ class HomeLogs : Routes.Route() { LogLevel.WARN -> Icons.Outlined.Warning else -> Icons.Outlined.Info }, + modifier = Modifier.size(16.dp), contentDescription = null, ) Text( text = LogChannel.fromChannel(line.tag)?.shortName ?: line.tag, modifier = Modifier.padding(start = 4.dp), - fontWeight = FontWeight.Light, - fontSize = 10.sp, + fontWeight = FontWeight.Bold, + fontSize = 12.sp, ) Text( @@ -212,10 +209,9 @@ class HomeLogs : Routes.Route() { Text( text = line.message.trimIndent(), - fontSize = 10.sp, - maxLines = if (expand) Int.MAX_VALUE else 6, - overflow = if (expand) TextOverflow.Visible else TextOverflow.Ellipsis, - softWrap = !expand, + lineHeight = 10.sp, + fontSize = 9.sp, + maxLines = Int.MAX_VALUE, ) } }