From 162dfb7c71232f0a84db49996af16e71567f018e Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 20 Apr 2020 17:08:45 +0800 Subject: [PATCH] Improve performance --- .../message/data/CombinedMessage.kt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/CombinedMessage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/CombinedMessage.kt index f2fb3f7bc..0008e48b3 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/CombinedMessage.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/CombinedMessage.kt @@ -34,7 +34,7 @@ internal constructor( @JvmField internal val left: Message, // 必须已经完成 constrain single @JvmField internal val tail: Message ) : Message, MessageChain { - @OptIn(MiraiExperimentalAPI::class) + fun asSequence(): Sequence = sequence { yieldCombinedOrElementsFlatten(this@CombinedMessage) } @@ -43,12 +43,13 @@ internal constructor( return asSequence().iterator() } - override val size: Int by lazy { - var size = 0 - size += if (left is MessageChain) left.size else 1 - size += if (tail is MessageChain) tail.size else 1 - size - } + override val size: Int + get() = kotlin.run { + var size = 0 + size += if (left is MessageChain) left.size else 1 + size += if (tail is MessageChain) tail.size else 1 + size + } override fun equals(other: Any?): Boolean { return other is CombinedMessage && other.left == this.left && other.tail == this.tail @@ -76,7 +77,7 @@ internal constructor( private suspend fun SequenceScope.yieldCombinedOrElementsFlatten(message: Message) { when (message) { is CombinedMessage -> { - // fast path, 避免创建新的 iterator, 也不会挂起协程 + // fast path yieldCombinedOrElementsFlatten(message.left) yieldCombinedOrElementsFlatten(message.tail) }