8000 Implement ZSink.collectAllN by mijicd · Pull Request #4923 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implement ZSink.collectAllN #4923

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

Merged
merged 10 commits into from
Apr 11, 2021
Merged

Implement ZSink.collectAllN #4923

merged 10 commits into from
Apr 11, 2021

Conversation

mijicd
Copy link
Member
@mijicd mijicd commented Apr 9, 2021

Part of #4886

@mijicd mijicd requested a review from iravid as a code owner April 9, 2021 14:33
@@ -346,7 +346,7 @@ object ZSink {
* A sink that collects all of its inputs into chunks of maximum size `n`.
*/
def collectAllN[Err, In](n: Int): ZSink[Any, Err, In, Err, In, Chunk[In]] =
foldUntil(Chunk.empty: Chunk[In], n.toLong)(_ appended _)
foldUntil(Chunk.empty: Chunk[In], n.toLong)(_ :+ _)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be using a ChunkBuilder here?

Copy link
Member Author
@mijicd mijicd Apr 9, 2021

Choose a reason for hiding this comment

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

Would be neat, but it's one extra step to materialize it. Should be safe otherwise.

Copy link
Member

Choose a reason for hiding this comment

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

Ah a ChunkBuilder is a good point. We can also hint it with the size so should be significantly more efficient.

Copy link
Member Author
@mijicd mijicd Apr 9, 2021

Choose a reason for hiding this comment

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

Tackled in 0427c09.

Edit: scratch this, it's not good.

Copy link
Member Author

Choose a reason for hiding this comment

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

Here's the updated version: 375647a

@@ -342,6 +342,14 @@ object ZSink {
new ZSink(loop(Chunk.empty))
}

/**
* A sink that collects all of its inputs into chunks of maximum size `n`.
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add a note that every chunk is preallocated and must fit in memory? Or @adamgfraser should we use the same approach you arrived at with regards to the size hint? (Which I don't remember what it was 😅)

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the remark.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I've come to the point of view that we should just hint the specified size if an operator allows specifying a particular chunk size. I think we have to do that for maximum efficiency and it is just user error if you specify a chunk size like Int.MaxValue.

I was a little hesitant on this because of an issue we had with someone blowing themselves up using queue.takeUpTo(Int.MaxValue) but I think that is different in that there the n is how many elements to take and chunking is an implementation detail, whereas here the whole point of the parameter is to specify the size of the chunk to take.

@mijicd mijicd mentioned this pull request Apr 9, 2021
@mijicd mijicd force-pushed the feature/collectallN branch from e810e5a to b8f6ea9 Compare April 10, 2021 21:33
@@ -20,6 +20,14 @@ object ZSinkSpec extends ZIOBaseSpec {
def spec: ZSpec[Environment, Failure] = {
suite("ZSinkSpec")(
suite("Constructors")(
suite("collectAllN")(
Copy link
Member

Choose a reason for hiding this comment

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

Same re tests from ZTransducerSpec :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 239c554. As discussed over chat, we have to leave with trailing chunks, therefore the nonapplicable tests are dropped.

@mijicd mijicd force-pushed the feature/collectallN branch from b8f6ea9 to 239c554 Compare April 11, 2021 08:46
@mijicd mijicd requested a review from iravid April 11, 2021 09:59
@iravid iravid merged commit 7989754 into zio:series/2.x Apr 11, 2021
@mijicd mijicd deleted the feature/collectallN branch April 11, 2021 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0