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

Randomize Stream::merge to improve the throughput. #503

Merged
merged 6 commits into from Nov 19, 2019
Merged

Randomize Stream::merge to improve the throughput. #503

merged 6 commits into from Nov 19, 2019

Conversation

Razican
Copy link
Contributor

@Razican Razican commented Nov 11, 2019

This implements #490. Let me know of any improvements that can be done!

@Razican
Copy link
Contributor Author

Razican commented Nov 11, 2019

Hi, so I made 3 commits. The first one was a bit of a fail. Tests passed on my laoptop because this was an unstable function.

The second commit is what I think it should look like: one function to not duplicate code, but I was getting an error saying that first/second didn't implement Stream, which they do. So I don't understand the issue.

Duplicating the code in the two branches of the if works, though.

Do you know why the second commit might be failing?

@Razican
Copy link
Contributor Author

Razican commented Nov 18, 2019

Hi, I fixed the deduplication of the code, so I think this is ready for merging.

Copy link
Member

@k-nasa k-nasa left a comment

Choose a reason for hiding this comment

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

I think this is very good!
I pointed out and asked some questions.

S: Stream<Item = I>,
{
match first.poll_next(cx) {
Poll::Ready(Some(item)) => Poll::Ready(Some(item)),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Poll::Ready(Some(item)) => Poll::Ready(Some(item)),
Poll::Ready(item) => Poll::Ready(item),

I think either is fine, but I thought it was a little redundant.
However, the above changes will not match None, so the order must be changed.
Which do you think is better?

Poll::Ready(None) => second.poll_next(cx),
Poll::Ready(item) => Poll::Ready(item),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm yes, I will review this, better if we remove redundancy. I just used the code that was already in place, but this change looks better.

Poll::Ready(Some(item)) => Poll::Ready(Some(item)),
Poll::Ready(None) => second.poll_next(cx),
Poll::Pending => match second.poll_next(cx) {
Poll::Ready(Some(item)) => Poll::Ready(Some(item)),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Poll::Ready(Some(item)) => Poll::Ready(Some(item)),
Poll::Ready(item) => Poll::Ready(item),

The same applies here

Comment on lines +1679 to +1680
lst.sort_unstable();
assert_eq!(&lst, &[1u8, 2u8, 3u8]);
Copy link
Member

Choose a reason for hiding this comment

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

👍

Comment on lines 1673 to 1677
let mut lst = Vec::new();
assert_eq!(s.next().await, Some(1u8));
assert_eq!(s.next().await, Some(2u8));
assert_eq!(s.next().await, Some(3u8));
assert_eq!(s.next().await, None);
while let Some(n) = s.next().await {
lst.push(n)
}
Copy link
Member

Choose a reason for hiding this comment

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

I think I can write like this.

use async_std::stream::FromStream;
let v = Vec::from_stream(stream).await;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this makes it more clear :)

@Razican
Copy link
Contributor Author

Razican commented Nov 19, 2019

I made the requested changes and reduced the number of match arms in the second match (two arms had the same value).

Copy link
Contributor

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

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

💯

Copy link
Member

@k-nasa k-nasa left a comment

Choose a reason for hiding this comment

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

LGTM!!

@k-nasa k-nasa merged commit 3bc4d29 into async-rs:master Nov 19, 2019
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.

None yet

3 participants