Optimise DiskStore::list

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

@ -81,16 +81,23 @@ public class DiskStore private constructor(
override fun list(archive: Int): List<Int> {
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<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
for (group in 0 until groupCount) {
var group = 0
while (remaining > 0) {
tempBuf.clear()
index.read(pos, tempBuf, INDEX_ENTRY_SIZE)
val n = min(remaining, tempBuf.writableBytes().toLong()).toInt()
index.read(pos, tempBuf, n)
pos += n
remaining -= n
while (tempBuf.isReadable) {
tempBuf.skipBytes(3)
val block = tempBuf.readUnsignedMedium()
@ -98,7 +105,8 @@ public class DiskStore private constructor(
groups += group
}
pos += INDEX_ENTRY_SIZE
group++
}
}
return groups

Loading…
Cancel
Save