Optimise DiskStore::list

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 4 years ago
parent 6314f3625a
commit 7ab567b7b9
  1. 28
      cache/src/main/java/dev/openrs2/cache/DiskStore.kt

@ -81,24 +81,32 @@ public class DiskStore private constructor(
override fun list(archive: Int): List<Int> { override fun list(archive: Int): List<Int> {
checkArchive(archive) 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 index = indexes[archive] ?: throw FileNotFoundException()
val groups = mutableListOf<Int>() val groups = mutableListOf<Int>()
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 var pos = 0L
for (group in 0 until groupCount) { var group = 0
while (remaining > 0) {
tempBuf.clear() tempBuf.clear()
index.read(pos, tempBuf, INDEX_ENTRY_SIZE)
tempBuf.skipBytes(3)
val block = tempBuf.readUnsignedMedium() val n = min(remaining, tempBuf.writableBytes().toLong()).toInt()
if (block != 0) { index.read(pos, tempBuf, n)
groups += group 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 return groups

Loading…
Cancel
Save