8000 Optimize `ZPipeline::mapEitherChunked` by guizmaii · Pull Request #9816 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Optimize ZPipeline::mapEitherChunked #9816

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

Open
wants to merge 1 commit into
base: series/2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions streams/shared/src/main/scala/zio/stream/ZPipeline.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1820,22 +1820,23 @@ object ZPipeline extends ZPipelinePlatformSpecificConstructors {
chunk => {
val size = chunk.size

if (size == 0) reader
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In another PR, we were talking about the fact that this Chunk can probably never be empty

else if (size == 1) {
if (size == 1) {
val a = chunk.head

f(a) match {
case r: Right[?, Out] => ZChannel.write(Chunk.single(r.value)) *> reader
case l: Left[Err, ?] => ZChannel.refailCause(Cause.fail(l.value))
}
} else {
val builder: ChunkBuilder[Out] = ChunkBuilder.make[Out](chunk.size)
val iterator: Iterator[In] = chunk.iterator
val builder: ChunkBuilder[Out] = ChunkBuilder.make[Out](size)
val iterator = chunk.chunkIterator
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about ChunkIterator

image

var index: Int = 0
var error: Err = null.asInstanceOf[Err]

while (iterator.hasNext && (error == null)) {
val a = iterator.next()
f(a) match {
while (iterator.hasNextAt(index) && (error == null)) {
Copy link
Contributor
@kyri-petrou kyri-petrou Apr 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm 99.99% sure that if you have size already stored locally you can simply iterate by checking index < size without having to call hasNextAt(index)

val in = iterator.nextAt(index)
index += 1
f(in) match {
case r: Right[?, Out] => builder.addOne(r.value)
case l: Left[Err, ?] => error = l.value
}
Expand Down
Loading
0