diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt index 42b52af2..5523e366 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt @@ -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() + private var innerChildren: Object2ObjectMap = 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 = Collections.unmodifiableMap(innerChildren) + override var children: Map = Object2ObjectMaps.unmodifiable(innerChildren) + private set fun freeze() { check(!frozen) { "Already frozen" }