Строковый интернер создаём сразу
This commit is contained in:
parent
1fe0b25efa
commit
ec98c6970c
@ -447,7 +447,5 @@ object Starbound {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
items.values.forEach { println(it) }
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,6 @@ import ru.dbotthepony.kstarbound.getValue
|
||||
import ru.dbotthepony.kstarbound.setValue
|
||||
import java.lang.reflect.Constructor
|
||||
import kotlin.jvm.internal.DefaultConstructorMarker
|
||||
import kotlin.properties.Delegates
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.full.isSuperclassOf
|
||||
import kotlin.reflect.full.isSupertypeOf
|
||||
@ -113,7 +112,7 @@ private class MapAdapter<K, V>(private val boundKey: Class<K>, private val bound
|
||||
}
|
||||
}
|
||||
|
||||
private class StringMapAdapter<V>(private val bound: Class<V>, private val stringAdapter: TypeAdapter<String>, private val interner: () -> Interner<String>) : TypeAdapter<Map<String, V>?>() {
|
||||
private class StringMapAdapter<V>(private val bound: Class<V>, private val stringAdapter: TypeAdapter<String>, private val interner: Interner<String>) : TypeAdapter<Map<String, V>?>() {
|
||||
private val resolvedBound by lazy { resolveBound(bound, stringAdapter) }
|
||||
|
||||
override fun write(out: JsonWriter, value: Map<String, V>?) {
|
||||
@ -139,7 +138,7 @@ private class StringMapAdapter<V>(private val bound: Class<V>, private val strin
|
||||
val resolvedBound = resolvedBound
|
||||
|
||||
while (reader.peek() != JsonToken.END_OBJECT) {
|
||||
builder.put(interner.invoke().intern(reader.nextName()), resolvedBound.read(reader))
|
||||
builder.put(interner.intern(reader.nextName()), resolvedBound.read(reader))
|
||||
}
|
||||
|
||||
reader.endObject()
|
||||
@ -174,10 +173,10 @@ private data class PackedProperty<Clazz : Any, T>(
|
||||
class KConcreteTypeAdapter<T : Any> private constructor(
|
||||
val bound: KClass<T>,
|
||||
private val types: ImmutableList<PackedProperty<T, *>>,
|
||||
private val asJsonArray: Boolean = false
|
||||
private val asJsonArray: Boolean = false,
|
||||
private val stringInterner: Interner<String>
|
||||
) : TypeAdapter<T>() {
|
||||
private val mapped = Object2IntArrayMap<String>()
|
||||
private val internedStrings = Interners.newWeakInterner<String>()
|
||||
private val loggedMisses = ObjectArraySet<String>()
|
||||
|
||||
var currentSymbolicName by ThreadLocal<String>()
|
||||
@ -347,13 +346,6 @@ class KConcreteTypeAdapter<T : Any> private constructor(
|
||||
}
|
||||
}
|
||||
|
||||
// intern'им строки для более быстрой работы сравнения последних
|
||||
for (i in readValues.indices) {
|
||||
if (readValues[i] is String) {
|
||||
readValues[i] = internedStrings.intern(readValues[i] as String)
|
||||
}
|
||||
}
|
||||
|
||||
if (asJsonArray) {
|
||||
reader.endArray()
|
||||
} else {
|
||||
@ -442,7 +434,7 @@ class KConcreteTypeAdapter<T : Any> private constructor(
|
||||
*/
|
||||
class Builder<T : Any>(val clazz: KClass<T>) {
|
||||
private val types = ArrayList<PackedProperty<T, *>>()
|
||||
private var interner by Delegates.notNull<Interner<String>>()
|
||||
private val stringInterner = Interners.newWeakInterner<String>()
|
||||
|
||||
private val internedStringAdapter: TypeAdapter<String> = object : TypeAdapter<String>() {
|
||||
override fun write(out: JsonWriter, value: String) {
|
||||
@ -450,7 +442,7 @@ class KConcreteTypeAdapter<T : Any> private constructor(
|
||||
}
|
||||
|
||||
override fun read(`in`: JsonReader): String {
|
||||
return interner.intern(TypeAdapters.STRING.read(`in`))
|
||||
return stringInterner.intern(TypeAdapters.STRING.read(`in`))
|
||||
}
|
||||
}
|
||||
|
||||
@ -559,7 +551,7 @@ class KConcreteTypeAdapter<T : Any> private constructor(
|
||||
* Таблица неизменяема (создаётся объект [ImmutableMap])
|
||||
*/
|
||||
fun <V> map(field: KProperty1<T, Map<String, V>?>, valueType: Class<V>): Builder<T> {
|
||||
types.add(PackedProperty(field, StringMapAdapter(valueType, internedStringAdapter, ::interner)))
|
||||
types.add(PackedProperty(field, StringMapAdapter(valueType, internedStringAdapter, stringInterner)))
|
||||
return this
|
||||
}
|
||||
|
||||
@ -569,14 +561,17 @@ class KConcreteTypeAdapter<T : Any> private constructor(
|
||||
* Таблица неизменяема (создаётся объект [ImmutableMap])
|
||||
*/
|
||||
fun <V : Any> map(field: KProperty1<T, Map<String, V>?>, valueType: KClass<V>): Builder<T> {
|
||||
types.add(PackedProperty(field, StringMapAdapter(valueType.java, internedStringAdapter, ::interner)))
|
||||
types.add(PackedProperty(field, StringMapAdapter(valueType.java, internedStringAdapter, stringInterner)))
|
||||
return this
|
||||
}
|
||||
|
||||
fun build(asList: Boolean = false): KConcreteTypeAdapter<T> {
|
||||
return KConcreteTypeAdapter(clazz, ImmutableList.copyOf(types), asJsonArray = asList).also {
|
||||
interner = it.internedStrings
|
||||
}
|
||||
return KConcreteTypeAdapter(
|
||||
bound = clazz,
|
||||
types = ImmutableList.copyOf(types),
|
||||
asJsonArray = asList,
|
||||
stringInterner = stringInterner
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user