Skip to content

Commit 641d671

Browse files
committed
Flow performance improvements: mark crucial Flow DSL (unsafeFlow, collect, transform, map, mapNotNull, filter, filterNot, filterNotNull) as inline
1 parent b7e93f0 commit 641d671

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-core.txt

-1
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,6 @@ public final class kotlinx/coroutines/flow/FlowKt {
792792
public static final fun asFlow ([Ljava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
793793
public static final fun broadcastIn (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/CoroutineScope;ILkotlinx/coroutines/CoroutineStart;)Lkotlinx/coroutines/channels/BroadcastChannel;
794794
public static synthetic fun broadcastIn$default (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/CoroutineScope;ILkotlinx/coroutines/CoroutineStart;ILjava/lang/Object;)Lkotlinx/coroutines/channels/BroadcastChannel;
795-
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
796795
public static final fun combineLatest (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
797796
public static final fun count (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
798797
public static final fun count (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;

kotlinx-coroutines-core/common/src/flow/Builders.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public fun <T> flow(@BuilderInference block: suspend FlowCollector<T>.() -> Unit
5858
*/
5959
@FlowPreview
6060
@PublishedApi
61-
internal fun <T> unsafeFlow(@BuilderInference block: suspend FlowCollector<T>.() -> Unit): Flow<T> {
61+
internal inline fun <T> unsafeFlow(@BuilderInference crossinline block: suspend FlowCollector<T>.() -> Unit): Flow<T> {
6262
return object : Flow<T> {
6363
override suspend fun collect(collector: FlowCollector<T>) {
6464
collector.block()

kotlinx-coroutines-core/common/src/flow/operators/Transform.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import kotlinx.coroutines.flow.unsafeFlow as flow
2626
* ```
2727
*/
2828
@FlowPreview
29-
public fun <T, R> Flow<T>.transform(@BuilderInference transform: suspend FlowCollector<R>.(value: T) -> Unit): Flow<R> {
29+
public inline fun <T, R> Flow<T>.transform(@BuilderInference crossinline transform: suspend FlowCollector<R>.(value: T) -> Unit): Flow<R> {
3030
return flow {
3131
collect { value ->
3232
transform(value)
@@ -38,7 +38,7 @@ public fun <T, R> Flow<T>.transform(@BuilderInference transform: suspend FlowCol
3838
* Returns a flow containing only values of the original flow that matches the given [predicate].
3939
*/
4040
@FlowPreview
41-
public fun <T> Flow<T>.filter(predicate: suspend (T) -> Boolean): Flow<T> = flow {
41+
public inline fun <T> Flow<T>.filter(crossinline predicate: suspend (T) -> Boolean): Flow<T> = flow {
4242
collect { value ->
4343
if (predicate(value)) emit(value)
4444
}
@@ -48,7 +48,7 @@ public fun <T> Flow<T>.filter(predicate: suspend (T) -> Boolean): Flow<T> = flow
4848
* Returns a flow containing only values of the original flow that do not match the given [predicate].
4949
*/
5050
@FlowPreview
51-
public fun <T> Flow<T>.filterNot(predicate: suspend (T) -> Boolean): Flow<T> = flow {
51+
public inline fun <T> Flow<T>.filterNot(crossinline predicate: suspend (T) -> Boolean): Flow<T> = flow {
5252
collect { value ->
5353
if (!predicate(value)) emit(value)
5454
}
@@ -73,15 +73,15 @@ public fun <T: Any> Flow<T?>.filterNotNull(): Flow<T> = flow<T> {
7373
* Returns a flow containing the results of applying the given [transform] function to each value of the original flow.
7474
*/
7575
@FlowPreview
76-
public fun <T, R> Flow<T>.map(transform: suspend (value: T) -> R): Flow<R> = transform { value ->
76+
public inline fun <T, R> Flow<T>.map(crossinline transform: suspend (value: T) -> R): Flow<R> = transform { value ->
7777
emit(transform(value))
7878
}
7979

8080
/**
8181
* Returns a flow that contains only non-null results of applying the given [transform] function to each value of the original flow.
8282
*/
8383
@FlowPreview
84-
public fun <T, R: Any> Flow<T>.mapNotNull(transform: suspend (value: T) -> R?): Flow<R> = transform { value ->
84+
public inline fun <T, R: Any> Flow<T>.mapNotNull(crossinline transform: suspend (value: T) -> R?): Flow<R> = transform { value ->
8585
val transformed = transform(value) ?: return@transform
8686
emit(transformed)
8787
}

kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import kotlin.jvm.*
2828
* ```
2929
*/
3030
@FlowPreview
31-
public suspend fun <T> Flow<T>.collect(action: suspend (value: T) -> Unit): Unit =
31+
public suspend inline fun <T> Flow<T>.collect(crossinline action: suspend (value: T) -> Unit): Unit =
3232
collect(object : FlowCollector<T> {
3333
override suspend fun emit(value: T) = action(value)
3434
})

0 commit comments

Comments
 (0)