-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
06a750d
first draft
maminrayej cb53930
do not return result
maminrayej 619728f
add test for write_vectored on files
maminrayej d4c480a
fix clippy error
maminrayej 6612764
resolve comments
maminrayej 49b8961
indicate that File has an efficient poll_write_vectored impl
maminrayej File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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}; | ||||||
|
@@ -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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is in the test twice.
Suggested change
|
||||||
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(); | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean
b.len()
isO(n)
and causes thebufs.iter()
to beO(n^2)
? I thought since it's aDeref
to&[u8]
, it'sO(1)
.I've provided an alternate implementation that doesn't use
bufs.iter()
.There was a problem hiding this comment.
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 ofbufs
, which you iterate over.