@@ -24,6 +24,11 @@ open class ChannelSinkBenchmark {
24
24
private val unconfinedOneElement = Dispatchers .Unconfined + tl.asContextElement()
25
25
private val unconfinedTwoElements = Dispatchers .Unconfined + tl.asContextElement() + tl2.asContextElement()
26
26
27
+ private val elements = (0 until N ).toList()
28
+
29
+ @Param(" 0" , " 1" , " 8" , " 32" )
30
+ var channelCapacity = 0
31
+
27
32
@Benchmark
28
33
fun channelPipeline (): Int = runBlocking {
29
34
run (unconfined)
@@ -41,14 +46,14 @@ open class ChannelSinkBenchmark {
41
46
42
47
private suspend inline fun run (context : CoroutineContext ): Int {
43
48
return Channel
44
- .range(1 , 10_000 , context)
45
- .filter(context) { it % 4 == 0 }
46
- .fold(0 ) { a, b -> a + b }
49
+ .range(context) // should not allocate `Int`s!
50
+ .filter(context) { it % 4 == 0 } // should not allocate `Int`s!
51
+ .fold(0 ) { a, b -> if (a % 8 == 0 ) a else b } // should not allocate `Int`s!
47
52
}
48
53
49
- private fun Channel.Factory.range (start : Int , count : Int , context : CoroutineContext ) = GlobalScope .produce(context) {
50
- for (i in start until (start + count) )
51
- send(i)
54
+ private fun Channel.Factory.range (context : CoroutineContext ) = GlobalScope .produce(context, capacity = channelCapacity ) {
55
+ for (i in 0 until N )
56
+ send(elements[i]) // should not allocate `Int`s!
52
57
}
53
58
54
59
// Migrated from deprecated operators, are good only for stressing channels
@@ -69,3 +74,4 @@ open class ChannelSinkBenchmark {
69
74
}
70
75
}
71
76
77
+ private const val N = 10_000
0 commit comments