Merge pull request #364 from stjepang/optimize-flushing

Don't flush files if they weren't written to
pull/401/head
Yoshua Wuyts 5 years ago committed by GitHub
commit e5675151b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -66,6 +66,23 @@ pub struct File {
} }
impl File { impl File {
/// Creates an async file handle.
pub(crate) fn new(file: std::fs::File, is_flushed: bool) -> File {
let file = Arc::new(file);
File {
file: file.clone(),
lock: Lock::new(State {
file,
mode: Mode::Idle,
cache: Vec::new(),
is_flushed,
last_read_err: None,
last_write_err: None,
}),
}
}
/// Opens a file in read-only mode. /// Opens a file in read-only mode.
/// ///
/// See the [`OpenOptions::open`] function for more options. /// See the [`OpenOptions::open`] function for more options.
@ -96,7 +113,7 @@ impl File {
pub async fn open<P: AsRef<Path>>(path: P) -> io::Result<File> { pub async fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
let path = path.as_ref().to_owned(); let path = path.as_ref().to_owned();
let file = blocking::spawn(move || std::fs::File::open(&path)).await?; let file = blocking::spawn(move || std::fs::File::open(&path)).await?;
Ok(file.into()) Ok(File::new(file, true))
} }
/// Opens a file in write-only mode. /// Opens a file in write-only mode.
@ -131,7 +148,7 @@ impl File {
pub async fn create<P: AsRef<Path>>(path: P) -> io::Result<File> { pub async fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
let path = path.as_ref().to_owned(); let path = path.as_ref().to_owned();
let file = blocking::spawn(move || std::fs::File::create(&path)).await?; let file = blocking::spawn(move || std::fs::File::create(&path)).await?;
Ok(file.into()) Ok(File::new(file, true))
} }
/// Synchronizes OS-internal buffered contents and metadata to disk. /// Synchronizes OS-internal buffered contents and metadata to disk.
@ -383,19 +400,7 @@ impl Seek for &File {
impl From<std::fs::File> for File { impl From<std::fs::File> for File {
fn from(file: std::fs::File) -> File { fn from(file: std::fs::File) -> File {
let file = Arc::new(file); File::new(file, false)
File {
file: file.clone(),
lock: Lock::new(State {
file,
mode: Mode::Idle,
cache: Vec::new(),
is_flushed: false,
last_read_err: None,
last_write_err: None,
}),
}
} }
} }

@ -284,7 +284,10 @@ impl OpenOptions {
pub fn open<P: AsRef<Path>>(&self, path: P) -> impl Future<Output = io::Result<File>> { pub fn open<P: AsRef<Path>>(&self, path: P) -> impl Future<Output = io::Result<File>> {
let path = path.as_ref().to_owned(); let path = path.as_ref().to_owned();
let options = self.0.clone(); let options = self.0.clone();
async move { blocking::spawn(move || options.open(path).map(|f| f.into())).await } async move {
let file = blocking::spawn(move || options.open(path)).await?;
Ok(File::new(file, true))
}
} }
} }

Loading…
Cancel
Save