Slightly improve memory usage of starbound pak fs

This commit is contained in:
DBotThePony 2025-01-01 09:42:09 +07:00
parent 8d8143c914
commit 79381845b0
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -1,7 +1,10 @@
package ru.dbotthepony.kstarbound.io
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap
import it.unimi.dsi.fastutil.objects.Object2ObjectFunction
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap
import it.unimi.dsi.fastutil.objects.Object2ObjectMap
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps
import ru.dbotthepony.kommons.gson.get
import ru.dbotthepony.kommons.io.readBinaryString
import ru.dbotthepony.kommons.io.readVarInt
@ -48,10 +51,16 @@ class StarboundPak(val path: File, callback: (finished: Boolean, status: String)
get() = false
private var frozen = false
private val innerChildren = Object2ObjectLinkedOpenHashMap<String, IStarboundFile>()
private var innerChildren: Object2ObjectMap<String, IStarboundFile> = Object2ObjectArrayMap(2)
fun put(value: IStarboundFile) {
check(!frozen) { "Can't put, already frozen!" }
if (innerChildren is Object2ObjectArrayMap && innerChildren.size >= 8) {
innerChildren = Object2ObjectLinkedOpenHashMap(innerChildren)
children = Object2ObjectMaps.unmodifiable(innerChildren)
}
innerChildren[value.name] = value
}
@ -61,7 +70,8 @@ class StarboundPak(val path: File, callback: (finished: Boolean, status: String)
return innerChildren.computeIfAbsent(name, Object2ObjectFunction { SBDirectory(it as String, this) }) as? SBDirectory ?: throw IllegalStateException("$name already exists (in ${computeFullPath()})")
}
override val children: Map<String, IStarboundFile> = Collections.unmodifiableMap(innerChildren)
override var children: Map<String, IStarboundFile> = Object2ObjectMaps.unmodifiable(innerChildren)
private set
fun freeze() {
check(!frozen) { "Already frozen" }