Skip to content

Commit 0fdf2ac

Browse files
committed
refactoring + removing binaryen gufa flag for now
1 parent b14a8d7 commit 0fdf2ac

File tree

8 files changed

+63
-46
lines changed

8 files changed

+63
-46
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ tasks.withType<org.jetbrains.kotlin.gradle.targets.wasm.d8.D8Exec>().configureEa
109109
}
110110

111111
tasks.withType<org.jetbrains.kotlin.gradle.targets.wasm.binaryen.BinaryenExec>().configureEach {
112+
val filteredArgs = binaryenArgs.filter { it != "--gufa" }
113+
binaryenArgs = filteredArgs.toMutableList()
112114
binaryenArgs.add("--enable-stack-switching")
113115
binaryenArgs.add("--enable-multivalue")
114116
}

src/commonMain/kotlin/macroBenchmarks/MacroBenchmarks.kt

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
package macroBenchmarks
1818

1919
import kotlinx.benchmark.*
20-
import macroBenchmarks.coroutinesFastBenchmarks.SharedFlowBaseline
21-
import macroBenchmarks.coroutinesFastBenchmarks.SimpleChannelBenchmark
20+
import macroBenchmarks.coroutinesSlowBenchmarks.Coroutines
2221

2322
open class MacroBenchmarksBase {
2423
protected fun runBenchmark(macroBenchmark: MacroBenchmark) {
@@ -102,24 +101,4 @@ class MacroBenchmarksFast : MacroBenchmarksBase() {
102101
fun towers() {
103102
runBenchmark(Towers())
104103
}
105-
106-
@Benchmark
107-
fun cancellableChannelBenchmark() {
108-
runBenchmark(SimpleChannelBenchmark.CancellableChannelBenchmark())
109-
}
110-
111-
@Benchmark
112-
fun cancellableReusableChannelBenchmark() {
113-
runBenchmark(SimpleChannelBenchmark.CancellableReusableChannelBenchmark())
114-
}
115-
116-
@Benchmark
117-
fun nonCancellableChannelBenchmark() {
118-
runBenchmark(SimpleChannelBenchmark.NonCancellableChannelBenchmark())
119-
}
120-
121-
@Benchmark
122-
fun sharedFlowBaseline() {
123-
runBenchmark(SharedFlowBaseline())
124-
}
125104
}

src/commonMain/kotlin/macroBenchmarks/Coroutines.kt renamed to src/commonMain/kotlin/macroBenchmarks/coroutinesSlowBenchmarks/Coroutines.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package macroBenchmarks
1+
package macroBenchmarks.coroutinesSlowBenchmarks
22

3+
import macroBenchmarks.MacroBenchmark
34
import macroBenchmarks.coroutines.AbstractGenerator
45
import macroBenchmarks.coroutines.ClosedRangeGenerator
56
import macroBenchmarks.coroutines.CoroutineStub

src/commonMain/kotlin/macroBenchmarks/coroutinesSlowBenchmarks/LaunchBenchmark.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import macroBenchmarks.coroutines.ParametrizedDispatcherBase
77
import kotlin.coroutines.Continuation
88
import kotlin.coroutines.startCoroutine
99

10+
/*
11+
* Adapted benchmark from kotlinx.coroutines
12+
* https://github.com/Kotlin/kotlinx.coroutines/blob/master/benchmarks/src/jmh/kotlin/benchmarks/scheduler/LaunchBenchmark.kt
13+
* Probably, no need of FJP-like adaptation, cause in K/Wasm no multithreading?
14+
*
15+
* Used CyclicBarrier from arrow-kt as an alternative to java CyclicBarrier
16+
*/
1017
@State(Scope.Benchmark)
1118
open class LaunchBenchmark : ParametrizedDispatcherBase() {
1219

src/commonMain/kotlin/macroBenchmarks/coroutinesSlowBenchmarks/SelectBenchmark.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import kotlinx.benchmark.*
99
import macroBenchmarks.coroutines.ParametrizedDispatcherBase
1010
import kotlin.concurrent.Volatile
1111

12+
/*
13+
* Adapted benchmark from kotlinx.coroutines
14+
* https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/benchmarks/jvm/kotlin/kotlinx/coroutines/channels/SelectBenchmark.kt
15+
*/
1216
@State(Scope.Benchmark)
1317
open class SelectBenchmark: ParametrizedDispatcherBase() {
1418
// 450

src/commonMain/kotlin/macroBenchmarks/coroutinesSlowBenchmarks/SemaphoreBenchmark.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ private fun doGeomDistrWork(work: Int) {
1717
// are distributed geometrically, see https://github.com/Kotlin/kotlinx.coroutines/pull/1464#discussion_r355705325
1818
val p = 1.0 / work
1919

20-
// used to have ThreadLocal to avoid contention, but there is no multithreading in K/Wasm?
20+
// Used to have ThreadLocal to avoid contention, but there is no multithreading in K/Wasm?
2121
// val r = ThreadLocalRandom.current()
2222
val r = Random
2323
while (true) {
2424
if (r.nextDouble() < p) break
2525
}
2626
}
2727

28+
/*
29+
* Adapted benchmark from kotlinx.coroutines
30+
* https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/benchmarks/jvm/kotlin/kotlinx/coroutines/SemaphoreBenchmark.kt
31+
*/
2832
@State(Scope.Benchmark)
2933
open class SemaphoreBenchmark: ParametrizedDispatcherBase() {
3034

31-
// with params both stack-switching and non-stack-switching versions fail
35+
// With params both stack-switching and non-stack-switching versions fail
3236
// @Param("0", "1000")
3337
var coroutines: Int = 1000
3438

src/commonMain/kotlin/macroBenchmarks/coroutinesFastBenchmarks/SharedFlowBaseline.kt renamed to src/commonMain/kotlin/macroBenchmarks/coroutinesSlowBenchmarks/SharedFlowBaseline.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
package macroBenchmarks.coroutinesFastBenchmarks
1+
package macroBenchmarks.coroutinesSlowBenchmarks
22

33
import kotlinx.coroutines.flow.MutableSharedFlow
44
import kotlinx.coroutines.flow.take
55
import kotlinx.coroutines.launch
6-
import macroBenchmarks.coroutines.ParametrizedDispatcherBaseSlow
6+
import macroBenchmarks.coroutines.ParametrizedDispatcherBase
77
import kotlin.coroutines.Continuation
88
import kotlin.coroutines.startCoroutine
9+
import kotlinx.benchmark.*
910

10-
// Stresses out 'synchronized' codepath in MutableSharedFlow
11-
open class SharedFlowBaseline : ParametrizedDispatcherBaseSlow() {
11+
/* Adapted benchmark from kotlinx.coroutines
12+
* https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/benchmarks/main/kotlin/SharedFlowBaseline.kt
13+
* Stresses out 'synchronized' codepath in MutableSharedFlow
14+
*/
15+
@State(Scope.Benchmark)
16+
open class SharedFlowBaseline : ParametrizedDispatcherBase() {
1217

1318
private var size: Int = 10_000
1419

15-
override fun verifyResult(result: Any) = (result is Int) && result == size * (size - 1) / 2
16-
17-
override fun benchmark(): Any {
20+
@Benchmark
21+
fun baseline() {
1822
var sum = 0
1923
var done = false
2024
suspend {
@@ -27,6 +31,6 @@ open class SharedFlowBaseline : ParametrizedDispatcherBaseSlow() {
2731
}.startCoroutine(Continuation(coroutineContext) { it.getOrThrow() })
2832
coroutineContext.drain()
2933
check(done) { "benchmark did not complete" }
30-
return sum
34+
check(sum == size * (size - 1) / 2) { "benchmark did not complete $sum" }
3135
}
32-
}
36+
}

src/commonMain/kotlin/macroBenchmarks/coroutinesFastBenchmarks/SimpleChannelBenchmark.kt renamed to src/commonMain/kotlin/macroBenchmarks/coroutinesSlowBenchmarks/SimpleChannelBenchmark.kt

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package macroBenchmarks.coroutinesFastBenchmarks
1+
package macroBenchmarks.coroutinesSlowBenchmarks
22

33
import kotlinx.coroutines.launch
44
import macroBenchmarks.coroutines.CancellableChannel
@@ -9,9 +9,13 @@ import macroBenchmarks.coroutines.SimpleChannel
99
import kotlin.concurrent.Volatile
1010
import kotlin.coroutines.Continuation
1111
import kotlin.coroutines.startCoroutine
12+
import kotlinx.benchmark.*
1213

13-
// Stresses out 'synchronized' codepath in MutableSharedFlow
14-
sealed class SimpleChannelBenchmark : ParametrizedDispatcherBaseSlow() {
14+
/*
15+
* Adapted benchmark from kotlinx.coroutines
16+
* https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/benchmarks/jvm/kotlin/kotlinx/coroutines/channels/SimpleChannelBenchmark.kt
17+
*/
18+
abstract class SimpleChannelBenchmark : ParametrizedDispatcherBaseSlow() {
1519

1620
private val iterations = 10_000
1721
protected abstract fun makeChannel(): SimpleChannel
@@ -39,16 +43,28 @@ sealed class SimpleChannelBenchmark : ParametrizedDispatcherBaseSlow() {
3943
check(done) { "benchmark did not complete" }
4044
return sink
4145
}
46+
}
4247

43-
class CancellableChannelBenchmark: SimpleChannelBenchmark() {
44-
override fun makeChannel() = CancellableChannel()
45-
}
48+
@State(Scope.Benchmark)
49+
class CancellableChannelBenchmark: SimpleChannelBenchmark() {
50+
override fun makeChannel() = CancellableChannel()
4651

47-
class CancellableReusableChannelBenchmark: SimpleChannelBenchmark() {
48-
override fun makeChannel() = CancellableReusableChannel()
49-
}
52+
@Benchmark
53+
fun cancellable() = benchmark()
54+
}
5055

51-
class NonCancellableChannelBenchmark: SimpleChannelBenchmark() {
52-
override fun makeChannel() = NonCancellableChannel()
53-
}
56+
@State(Scope.Benchmark)
57+
class CancellableReusableChannelBenchmark: SimpleChannelBenchmark() {
58+
override fun makeChannel() = CancellableReusableChannel()
59+
60+
@Benchmark
61+
fun cancellableReusable() = benchmark()
5462
}
63+
64+
@State(Scope.Benchmark)
65+
class NonCancellableChannelBenchmark: SimpleChannelBenchmark() {
66+
override fun makeChannel() = NonCancellableChannel()
67+
68+
@Benchmark
69+
fun nonCancellable() = benchmark()
70+
}

0 commit comments

Comments
 (0)