кеш свойств

This commit is contained in:
DBotThePony 2023-02-01 12:33:51 +07:00
parent ae8f8ad658
commit 1fd4ed6257
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -14,12 +14,15 @@ import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import it.unimi.dsi.fastutil.objects.ObjectArraySet
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kstarbound.Starbound
import ru.dbotthepony.kstarbound.defs.util.flattenJsonElement
import ru.dbotthepony.kstarbound.io.json.util.asReference
import ru.dbotthepony.kstarbound.util.NotNullVar
import java.util.concurrent.ConcurrentHashMap
import kotlin.properties.Delegates
import kotlin.reflect.KCallable
import kotlin.reflect.KClass
@ -349,12 +352,27 @@ class BuilderAdapter<T : Any> private constructor(
companion object : TypeAdapterFactory {
private val LOGGER = LogManager.getLogger()
private val declCache = Reference2ObjectOpenHashMap<KClass<*>, ArrayList<KMutableProperty1<*, *>>>()
private fun collectDecl(input: KClass<*>, output: MutableMap<String, KMutableProperty1<*, *>>) {
for (decl in collectDeclImpl(input)) {
output.putIfAbsent(decl.name, decl)
}
}
private fun collectDeclImpl(input: KClass<*>): ArrayList<KMutableProperty1<*, *>> {
val getValues = synchronized(declCache) { declCache[input] }
if (getValues != null) {
return getValues
}
val values = ObjectLinkedOpenHashSet<KMutableProperty1<*, *>>()
for (decl in input.declaredMembers) {
if (decl is KMutableProperty1<*, *>) {
decl.isAccessible = true
output.putIfAbsent(decl.name, decl)
values.add(decl)
}
}
@ -362,9 +380,13 @@ class BuilderAdapter<T : Any> private constructor(
val classifier = parent.classifier
if (classifier is KClass<*> && !classifier.java.isInterface) {
collectDecl(classifier, output)
values.addAll(collectDeclImpl(classifier))
}
}
val list = ArrayList(values)
synchronized(declCache) { declCache.put(input, list) }
return list
}
override fun <T : Any> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {