Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore thread context elements when directly resuming to parent.
This fix solves the problem of restoring thread-context when returning to another context in undispatched way. It impacts suspend/resume performance of coroutines that use ThreadContextElement and undispatched coroutines. The kotlinx.coroutines code poisons the context with special 'UndispatchedMarker' element and linear lookup is performed only when the marker is present. The code also contains description of an alternative approach in order to save a linear lookup in complex coroutines hierarchies. Fast-path of coroutine resumption is slowed down by a single context lookup. Fixes #985 Co-authored-by: Roman Elizarov <elizarov@gmail.com>
- Loading branch information
Showing
12 changed files
with
420 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
benchmarks/src/jmh/kotlin/benchmarks/ChannelSinkDepthBenchmark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package benchmarks | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.channels.* | ||
import org.openjdk.jmh.annotations.* | ||
import java.util.concurrent.* | ||
import kotlin.coroutines.* | ||
|
||
@Warmup(iterations = 7, time = 1) | ||
@Measurement(iterations = 5, time = 1) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Benchmark) | ||
@Fork(2) | ||
open class ChannelSinkDepthBenchmark { | ||
private val tl = ThreadLocal.withInitial({ 42 }) | ||
|
||
private val unconfinedOneElement = Dispatchers.Unconfined + tl.asContextElement() | ||
|
||
@Benchmark | ||
fun depth1(): Int = runBlocking { | ||
run(1, unconfinedOneElement) | ||
} | ||
|
||
@Benchmark | ||
fun depth10(): Int = runBlocking { | ||
run(10, unconfinedOneElement) | ||
} | ||
|
||
@Benchmark | ||
fun depth100(): Int = runBlocking { | ||
run(100, unconfinedOneElement) | ||
} | ||
|
||
@Benchmark | ||
fun depth1000(): Int = runBlocking { | ||
run(1000, unconfinedOneElement) | ||
} | ||
|
||
private suspend inline fun run(callTraceDepth: Int, context: CoroutineContext): Int { | ||
return Channel | ||
.range(1, 10_000, context) | ||
.filter(callTraceDepth, context) { it % 4 == 0 } | ||
.fold(0) { a, b -> a + b } | ||
} | ||
|
||
private fun Channel.Factory.range(start: Int, count: Int, context: CoroutineContext) = | ||
GlobalScope.produce(context) { | ||
for (i in start until (start + count)) | ||
send(i) | ||
} | ||
|
||
// Migrated from deprecated operators, are good only for stressing channels | ||
|
||
private fun ReceiveChannel<Int>.filter( | ||
callTraceDepth: Int, | ||
context: CoroutineContext = Dispatchers.Unconfined, | ||
predicate: suspend (Int) -> Boolean | ||
): ReceiveChannel<Int> = | ||
GlobalScope.produce(context, onCompletion = { cancel() }) { | ||
deeplyNestedFilter(this, callTraceDepth, predicate) | ||
} | ||
|
||
private suspend fun ReceiveChannel<Int>.deeplyNestedFilter( | ||
sink: ProducerScope<Int>, | ||
depth: Int, | ||
predicate: suspend (Int) -> Boolean | ||
) { | ||
if (depth <= 1) { | ||
for (e in this) { | ||
if (predicate(e)) sink.send(e) | ||
} | ||
} else { | ||
deeplyNestedFilter(sink, depth - 1, predicate) | ||
require(true) // tail-call | ||
} | ||
} | ||
|
||
private suspend inline fun <E, R> ReceiveChannel<E>.fold(initial: R, operation: (acc: R, E) -> R): R { | ||
var accumulator = initial | ||
consumeEach { | ||
accumulator = operation(accumulator, it) | ||
} | ||
return accumulator | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.