https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

danny

12/06/2018, 7:09 PM
Any one else notice that
CoroutineScheduler
seems far too conservative about parking threads?
I see a bunch of time spend in
Thread.yield
from
cpuWorkerIdle
- about double the system CPU time as user time. I must only have a few dozen coroutines running
Guess this approximiates what I'm seeing -
Copy code
fun main() {
    runBlocking {
        val count = AtomicInteger()
        val ints = produce<Int> {
            repeat(10) {
                launch(Dispatchers.Default) {
                    while (true) {
                        channel.send(count.incrementAndGet())
                    }
                }
            }
        }
        for (int in ints) {
            // Single consumer doing something expensive that can't be concurrent
            Thread.sleep(2)
        }
    }
}
Vary the sleep in the loop consuming the channel and watch the scheduler back off and start parking threads
message has been deleted
message has been deleted
v

Vsevolod Tolstopyatov [JB]

12/07/2018, 11:23 AM
https://github.com/Kotlin/kotlinx.coroutines/issues/840 yes, it is too conservative, this behaviour definitely will be fixed
d

danny

12/10/2018, 9:01 PM
@Vsevolod Tolstopyatov [JB] is that something that's being actively worked on? I'd prefer to not have to rewrite to avoid the issue, but the system property only helps with
Dispatchers.Default
and not
IO
Also feels like there's more going on than just the spin/yield overhead - threads appear to spend far more time running than I'd expect at low throughput
v

Vsevolod Tolstopyatov [JB]

12/12/2018, 1:46 PM
Partial fix of this issue will be shipped in 1.1.0