Skip to content

Commit 1fd7da3

Browse files
committed
Reduce hand-rolled ArrayQueue with ArrayDeque in standard library in order to (potentially) reduce dex size
Move ArrayQueue to JS-specific source-set to avoid touching JS
1 parent 7d8e275 commit 1fd7da3

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

kotlinx-coroutines-core/common/src/EventLoop.common.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal abstract class EventLoop : CoroutineDispatcher() {
3636
* Queue used by [Dispatchers.Unconfined] tasks.
3737
* These tasks are thread-local for performance and take precedence over the rest of the queue.
3838
*/
39-
private var unconfinedQueue: ArrayQueue<DispatchedTask<*>>? = null
39+
private var unconfinedQueue: ArrayDeque<DispatchedTask<*>>? = null
4040

4141
/**
4242
* Processes next event in this event loop.
@@ -49,7 +49,7 @@ internal abstract class EventLoop : CoroutineDispatcher() {
4949
* **NOTE**: Must be invoked only from the event loop's thread
5050
* (no check for performance reasons, may be added in the future).
5151
*/
52-
public open fun processNextEvent(): Long {
52+
open fun processNextEvent(): Long {
5353
if (!processUnconfinedEvent()) return Long.MAX_VALUE
5454
return 0
5555
}
@@ -59,10 +59,10 @@ internal abstract class EventLoop : CoroutineDispatcher() {
5959
protected open val nextTime: Long
6060
get() {
6161
val queue = unconfinedQueue ?: return Long.MAX_VALUE
62-
return if (queue.isEmpty) Long.MAX_VALUE else 0L
62+
return if (queue.isEmpty()) Long.MAX_VALUE else 0L
6363
}
6464

65-
public fun processUnconfinedEvent(): Boolean {
65+
fun processUnconfinedEvent(): Boolean {
6666
val queue = unconfinedQueue ?: return false
6767
val task = queue.removeFirstOrNull() ?: return false
6868
task.run()
@@ -74,27 +74,27 @@ internal abstract class EventLoop : CoroutineDispatcher() {
7474
* By default, event loop implementation is thread-local and should not processed in the context
7575
* (current thread's event loop should be processed instead).
7676
*/
77-
public open fun shouldBeProcessedFromContext(): Boolean = false
77+
open fun shouldBeProcessedFromContext(): Boolean = false
7878

7979
/**
8080
* Dispatches task whose dispatcher returned `false` from [CoroutineDispatcher.isDispatchNeeded]
8181
* into the current event loop.
8282
*/
83-
public fun dispatchUnconfined(task: DispatchedTask<*>) {
83+
fun dispatchUnconfined(task: DispatchedTask<*>) {
8484
val queue = unconfinedQueue ?:
85-
ArrayQueue<DispatchedTask<*>>().also { unconfinedQueue = it }
85+
ArrayDeque<DispatchedTask<*>>().also { unconfinedQueue = it }
8686
queue.addLast(task)
8787
}
8888

89-
public val isActive: Boolean
89+
val isActive: Boolean
9090
get() = useCount > 0
9191

92-
public val isUnconfinedLoopActive: Boolean
92+
val isUnconfinedLoopActive: Boolean
9393
get() = useCount >= delta(unconfined = true)
9494

9595
// May only be used from the event loop's thread
96-
public val isUnconfinedQueueEmpty: Boolean
97-
get() = unconfinedQueue?.isEmpty ?: true
96+
val isUnconfinedQueueEmpty: Boolean
97+
get() = unconfinedQueue?.isEmpty() ?: true
9898

9999
private fun delta(unconfined: Boolean) =
100100
if (unconfined) (1L shl 32) else 1L
@@ -200,7 +200,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
200200
}
201201
}
202202

203-
protected override val nextTime: Long
203+
override val nextTime: Long
204204
get() {
205205
if (super.nextTime == 0L) return 0L
206206
val queue = _queue.value
@@ -227,7 +227,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
227227
rescheduleAllDelayed()
228228
}
229229

230-
public override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
230+
override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
231231
val timeNanos = delayToNanos(timeMillis)
232232
if (timeNanos < MAX_DELAY_NS) {
233233
val now = nanoTime()
@@ -283,7 +283,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
283283
return nextTime
284284
}
285285

286-
public final override fun dispatch(context: CoroutineContext, block: Runnable) = enqueue(block)
286+
final override fun dispatch(context: CoroutineContext, block: Runnable) = enqueue(block)
287287

288288
open fun enqueue(task: Runnable) {
289289
if (enqueueImpl(task)) {
@@ -362,7 +362,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
362362

363363
}
364364

365-
public fun schedule(now: Long, delayedTask: DelayedTask) {
365+
fun schedule(now: Long, delayedTask: DelayedTask) {
366366
when (scheduleImpl(now, delayedTask)) {
367367
SCHEDULE_OK -> if (shouldUnpark(delayedTask)) unpark()
368368
SCHEDULE_COMPLETED -> reschedule(now, delayedTask)
@@ -481,7 +481,6 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
481481
final override fun dispose() {
482482
val heap = _heap
483483
if (heap === DISPOSED_TASK) return // already disposed
484-
@Suppress("UNCHECKED_CAST")
485484
(heap as? DelayedTaskQueue)?.remove(this) // remove if it is in heap (first)
486485
_heap = DISPOSED_TASK // never add again to any heap
487486
}
@@ -530,7 +529,7 @@ internal expect fun createEventLoop(): EventLoop
530529
internal expect fun nanoTime(): Long
531530

532531
internal expect object DefaultExecutor {
533-
public fun enqueue(task: Runnable)
532+
fun enqueue(task: Runnable)
534533
}
535534

536535
/**

kotlinx-coroutines-core/common/src/internal/ArrayQueue.kt renamed to kotlinx-coroutines-core/js/src/internal/ArrayQueue.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.coroutines.internal
@@ -11,22 +11,22 @@ internal open class ArrayQueue<T : Any> {
1111

1212
val isEmpty: Boolean get() = head == tail
1313

14-
public fun addLast(element: T) {
14+
fun addLast(element: T) {
1515
elements[tail] = element
1616
tail = (tail + 1) and elements.size - 1
1717
if (tail == head) ensureCapacity()
1818
}
1919

2020
@Suppress("UNCHECKED_CAST")
21-
public fun removeFirstOrNull(): T? {
21+
fun removeFirstOrNull(): T? {
2222
if (head == tail) return null
2323
val element = elements[head]
2424
elements[head] = null
2525
head = (head + 1) and elements.size - 1
2626
return element as T
2727
}
2828

29-
public fun clear() {
29+
fun clear() {
3030
head = 0
3131
tail = 0
3232
elements = arrayOfNulls(elements.size)

0 commit comments

Comments
 (0)