Skip to content
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

Improve performances: Remove the aggregateAsync call #751

Merged
merged 4 commits into from
Mar 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,7 @@ private[consumer] final class Runloop private (
.fromQueue(commandQueue)
.timeoutFail[Throwable](RunloopTimeout)(runloopTimeout)
.takeWhile(_ != StopRunloop)
.aggregateAsync(ZSink.collectAllN[Command](commandQueueSize))
.runFoldZIO(State.initial) { case (state, commands) =>
.runFoldChunksDiscardZIO(State.initial) { (state, commands) =>
for {
_ <- ZIO.logTrace(s"Processing ${commands.size} commands: ${commands.mkString(",")}")
isShutdown <- isShutdown
Expand All @@ -516,6 +515,26 @@ private[consumer] final class Runloop private (
}

private[consumer] object Runloop {
private implicit final class StreamOps[R, E, A](private val stream: ZStream[R, E, A]) extends AnyVal {

/**
* Inlined, simplified and specialized for our needs version of [[ZSink.foldChunksZIO]]
*
* Code initially inspired by the implementation of [[ZStream.runFoldZIO]] with everything we don't need removed and
* with chunking added
*/
def runFoldChunksDiscardZIO[R1 <: R, E1 >: E, S](s: S)(f: (S, Chunk[A]) => ZIO[R1, E1, S]): ZIO[R1, E1, Unit] = {
def reader(s: S): ZChannel[R1, E1, Chunk[A], Any, E1, Nothing, Unit] =
ZChannel.readWith(
(in: Chunk[A]) => ZChannel.fromZIO(f(s, in)).flatMap(reader),
(err: E1) => ZChannel.fail(err),
(_: Any) => ZChannel.unit
)

stream.run(ZSink.fromChannel(reader(s)))
}
}

type ByteArrayCommittableRecord = CommittableRecord[Array[Byte], Array[Byte]]

// Internal parameters, should not be necessary to tune
Expand Down