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

fix rechunkRandomly with large ending chunk #3277

Merged
Show file tree
Hide file tree
Changes from 3 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
46 changes: 32 additions & 14 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2383,23 +2383,41 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,

def nextSize(sourceSize: Int): Int = (factor * sourceSize).toInt

def go(acc: Chunk[O], sizeOpt: Int, s: Pull[F2, O, Unit]): Pull[F2, O, Unit] =
s.uncons.flatMap {
case Some((hd, tl)) =>
val size = if (sizeOpt > 0) sizeOpt else nextSize(hd.size)
if (acc.size < size)
go(acc ++ hd, size, tl)
else if (acc.size == size)
Pull.output(acc) >> go(hd, size, tl)
else {
val (out, rem) = acc.splitAt(size - 1)
Pull.output(out) >> go(rem ++ hd, -1, tl)
def go(
acc: Chunk[O],
sizeOpt: Int,
lastChunkSize: Int,
s: Pull[F2, O, Unit]
): Pull[F2, O, Unit] = {

val size = if (sizeOpt > 0) sizeOpt else nextSize(lastChunkSize)

if (acc.size < size)
s.uncons.flatMap {
case None => Pull.output(acc)
case Some((hd, tl)) =>
go(acc ++ hd, size, hd.size, tl)
}
else if (acc.size == size)
Pull.output(acc) >>
s.uncons.flatMap {
case None => Pull.done
case Some((hd, tl)) =>
go(hd, size, hd.size, tl)
}
case None =>
Pull.output(acc)
else {
val (out, rem) = acc.splitAt(size - 1)
Pull.output(out) >> go(rem, -1, lastChunkSize, s)

}

go(Chunk.empty, -1, underlying).stream
}

underlying.uncons.flatMap {
case None => Pull.done
Copy link

@filipwiech filipwiech Aug 15, 2023

Choose a reason for hiding this comment

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

Idea, since the new implementation is slightly more complex anyway, maybe we could avoid creation of the random object (val random = new scala.util.Random(seed)) in this empty case, since it's then not used? Or perhaps it's not worth it, not sure. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could, but I'm not sure if it's worth it. It'd only have any effect if the random rechunking was called repeatedly on empty streams, which seems like an extraordinarily niche scenario.

Anyway, I'm happy to do it if told otherwise, but I'll save the CI electricity for now

case Some((hd, tl)) =>
go(hd, -1, hd.size, tl)
}.stream
}

/** Rechunks the stream such that output chunks are within [inputChunk.size * minFactor, inputChunk.size * maxFactor].
Expand Down
10 changes: 10 additions & 0 deletions core/shared/src/test/scala/fs2/StreamCombinatorsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,16 @@ class StreamCombinatorsSuite extends Fs2Suite {
}
}
}

test("rechunkRandomlyWithSeed should correclty rechunk big chunks at the end of a stream") {
domaspoliakas marked this conversation as resolved.
Show resolved Hide resolved
val chunks = Stream
.chunk(Chunk.seq(List.fill(5000)(1)))
.rechunkRandomlyWithSeed(0.01, 0.1)(1L)
.chunks
.compile
.toList
assert(chunks.forall(_.size < 500))

Choose a reason for hiding this comment

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

Just a small comment, if I understand the documentation and the implementation of the rechunkRandomlyWithSeed correctly, then the minimum and maximum size factors are inclusive, so maybe the assertion should actually be:

Suggested change
assert(chunks.forall(_.size < 500))
assert(chunks.forall(_.size <= 500))

But I could be wrong about that. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, it should be inclusive

}
}

group("rechunkRandomly") {
Expand Down