Skip to content

Commit d837e02

Browse files
committed
handle Dispatchers.IO.limitedParallelism(Int.MAX_VALUE) case (Kotlin#3442)
`LimitedDispatcher.limitedParallelism` returns `this` if requested parallelism is greater or equal to the own parallelism of the said `LimitedDispatcher`. `UnlimitedIoScheduler` has parallelism effectively set to `Int.MAX_VALUE`, so `parallelism >= this.parallelism` check folds into `parallelism == Int.MAX_VALUE`. Before the change `LimitedDispatcher(Int.MAX_VALUE)` was returned. While it does work as expected, any submitted task goes through its queue and `Int.MAX_VALUE` number of workers. The change allows to eliminate the `LimitedDispatcher` instance and its queue in this extreme case.
1 parent bb4dcde commit d837e02

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ private object UnlimitedIoScheduler : CoroutineDispatcher() {
3838
override fun dispatch(context: CoroutineContext, block: Runnable) {
3939
DefaultScheduler.dispatchWithContext(block, BlockingContext, false)
4040
}
41+
42+
@ExperimentalCoroutinesApi
43+
override fun limitedParallelism(parallelism: Int): CoroutineDispatcher {
44+
parallelism.checkParallelism()
45+
if (parallelism == Int.MAX_VALUE) return this
46+
return super.limitedParallelism(parallelism)
47+
}
4148
}
4249

4350
// Dispatchers.IO

0 commit comments

Comments
 (0)