Skip to content

Commit

Permalink
Make the fastPublisher in the FlowInterop benchmark a sequential one
Browse files Browse the repository at this point in the history
  • Loading branch information
BalmungSan committed Oct 21, 2024
1 parent 3452b3e commit 51a7351
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions benchmark/src/main/scala/fs2/benchmark/FlowInteropBenchmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@ import org.openjdk.jmh.annotations.{

import java.util.concurrent.TimeUnit
import java.util.concurrent.Flow.{Publisher, Subscriber, Subscription}

import scala.concurrent.Future
import scala.annotation.elidable

@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class FlowInteropBenchmark {
@Param(Array("1024", "5120", "10240"))
var totalElements: Int = _
var totalElements: Long = _

@Param(Array("1000"))
var iterations: Int = _
var iterations: Long = _

@Benchmark
def fastPublisher(): Unit = {
Expand All @@ -57,25 +56,21 @@ class FlowInteropBenchmark {
override final def subscribe(subscriber: Subscriber[? >: Unit]): Unit =
subscriber.onSubscribe(
new Subscription {
@volatile var i: Int = 0
var i: Long = 0
@volatile var canceled: Boolean = false

// Sequential fast Publisher.
override final def request(n: Long): Unit = {
Future {
var j = 0
while ((j < n) && (i < totalElements) && !canceled) {
subscriber.onNext(())
i += 1
j += 1
}
val elementsToProduce = math.min(i + n, totalElements)

if (i == totalElements || canceled) {
subscriber.onComplete()
}
}(global.compute)
while (i < elementsToProduce) {
subscriber.onNext(())
i += 1
}

// Discarding the Future so it runs in the background.
()
if (i == totalElements || canceled) {
subscriber.onComplete()
}
}

override final def cancel(): Unit =
Expand Down

0 comments on commit 51a7351

Please sign in to comment.