mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-29 01:01:19 +08:00
Improve performance
This commit is contained in:
parent
c3de96dde0
commit
c89e2a7aa6
@ -51,15 +51,26 @@ fun <E> LockFreeLinkedList<E>.toMutableSet(): MutableSet<E> {
|
|||||||
/**
|
/**
|
||||||
* Builds a [Sequence] containing all the elements in [this] in the same order.
|
* Builds a [Sequence] containing all the elements in [this] in the same order.
|
||||||
*
|
*
|
||||||
* Note that the sequence is dynamic, that is, elements are yielded atomically only when it is required
|
* Note that the sequence is dynamic
|
||||||
*/
|
*/
|
||||||
@MiraiInternalAPI
|
@MiraiInternalAPI
|
||||||
fun <E> LockFreeLinkedList<E>.asSequence(): Sequence<E> {
|
fun <E> LockFreeLinkedList<E>.asSequence(): Sequence<E> {
|
||||||
return sequence {
|
return generateSequence(head) { current: LockFreeLinkedListNode<E> ->
|
||||||
forEach {
|
current.nextValidNode(until = tail).takeIf { it != tail }
|
||||||
yield(it)
|
}.drop(1) // drop head, should be dropped lazily
|
||||||
|
.map { it.nodeValue }
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(MiraiInternalAPI::class)
|
||||||
|
internal fun <E> LockFreeLinkedListNode<E>.nextValidNode(until: LockFreeLinkedListNode<E>): LockFreeLinkedListNode<E> {
|
||||||
|
var node: LockFreeLinkedListNode<E> = this.nextNode
|
||||||
|
while (node != until) {
|
||||||
|
if (node.isValidElementNode()) {
|
||||||
|
return node
|
||||||
}
|
}
|
||||||
|
node = node.nextNode
|
||||||
}
|
}
|
||||||
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
@MiraiInternalAPI
|
@MiraiInternalAPI
|
||||||
|
@ -20,6 +20,7 @@ import kotlin.test.assertFalse
|
|||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
@Suppress("UnusedEquals")
|
@Suppress("UnusedEquals")
|
||||||
|
@OptIn(MiraiInternalAPI::class)
|
||||||
@MiraiExperimentalAPI
|
@MiraiExperimentalAPI
|
||||||
internal class LockFreeLinkedListTest {
|
internal class LockFreeLinkedListTest {
|
||||||
@Test
|
@Test
|
||||||
@ -263,7 +264,7 @@ internal class LockFreeLinkedListTest {
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
@OptIn(ExperimentalCoroutinesApi::class, MiraiInternalAPI::class)
|
||||||
@MiraiExperimentalAPI
|
@MiraiExperimentalAPI
|
||||||
internal suspend inline fun <E : LockFreeLinkedList<*>> E.concurrentDo(
|
internal suspend inline fun <E : LockFreeLinkedList<*>> E.concurrentDo(
|
||||||
numberOfCoroutines: Int,
|
numberOfCoroutines: Int,
|
||||||
|
Loading…
Reference in New Issue
Block a user