Skip to content

Commit 6f395d7

Browse files
committed
handle Dispatchers.Default.limitedParallelism when requested parallelism >= core pool size (Kotlin#3442)
`LimitedDispatcher.limitedParallelism` returns `this` if requested parallelism is greater or equal to the own parallelism of the said `LimitedDispatcher`. `DefaultScheduler` has parallelism effectively set to `CORE_POOL_SIZE`. Before the change `LimitedDispatcher(parallelism)` was returned. While it does work as expected, any submitted task goes through its queue and `parallelism` number of workers. The change allows to eliminate the `LimitedDispatcher` instance and its queue in case the requested parallelism is greater or equal to `CORE_POOL_SIZE`.
1 parent d837e02 commit 6f395d7

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

kotlinx-coroutines-core/jvm/src/scheduling/Dispatcher.kt

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ internal object DefaultScheduler : SchedulerCoroutineDispatcher(
1414
CORE_POOL_SIZE, MAX_POOL_SIZE,
1515
IDLE_WORKER_KEEP_ALIVE_NS, DEFAULT_SCHEDULER_NAME
1616
) {
17+
18+
@ExperimentalCoroutinesApi
19+
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
20+
parallelism.checkParallelism()
21+
if (parallelism >= CORE_POOL_SIZE) return this
22+
return super.limitedParallelism(parallelism)
23+
}
24+
1725
// Shuts down the dispatcher, used only by Dispatchers.shutdown()
1826
internal fun shutdown() {
1927
super.close()

0 commit comments

Comments
 (0)