Skip to content

Commit 673c859

Browse files
committed
Start from 2 threads instead of #CORES_COUNT
1 parent e7d5ad8 commit 673c859

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineScheduler.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,13 @@ internal class CoroutineScheduler(
161161
}
162162

163163
workers = arrayOfNulls(maxPoolSize)
164-
// todo: can we lazily create corePool, too?
165-
// todo: The goal: when running "small" workload on "large" machine we should not consume extra resource in advance
166-
// todo: Can't we just invoke createNewWorker here to get the first one up and running?
167-
for (i in 0 until corePoolSize) {
164+
// By default create at most 2 workers and allocate next ones lazily
165+
val initialSize = corePoolSize.coerceAtMost(2)
166+
for (i in 0 until initialSize) {
168167
workers[i] = PoolWorker(i).apply { start() }
169168
}
170169

171-
controlState.value = corePoolSize.toLong()
170+
controlState.value = initialSize.toLong()
172171
}
173172

174173
/*

core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineDispatcherTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class CoroutineDispatcherTest : SchedulerTestBase() {
9292
assertEquals(thread, Thread.currentThread())
9393
}
9494

95-
checkPoolThreadsCreated()
95+
checkPoolThreadsCreated(initialPoolSize()..initialPoolSize() + 1)
9696
}
9797

9898
@Test
@@ -131,7 +131,7 @@ class CoroutineDispatcherTest : SchedulerTestBase() {
131131
finish(4)
132132
}
133133

134-
checkPoolThreadsCreated()
134+
checkPoolThreadsCreated(initialPoolSize()..CORES_COUNT)
135135
}
136136

137137
@Test

core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineSchedulerShrinkTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CoroutineSchedulerShrinkTest : SchedulerTestBase() {
2323
// Init dispatcher
2424
async(dispatcher) { }.await()
2525
// Pool is initialized with core size in the beginning
26-
checkPoolThreadsExist(corePoolSize)
26+
checkPoolThreadsExist(initialPoolSize()..initialPoolSize() + 1)
2727

2828
// Run blocking tasks and check increased threads count
2929
val blockingTasks = launchBlocking()

core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/SchedulerTestBase.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ abstract class SchedulerTestBase : TestBase() {
4444
require(threads == expectedThreadsCount) { "Expected $expectedThreadsCount threads, but has $threads" }
4545
}
4646

47+
fun initialPoolSize() = Runtime.getRuntime().availableProcessors().coerceAtMost(2)
48+
4749
private fun maxSequenceNumber(): Int? {
4850
return Thread.getAllStackTraces().keys.filter { it is CoroutineScheduler.PoolWorker }
4951
.map { sequenceNumber(it.name) }.max()

0 commit comments

Comments
 (0)