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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions tokio/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,77 @@ impl AsyncWrite for File {
}
}

fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[io::IoSlice<'_>],
) -> Poll<Result<usize, io::Error>> {
ready!(crate::trace::trace_leaf(cx));
let me = self.get_mut();
let inner = me.inner.get_mut();

if let Some(e) = inner.last_write_err.take() {
return Ready(Err(e.into()));
}

loop {
match inner.state {
Idle(ref mut buf_cell) => {
let mut buf = buf_cell.take().unwrap();

let seek = if !buf.is_empty() {
Some(SeekFrom::Current(buf.discard_read()))
} else {
None
};

let n = buf.copy_from_bufs(bufs);
let std = me.std.clone();

let blocking_task_join_handle = spawn_mandatory_blocking(move || {
let res = if let Some(seek) = seek {
(&*std).seek(seek).and_then(|_| buf.write_to(&mut &*std))
} else {
buf.write_to(&mut &*std)
};

(Operation::Write(res), buf)
})
.ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "background task failed")
})?;

inner.state = Busy(blocking_task_join_handle);

return Ready(Ok(n));
}
Busy(ref mut rx) => {
let (op, buf) = ready!(Pin::new(rx).poll(cx))?;
inner.state = Idle(Some(buf));

match op {
Operation::Read(_) => {
// We don't care about the result here. The fact
// that the cursor has advanced will be reflected in
// the next iteration of the loop
continue;
}
Operation::Write(res) => {
// If the previous write was successful, continue.
// Otherwise, error.
res?;
continue;
}
Operation::Seek(_) => {
// Ignore the seek
continue;
}
}
}
}
}
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
ready!(crate::trace::trace_leaf(cx));
let inner = self.inner.get_mut();
Expand Down
19 changes: 19 additions & 0 deletions tokio/src/io/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,24 @@ cfg_fs! {
self.buf.truncate(0);
ret
}

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.


let mut rem = n;
for buf in bufs {
if rem == 0 {
break
}

let len = buf.len().min(rem);
self.buf.extend_from_slice(&buf[..len]);
rem -= len;
}

n
}
}
}
35 changes: 35 additions & 0 deletions tokio/tests/fs_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![cfg(all(feature = "full", not(target_os = "wasi")))] // WASI does not support all fs operations

use std::io::prelude::*;
use std::io::IoSlice;
use tempfile::NamedTempFile;
use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt, SeekFrom};
Expand Down Expand Up @@ -49,6 +50,40 @@ async fn basic_write_and_shutdown() {
assert_eq!(file, HELLO);
}

#[tokio::test]
async fn write_vectored() {
let tempfile = tempfile();

let mut file = File::create(tempfile.path()).await.unwrap();

let ret = file
.write_vectored(&[IoSlice::new(HELLO), IoSlice::new(HELLO)])
.await
.unwrap();
assert_eq!(ret, HELLO.bytes().count() * 2);
file.flush().await.unwrap();

let file = std::fs::read(tempfile.path()).unwrap();
assert_eq!(file, [HELLO, HELLO].concat());
}

#[tokio::test]
async fn write_vectored_and_shutdown() {
let tempfile = tempfile();

let mut file = File::create(tempfile.path()).await.unwrap();

let ret = file
.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);

file.shutdown().await.unwrap();

let file = std::fs::read(tempfile.path()).unwrap();
assert_eq!(file, [HELLO, HELLO].concat());
}

#[tokio::test]
async fn rewind_seek_position() {
let tempfile = tempfile();
Expand Down