Start from 2 threads instead of #CORES_COUNT
diff --git a/core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineScheduler.kt b/core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineScheduler.kt
index 251d6a1..8803f8a 100644
--- a/core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineScheduler.kt
+++ b/core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineScheduler.kt
@@ -161,14 +161,13 @@
}
workers = arrayOfNulls(maxPoolSize)
- // todo: can we lazily create corePool, too?
- // todo: The goal: when running "small" workload on "large" machine we should not consume extra resource in advance
- // todo: Can't we just invoke createNewWorker here to get the first one up and running?
- for (i in 0 until corePoolSize) {
+ // By default create at most 2 workers and allocate next ones lazily
+ val initialSize = corePoolSize.coerceAtMost(2)
+ for (i in 0 until initialSize) {
workers[i] = PoolWorker(i).apply { start() }
}
- controlState.value = corePoolSize.toLong()
+ controlState.value = initialSize.toLong()
}
/*
diff --git a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineDispatcherTest.kt b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineDispatcherTest.kt
index 94fdf09..55c90bd 100644
--- a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineDispatcherTest.kt
+++ b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineDispatcherTest.kt
@@ -92,7 +92,7 @@
assertEquals(thread, Thread.currentThread())
}
- checkPoolThreadsCreated()
+ checkPoolThreadsCreated(initialPoolSize()..initialPoolSize() + 1)
}
@Test
@@ -131,7 +131,7 @@
finish(4)
}
- checkPoolThreadsCreated()
+ checkPoolThreadsCreated(initialPoolSize()..CORES_COUNT)
}
@Test
diff --git a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineSchedulerShrinkTest.kt b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineSchedulerShrinkTest.kt
index c888e88..20278a6 100644
--- a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineSchedulerShrinkTest.kt
+++ b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineSchedulerShrinkTest.kt
@@ -23,7 +23,7 @@
// Init dispatcher
async(dispatcher) { }.await()
// Pool is initialized with core size in the beginning
- checkPoolThreadsExist(corePoolSize)
+ checkPoolThreadsExist(initialPoolSize()..initialPoolSize() + 1)
// Run blocking tasks and check increased threads count
val blockingTasks = launchBlocking()
diff --git a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/SchedulerTestBase.kt b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/SchedulerTestBase.kt
index d738767..ef4ce14 100644
--- a/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/SchedulerTestBase.kt
+++ b/core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/SchedulerTestBase.kt
@@ -44,6 +44,8 @@
require(threads == expectedThreadsCount) { "Expected $expectedThreadsCount threads, but has $threads" }
}
+ fun initialPoolSize() = Runtime.getRuntime().availableProcessors().coerceAtMost(2)
+
private fun maxSequenceNumber(): Int? {
return Thread.getAllStackTraces().keys.filter { it is CoroutineScheduler.PoolWorker }
.map { sequenceNumber(it.name) }.max()