-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
fs: add read_at/write_at/seek_read/seek_write for fs::File #6427
base: master
Are you sure you want to change the base?
Conversation
620fcac
to
ba4c86a
Compare
Well, about the interface on Windows: Originally, I thought Given the different behaviors, I think we should expose them by trait? I would like to hear your thoughts before making any further changes:) |
You can provide a unix-specific method for #[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
fn my_unix_specific_method() {} The second method ensures that it is rendered properly in the documentation. Please double check the generated documentation for your code as well. You can do that either by generating it locally via the instructions in |
Thanks for that reminder of the doc cfg, I will do it soon:) |
9ca599d
to
3d548d8
Compare
Hi, this PR should be ready for review:
Ping me if you guys need me to squash my commits |
let std = self.std.clone(); | ||
let n = buf.len(); | ||
let bytes_read = asyncify(move || _read_at(&std, n, offset)).await?; | ||
let len = bytes_read.len(); | ||
buf[..len].copy_from_slice(&bytes_read); |
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.
The Tokio file already has a bunch of logic for keeping track of and reusing a buffer, but none of these functions use that logic. Is there a reason you're doing it this way?
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.
The Tokio file already has a bunch of logic for keeping track of and reusing a buffer,
Well, I am not aware of this, do you mean the bytes
crate? If not, would you like to show me some examples?
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, I mean the stuff related to these types:
Lines 96 to 118 in c98e22f
struct Inner { | |
state: State, | |
/// Errors from writes/flushes are returned in write/flush calls. If a write | |
/// error is observed while performing a read, it is saved until the next | |
/// write / flush call. | |
last_write_err: Option<io::ErrorKind>, | |
pos: u64, | |
} | |
#[derive(Debug)] | |
enum State { | |
Idle(Option<Buf>), | |
Busy(JoinHandle<(Operation, Buf)>), | |
} | |
#[derive(Debug)] | |
enum Operation { | |
Read(io::Result<usize>), | |
Write(io::Result<()>), | |
Seek(io::Result<u64>), | |
} |
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.
Thanks for showing me this!
I just took another look at other methods implemented with spawn_blocking()
, seems that I should add:
let mut inner = self.inner.lock().await;
inner.complete_inflight().await;
before involving the actual logic, right?
Update: Well, seems more complicated than I thought, I originally thought we could simply implement it using asyncify()
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.
One could probably argue either way about whether it's even desireable. One advantage of read_at
is that you can have several calls in parallel, but if we use the state logic, then they will run one-after-the-other.
I'm not sure what the best answer here is.
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.
One advantage of read_at is that you can have several calls in parallel, but if we use the state logic, then they will run one-after-the-other.
Thanks for pointing it out! The capability of enabling concurrent access is indeed the reason why pread/pwrite
are added, so I slightly tend to use separate buffers
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.
One could probably argue either way about whether it's even desireable. One advantage of
read_at
is that you can have several calls in parallel, but if we use the state logic, then they will run one-after-the-other.I'm not sure what the best answer here is.
I have a tentative plan, can we use RwLock
instead of Mutex
to achieve sharing among multiple readers and allowing them run in parallel?
Line 92 in c98e22f
inner: Mutex<Inner>, |
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.
@Chasing1020 A read/write lock doesn't help. Fundamentally, the shared buffer can only hold one piece of data at the time.
I think this would be a good feature. But what would make it really great would be if it can have platform-specific integration. For example, aio_read on FreeBSD. We need to be very careful with the initial implementation to ensure that acceleration will be possible. For example to allow for errors to be returned in the right places. Here is the existing FreeBSD version. https://docs.rs/tokio-file/latest/tokio_file/ . |
I agree and I am ok to have real async I/O for platforms that are capable of doing it |
I'm working on the platform-specific part right now. However, even though |
@asomers Can you open a new feature request for an AIO implementation of Regarding this PR, I still don't know what to do about the question of buffer management. The |
Motivation
See #1529
Solution
spawn_blocking()
, see Feature request: read_at/write_at for tokio::fs::File #1529 (comment)About the interface
These 2 methods are added directly to
tokio::fs::File
, the sync version APIs, are actually exposed through trait,FileExt
, will change it to a trait, something likeAsyncFileExt
if you want.