Improve performance

This commit is contained in:
Him188 2020-06-24 03:48:11 +08:00
parent 9a91c9b2a7
commit 927943322a
2 changed files with 65 additions and 81 deletions

View File

@ -19,10 +19,6 @@ import net.mamoe.mirai.console.setting.internal.isOdd
import net.mamoe.mirai.console.setting.internal.valueFromKTypeImpl import net.mamoe.mirai.console.setting.internal.valueFromKTypeImpl
import net.mamoe.mirai.console.setting.internal.valueImpl import net.mamoe.mirai.console.setting.internal.valueImpl
import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiExperimentalAPI
import net.mamoe.yamlkt.Yaml
import net.mamoe.yamlkt.YamlConfiguration
import net.mamoe.yamlkt.YamlConfiguration.ListSerialization.FLOW_SEQUENCE
import net.mamoe.yamlkt.YamlConfiguration.MapSerialization.FLOW_MAP
import net.mamoe.yamlkt.YamlNullableDynamicSerializer import net.mamoe.yamlkt.YamlNullableDynamicSerializer
import kotlin.internal.LowPriorityInOverloadResolution import kotlin.internal.LowPriorityInOverloadResolution
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
@ -66,75 +62,73 @@ internal abstract class SettingImpl {
internal val valueNodes: MutableList<Node<*>> = mutableListOf() internal val valueNodes: MutableList<Node<*>> = mutableListOf()
internal open val updaterSerializer: KSerializer<Unit> by lazy { internal open val updaterSerializer: KSerializer<Unit> = object : KSerializer<Unit> {
object : KSerializer<Unit> { override val descriptor: SerialDescriptor get() = settingUpdaterSerializerDescriptor
override val descriptor: SerialDescriptor get() = settingUpdaterSerializerDescriptor
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun deserialize(decoder: Decoder) { override fun deserialize(decoder: Decoder) {
val descriptor = descriptor val descriptor = descriptor
with(decoder.beginStructure(descriptor, *settingUpdaterSerializerTypeArguments)) { with(decoder.beginStructure(descriptor, *settingUpdaterSerializerTypeArguments)) {
if (decodeSequentially()) { if (decodeSequentially()) {
var index = 0
repeat(decodeCollectionSize(descriptor)) {
val serialName = decodeSerializableElement(descriptor, index++, String.serializer())
val node = findNodeInstance(serialName)
if (node == null) {
decodeSerializableElement(descriptor, index++, YamlNullableDynamicSerializer)
} else {
decodeSerializableElement(descriptor, index++, node.updaterSerializer)
}
}
} else {
outerLoop@ while (true) {
var serialName: String? = null
innerLoop@ while (true) {
val index = decodeElementIndex(descriptor)
if (index == CompositeDecoder.READ_DONE) {
check(serialName == null) { "name must be null at this moment." }
break@outerLoop
}
if (!index.isOdd()) { // key
check(serialName == null) { "name must be null at this moment" }
serialName = decodeSerializableElement(descriptor, index, String.serializer())
} else {
check(serialName != null) { "name must not be null at this moment" }
val node = findNodeInstance(serialName)
if (node == null) {
decodeSerializableElement(descriptor, index, YamlNullableDynamicSerializer)
} else {
decodeSerializableElement(descriptor, index, node.updaterSerializer)
}
break@innerLoop
}
}
}
}
endStructure(descriptor)
}
}
@Suppress("UNCHECKED_CAST")
override fun serialize(encoder: Encoder, value: Unit) {
val descriptor = descriptor
with(encoder.beginCollection(descriptor, valueNodes.size, *settingUpdaterSerializerTypeArguments)) {
var index = 0 var index = 0
repeat(decodeCollectionSize(descriptor)) {
// val vSerializer = settingUpdaterSerializerTypeArguments[1] as KSerializer<Any?> val serialName = decodeSerializableElement(descriptor, index++, String.serializer())
valueNodes.forEach { (serialName, _, valueSerializer) -> val node = findNodeInstance(serialName)
encodeSerializableElement(descriptor, index++, String.serializer(), serialName) if (node == null) {
encodeSerializableElement(descriptor, index++, valueSerializer, Unit) decodeSerializableElement(descriptor, index++, YamlNullableDynamicSerializer)
} else {
decodeSerializableElement(descriptor, index++, node.updaterSerializer)
}
} }
endStructure(descriptor) } else {
} outerLoop@ while (true) {
} var serialName: String? = null
innerLoop@ while (true) {
val index = decodeElementIndex(descriptor)
if (index == CompositeDecoder.READ_DONE) {
check(serialName == null) { "name must be null at this moment." }
break@outerLoop
}
if (!index.isOdd()) { // key
check(serialName == null) { "name must be null at this moment" }
serialName = decodeSerializableElement(descriptor, index, String.serializer())
} else {
check(serialName != null) { "name must not be null at this moment" }
val node = findNodeInstance(serialName)
if (node == null) {
decodeSerializableElement(descriptor, index, YamlNullableDynamicSerializer)
} else {
decodeSerializableElement(descriptor, index, node.updaterSerializer)
}
break@innerLoop
}
}
}
}
endStructure(descriptor)
}
} }
@Suppress("UNCHECKED_CAST")
override fun serialize(encoder: Encoder, value: Unit) {
val descriptor = descriptor
with(encoder.beginCollection(descriptor, valueNodes.size, *settingUpdaterSerializerTypeArguments)) {
var index = 0
// val vSerializer = settingUpdaterSerializerTypeArguments[1] as KSerializer<Any?>
valueNodes.forEach { (serialName, _, valueSerializer) ->
encodeSerializableElement(descriptor, index++, String.serializer(), serialName)
encodeSerializableElement(descriptor, index++, valueSerializer, Unit)
}
endStructure(descriptor)
}
}
} }
/** /**
@ -145,22 +139,9 @@ internal abstract class SettingImpl {
} }
companion object { companion object {
private val ABSENT_STUB = Any()
private val settingUpdaterSerializerTypeArguments = arrayOf(String.serializer(), YamlNullableDynamicSerializer) private val settingUpdaterSerializerTypeArguments = arrayOf(String.serializer(), YamlNullableDynamicSerializer)
private val settingUpdaterSerializerDescriptor = private val settingUpdaterSerializerDescriptor =
MapSerializer(settingUpdaterSerializerTypeArguments[0], settingUpdaterSerializerTypeArguments[1]).descriptor MapSerializer(settingUpdaterSerializerTypeArguments[0], settingUpdaterSerializerTypeArguments[1]).descriptor
val allFlow = Yaml(
YamlConfiguration(
nonStrictNullability = true,
nonStrictNumber = true,
mapSerialization = FLOW_MAP,
listSerialization = FLOW_SEQUENCE,
classSerialization = FLOW_MAP
)
)
} }
} }

View File

@ -0,0 +1,3 @@
package net.mamoe.mirai.console.setting
interface SettingStorage