Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore context preservation invariant in flatMapMerge (#1452)
* Introduce (again) flowProduce in order to properly propagate cancellation to the upstream in flatMapMerge. Previously this issue was masked by SerializingCollector fast-path * Re-implement flatMapMerge via the channel to have context preservation property Fixes #1440
- Loading branch information
Showing
14 changed files
with
216 additions
and
151 deletions.
There are no files selected for viewing
35 changes: 0 additions & 35 deletions
35
benchmarks/src/jmh/kotlin/benchmarks/YieldRelativeCostBenchmark.kt
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
benchmarks/src/jmh/kotlin/benchmarks/flow/FlatMapMergeBenchmark.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,47 @@ | ||
/* | ||
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package benchmarks.flow | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
import org.openjdk.jmh.annotations.* | ||
import java.util.concurrent.* | ||
|
||
@Warmup(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS) | ||
@Fork(value = 1) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@State(Scope.Benchmark) | ||
open class FlatMapMergeBenchmark { | ||
|
||
// Note: tests only absence of contention on downstream | ||
|
||
@Param("10", "100", "1000") | ||
private var iterations = 100 | ||
|
||
@Benchmark | ||
fun flatMapUnsafe() = runBlocking { | ||
benchmarks.flow.scrabble.flow { | ||
repeat(iterations) { emit(it) } | ||
}.flatMapMerge { value -> | ||
flowOf(value) | ||
}.collect { | ||
if (it == -1) error("") | ||
} | ||
} | ||
|
||
@Benchmark | ||
fun flatMapSafe() = runBlocking { | ||
kotlinx.coroutines.flow.flow { | ||
repeat(iterations) { emit(it) } | ||
}.flatMapMerge { value -> | ||
flowOf(value) | ||
}.collect { | ||
if (it == -1) error("") | ||
} | ||
} | ||
|
||
} |
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
81 changes: 0 additions & 81 deletions
81
kotlinx-coroutines-core/common/src/flow/internal/Concurrent.kt
This file was deleted.
Oops, something went wrong.
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
20 changes: 20 additions & 0 deletions
20
kotlinx-coroutines-core/common/src/flow/internal/SendingCollector.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,20 @@ | ||
/* | ||
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.flow.internal | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.channels.* | ||
import kotlinx.coroutines.flow.* | ||
|
||
/** | ||
* Collection that sends to channel | ||
* @suppress **This an internal API and should not be used from general code.** | ||
*/ | ||
@InternalCoroutinesApi | ||
public class SendingCollector<T>( | ||
private val channel: SendChannel<T> | ||
) : FlowCollector<T> { | ||
override suspend fun emit(value: T) = channel.send(value) | ||
} |
Oops, something went wrong.