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

Implement vectored write functionality for files #5958

Merged
merged 6 commits into from Aug 29, 2023

Conversation

maminrayej
Copy link
Member

The poll_write_vectored function for File currently uses the default implementation in AsyncWrite, which only writes the first non-empty buffer in bufs. This PR adds proper vectored write support for files.

Motivation

Solution

This implementation copies the maximum permissible data (as determined by the MAX_BUF constant) from the buffers to the internal buffer of File. All other behavior aligns with the existing poll_write method.

Resolves Issue #5949.

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-fs Module: tokio/fs labels Aug 29, 2023
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Overall, looks reasonable to me. I have a few nits:

.write_vectored(&[IoSlice::new(HELLO), IoSlice::new(HELLO)])
.await
.unwrap();
assert_eq!(ret, HELLO.bytes().count() * 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is in the test twice.

Suggested change
assert_eq!(ret, HELLO.bytes().count() * 2);
assert_eq!(ret, HELLO.len() * 2);

pub(crate) fn copy_from_bufs(&mut self, bufs: &[io::IoSlice<'_>]) -> usize {
assert!(self.is_empty());

let n = bufs.iter().map(|b| b.len()).sum::<usize>().min(MAX_BUF);
Copy link
Contributor

Choose a reason for hiding this comment

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

This iterates all of the buffers every time, even if we only write a few of them. If the buffers are very long and this is called in a loop, that gives quadratic performance.

We should be able to embed this logic inside the for loop instead to avoid that.

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean b.len() is O(n) and causes the bufs.iter() to be O(n^2)? I thought since it's a Deref to &[u8], it's O(1).
I've provided an alternate implementation that doesn't use bufs.iter().

Copy link
Contributor

Choose a reason for hiding this comment

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

No, b.len() is constant time. Instead, it's O(n) in the length of bufs, which you iterate over.

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

@Darksonn Darksonn merged commit 37bb47c into tokio-rs:master Aug 29, 2023
70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-fs Module: tokio/fs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants