From 7ab567b7b9a90368161462793e69e0255c01aab2 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 30 Aug 2020 11:42:26 +0100 Subject: [PATCH] Optimise DiskStore::list Signed-off-by: Graham --- .../main/java/dev/openrs2/cache/DiskStore.kt | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/cache/src/main/java/dev/openrs2/cache/DiskStore.kt b/cache/src/main/java/dev/openrs2/cache/DiskStore.kt index 5be35573c2..42472436c1 100644 --- a/cache/src/main/java/dev/openrs2/cache/DiskStore.kt +++ b/cache/src/main/java/dev/openrs2/cache/DiskStore.kt @@ -81,24 +81,32 @@ public class DiskStore private constructor( override fun list(archive: Int): List { checkArchive(archive) - alloc.buffer(TEMP_BUFFER_SIZE, TEMP_BUFFER_SIZE).use { tempBuf -> + alloc.buffer(INDEX_BUFFER_SIZE, INDEX_BUFFER_SIZE).use { tempBuf -> val index = indexes[archive] ?: throw FileNotFoundException() val groups = mutableListOf() - val groupCount = min(index.size() / INDEX_ENTRY_SIZE, Int.MAX_VALUE.toLong()).toInt() + var remaining = min(index.size() / INDEX_ENTRY_SIZE, Int.MAX_VALUE.toLong()) * INDEX_ENTRY_SIZE var pos = 0L - for (group in 0 until groupCount) { + var group = 0 + while (remaining > 0) { tempBuf.clear() - index.read(pos, tempBuf, INDEX_ENTRY_SIZE) - tempBuf.skipBytes(3) - val block = tempBuf.readUnsignedMedium() - if (block != 0) { - groups += group - } + val n = min(remaining, tempBuf.writableBytes().toLong()).toInt() + index.read(pos, tempBuf, n) + pos += n + remaining -= n - pos += INDEX_ENTRY_SIZE + while (tempBuf.isReadable) { + tempBuf.skipBytes(3) + + val block = tempBuf.readUnsignedMedium() + if (block != 0) { + groups += group + } + + group++ + } } return groups