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

Matt

01/31/2020, 5:03 AM
Hello, I'm creating a CommonMark parser (spec.commonmark.com) and the second stage is being done in parallel, the second stage is where inlines are parsed from blocks, I've noticed that if I have a large number of blocks (say 1000) and I launch them into a coroutine about 20% of my runtime is actually switching coroutine contexts. This is obviously not ideal and I was wondering if anyone knew how to make it so that each 'job' is ran until it is completed then the next 'job' is picked up
o

octylFractal

01/31/2020, 5:05 AM
that already happens as much as possible, if you're switching dispatchers it'd be quite inefficient and cumbersome to block your previous dispatcher from running anything at all
if you're not switching dispatchers, no suspension happens at all iirc
m

Matt

01/31/2020, 5:07 AM
this is the flamegraph I am getting, (above the blue is where I'm assuming context switching is happening) from what you said, this would be the completion of one job and the thread changing context to the next job?
Copy code
private suspend fun parseLeaves(leaves: List<Leaf>){
        coroutineScope {
            // launches all leaves into seperate coroutines to be dealt with as they can
            leaves.forEach { launch { analyzeBlock(it) } }
        }
    }
Is how Im launching my coroutines
o

octylFractal

01/31/2020, 5:08 AM
if you're getting a thread name change every context switch, you likely left the kotlin coroutines debug flag on
m

Matt

01/31/2020, 5:08 AM
ugh lol
how do i turn that off? mostly debugging but trying to see performance
Doing a report for school on my perofrmance vs other implementations
m

Matt

01/31/2020, 5:10 AM
thanks so much!
11 Views