-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Flow.transformWhile operator #2066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc1af1f
Flow.transformWhile operator
elizarov 0e48151
Update kotlinx-coroutines-core/common/src/flow/operators/Limit.kt
elizarov 4fb8987
~ Workaround JS BE bugs and more tests
elizarov 2e04a92
~ Issue KT-39227 added
elizarov 48bebb8
~ tranformWhile workaround, too
elizarov fdd724d
~ Fixes after review
elizarov 14f009e
Update benchmarks/src/jmh/kotlin/benchmarks/flow/TakeWhileBenchmark.kt
elizarov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
benchmarks/src/jmh/kotlin/benchmarks/flow/TakeWhileBenchmark.kt
This file contains hidden or 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,68 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") | ||
|
||
package benchmarks.flow | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.flow.internal.* | ||
import org.openjdk.jmh.annotations.* | ||
import java.util.concurrent.TimeUnit | ||
|
||
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | ||
@Fork(value = 1) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@State(Scope.Benchmark) | ||
open class TakeWhileBenchmark { | ||
@Param("1", "10", "100", "1000") | ||
private var size: Int = 0 | ||
|
||
private suspend inline fun Flow<Long>.consume() = | ||
filter { it % 2L != 0L } | ||
.map { it * it }.count() | ||
|
||
@Benchmark | ||
fun baseline() = runBlocking<Int> { | ||
(0L until size).asFlow().consume() | ||
} | ||
|
||
@Benchmark | ||
fun takeWhileDirect() = runBlocking<Int> { | ||
(0L..Long.MAX_VALUE).asFlow().takeWhileDirect { it < size }.consume() | ||
} | ||
|
||
@Benchmark | ||
fun takeWhileViaCollectWhile() = runBlocking<Int> { | ||
(0L..Long.MAX_VALUE).asFlow().takeWhileViaCollectWhile { it < size }.consume() | ||
} | ||
|
||
// Direct implemenatation by checking predicate and throwing AbortFlowException | ||
elizarov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private fun <T> Flow<T>.takeWhileDirect(predicate: suspend (T) -> Boolean): Flow<T> = unsafeFlow { | ||
try { | ||
collect { value -> | ||
if (predicate(value)) emit(value) | ||
else throw AbortFlowException(this) | ||
} | ||
} catch (e: AbortFlowException) { | ||
e.checkOwnership(owner = this) | ||
} | ||
} | ||
|
||
// Essentially the same code, but reusing the logic via collectWhile function | ||
private fun <T> Flow<T>.takeWhileViaCollectWhile(predicate: suspend (T) -> Boolean): Flow<T> = unsafeFlow { | ||
// This return is needed to work around a bug in JS BE: KT-39227 | ||
return@unsafeFlow collectWhile { value -> | ||
if (predicate(value)) { | ||
emit(value) | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
70 changes: 70 additions & 0 deletions
70
kotlinx-coroutines-core/common/test/flow/operators/TransformWhileTest.kt
This file contains hidden or 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,70 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.flow | ||
|
||
import kotlinx.coroutines.* | ||
import kotlin.test.* | ||
|
||
class TransformWhileTest : TestBase() { | ||
@Test | ||
fun testSimple() = runTest { | ||
val flow = (0..10).asFlow() | ||
val expected = listOf("A", "B", "C", "D") | ||
val actual = flow.transformWhile { value -> | ||
when(value) { | ||
0 -> { emit("A"); true } | ||
1 -> true | ||
2 -> { emit("B"); emit("C"); true } | ||
3 -> { emit("D"); false } | ||
else -> { expectUnreached(); false } | ||
} | ||
}.toList() | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun testCancelUpstream() = runTest { | ||
var cancelled = false | ||
val flow = flow { | ||
coroutineScope { | ||
launch(start = CoroutineStart.ATOMIC) { | ||
hang { cancelled = true } | ||
} | ||
emit(1) | ||
emit(2) | ||
emit(3) | ||
} | ||
} | ||
val transformed = flow.transformWhile { | ||
emit(it) | ||
it < 2 | ||
} | ||
assertEquals(listOf(1, 2), transformed.toList()) | ||
assertTrue(cancelled) | ||
} | ||
|
||
@Test | ||
fun testExample() = runTest { | ||
val source = listOf( | ||
DownloadProgress(0), | ||
DownloadProgress(50), | ||
DownloadProgress(100), | ||
DownloadProgress(147) | ||
) | ||
val expected = source.subList(0, 3) | ||
val actual = source.asFlow().completeWhenDone().toList() | ||
assertEquals(expected, actual) | ||
} | ||
|
||
private fun Flow<DownloadProgress>.completeWhenDone(): Flow<DownloadProgress> = | ||
transformWhile { progress -> | ||
emit(progress) // always emit progress | ||
!progress.isDone() // continue while download is not done | ||
} | ||
|
||
private data class DownloadProgress(val percent: Int) { | ||
fun isDone() = percent >= 100 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo