-
Notifications
You must be signed in to change notification settings - Fork 69
Stream and Pipe benchmarks #1300
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
Draft
johnhungerford
wants to merge
3
commits into
getkyo:main
Choose a base branch
from
johnhungerford:pipe-benchmark
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
196 changes: 196 additions & 0 deletions
196
kyo-bench/src/main/scala/kyo/bench/arena/StreamPipeBench.scala
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,196 @@ | ||
package kyo.bench.arena | ||
|
||
import WarmupJITProfile.* | ||
import kyo.AllowUnsafe | ||
import kyo.bench.BaseBench | ||
import org.openjdk.jmh.annotations.* | ||
|
||
class StreamPipeBench extends BaseBench: | ||
val seq = (0 until 10000).toVector | ||
|
||
import AllowUnsafe.embrace.danger | ||
|
||
@Benchmark | ||
def mapPureStreamBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.mapPure(_ + 1) | ||
.fold(0)(_ + _) | ||
.eval | ||
end mapPureStreamBench | ||
|
||
@Benchmark | ||
def mapPureStreamAbstractBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.mapAbstractPure(_ + 1) | ||
.fold(0)(_ + _) | ||
.eval | ||
end mapPureStreamAbstractBench | ||
|
||
@Benchmark | ||
def mapPurePipeBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.into(Pipe.mapPure((_: Int) + 1)) | ||
.fold(0)(_ + _) | ||
.eval | ||
end mapPurePipeBench | ||
|
||
@Benchmark | ||
def mapPurePipeAbstractBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.into(Pipe.mapAbstractPure((_: Int) + 1)) | ||
.fold(0)(_ + _) | ||
.eval | ||
end mapPurePipeAbstractBench | ||
|
||
@Benchmark | ||
def mapKyoStreamBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.map(v => Sync(v + 1)) | ||
.fold(0)(_ + _) | ||
.handle(Sync.Unsafe.evalOrThrow) | ||
end mapKyoStreamBench | ||
|
||
@Benchmark | ||
def mapKyoStreamAbstractBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.mapAbstract(v => Sync(v + 1)) | ||
.fold(0)(_ + _) | ||
.handle(Sync.Unsafe.evalOrThrow) | ||
end mapKyoStreamAbstractBench | ||
|
||
@Benchmark | ||
def mapKyoPipeBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.into(Pipe.map((v: Int) => Sync(v + 1))) | ||
.fold(0)(_ + _) | ||
.handle(Sync.Unsafe.evalOrThrow) | ||
end mapKyoPipeBench | ||
|
||
@Benchmark | ||
def mapKyoPipeAbstractBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.into(Pipe.mapAbstract((v: Int) => Sync(v + 1))) | ||
.fold(0)(_ + _) | ||
.handle(Sync.Unsafe.evalOrThrow) | ||
end mapKyoPipeAbstractBench | ||
|
||
@Benchmark | ||
def filterPureStreamBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.filterPure(_ % 2 == 0) | ||
.fold(0)(_ + _) | ||
.eval | ||
end filterPureStreamBench | ||
|
||
@Benchmark | ||
def filterPureStreamAbstractBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.filterAbstractPure(_ % 2 == 0) | ||
.fold(0)(_ + _) | ||
.eval | ||
end filterPureStreamAbstractBench | ||
|
||
@Benchmark | ||
def filterPurePipeBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.into(Pipe.filterPure((_: Int) % 2 == 0)) | ||
.fold(0)(_ + _) | ||
.eval | ||
end filterPurePipeBench | ||
|
||
@Benchmark | ||
def filterPurePipeAbstractBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.into(Pipe.filterAbstractPure((_: Int) % 2 == 0)) | ||
.fold(0)(_ + _) | ||
.eval | ||
end filterPurePipeAbstractBench | ||
|
||
@Benchmark | ||
def filterKyoStreamBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.filter(v => Sync(v % 2 == 0)) | ||
.fold(0)(_ + _) | ||
.handle(Sync.Unsafe.evalOrThrow) | ||
end filterKyoStreamBench | ||
|
||
@Benchmark | ||
def filterKyoStreamAbstractBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.filterAbstract(v => Sync(v % 2 == 0)) | ||
.fold(0)(_ + _) | ||
.handle(Sync.Unsafe.evalOrThrow) | ||
end filterKyoStreamAbstractBench | ||
|
||
@Benchmark | ||
def filterKyoPipeBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.into(Pipe.filter((v: Int) => Sync(v % 2 == 0))) | ||
.fold(0)(_ + _) | ||
.handle(Sync.Unsafe.evalOrThrow) | ||
end filterKyoPipeBench | ||
|
||
@Benchmark | ||
def filterKyoPipeAbstractBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.into(Pipe.filterAbstract((v: Int) => Sync(v % 2 == 0))) | ||
.fold(0)(_ + _) | ||
.handle(Sync.Unsafe.evalOrThrow) | ||
end filterKyoPipeAbstractBench | ||
|
||
@Benchmark | ||
def filterMapVarStreamBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.filter(v => Var.get[Int].map(i => ((i + v) % 2 > -1))) | ||
.map(v => Var.update[Int](_ + 1).map(i => v + i)) | ||
.fold(0)(_ + _) | ||
.handle(Var.run(0), Sync.Unsafe.evalOrThrow) | ||
end filterMapVarStreamBench | ||
|
||
@Benchmark | ||
def filterMapVarStreamAbstractBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.filterAbstract(v => Var.get[Int].map(i => ((i + v) % 2 > -1))) | ||
.mapAbstract(v => Var.update[Int](_ + 1).map(i => v + i)) | ||
.fold(0)(_ + _) | ||
.handle(Var.run(0), Sync.Unsafe.evalOrThrow) | ||
end filterMapVarStreamAbstractBench | ||
|
||
@Benchmark | ||
def filterMapVarPipeBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.into(Pipe.filter((v: Int) => Var.get[Int].map(i => ((i + v) % 2 > -1)))) | ||
.into(Pipe.map((v: Int) => Var.update[Int](_ + 1).map(i => v + i))) | ||
.fold(0)(_ + _) | ||
.handle(Var.run(0), Sync.Unsafe.evalOrThrow) | ||
end filterMapVarPipeBench | ||
|
||
@Benchmark | ||
def filterMapVarPipeAbstractBench() = | ||
import kyo.* | ||
Stream.init(seq) | ||
.into(Pipe.filterAbstract((v: Int) => Var.get[Int].map(i => ((i + v) % 2 > -1)))) | ||
.into(Pipe.mapAbstract((v: Int) => Var.update[Int](_ + 1).map(i => v + i))) | ||
.fold(0)(_ + _) | ||
.handle(Var.run(0), Sync.Unsafe.evalOrThrow) | ||
end filterMapVarPipeAbstractBench | ||
end StreamPipeBench |
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
39 changes: 39 additions & 0 deletions
39
kyo-prelude/shared/src/main/scala/kyo/StreamTransformations.scala
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,39 @@ | ||
package kyo | ||
|
||
private[kyo] object StreamTransformations: | ||
inline def handleMap[V1, B, V2, S, S2](chunk: Chunk[V1], cont: => B < S)(f: V1 => V2 < S2)(using | ||
Tag[Emit[Chunk[V2]]], | ||
Frame | ||
): B < (S & S2 & Emit[Chunk[V2]]) = | ||
Kyo.foreach(chunk)(f).map: newChunk => | ||
if newChunk.isEmpty then cont | ||
else Emit.valueWith(newChunk)(cont) | ||
|
||
inline def handleMapPure[V1, B, V2, S](chunk: Chunk[V1], cont: => B < S)(f: V1 => V2)(using | ||
Tag[Emit[Chunk[V2]]], | ||
Frame | ||
): B < (S & Emit[Chunk[V2]]) = | ||
val newChunk = chunk.map(f) | ||
if newChunk.isEmpty then cont | ||
else Emit.valueWith(newChunk)(cont) | ||
end handleMapPure | ||
|
||
inline def handleFilter[V1, B, S, S2](chunk: Chunk[V1], cont: => B < S)(f: V1 => Boolean < S2)( | ||
using | ||
Tag[Emit[Chunk[V1]]], | ||
Frame | ||
): B < (S & S2 & Emit[Chunk[V1]]) = | ||
Kyo.filter(chunk)(f).map: newChunk => | ||
if newChunk.isEmpty then cont | ||
else Emit.valueWith(newChunk)(cont) | ||
|
||
inline def handleFilterPure[V1, B, S](chunk: Chunk[V1], cont: => B < S)(f: V1 => Boolean)( | ||
using | ||
Tag[Emit[Chunk[V1]]], | ||
Frame | ||
): B < (S & Emit[Chunk[V1]]) = | ||
val newChunk = chunk.filter(f) | ||
if newChunk.isEmpty then cont | ||
else Emit.valueWith(newChunk)(cont) | ||
end handleFilterPure | ||
end StreamTransformations |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
the
arena
package is for comparative benchmarks, this new one should be under thebench
package directly