Skip to content

Commit

Permalink
Pre-allocate buffers in File::open_buffered and create_buffered
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Sep 24, 2024
1 parent eb07a61 commit 15d69c9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
10 changes: 8 additions & 2 deletions std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,10 @@ impl File {
/// ```
#[unstable(feature = "file_buffered", issue = "none")]
pub fn open_buffered<P: AsRef<Path>>(path: P) -> io::Result<io::BufReader<File>> {
File::open(path).map(io::BufReader::new)
// Allocate the buffer *first* so we don't affect the filesystem otherwise.
let buffer = io::BufReader::<Self>::try_new_buffer()?;
let file = File::open(path)?;
Ok(io::BufReader::with_buffer(file, buffer))
}

/// Opens a file in write-only mode.
Expand Down Expand Up @@ -472,7 +475,10 @@ impl File {
/// ```
#[unstable(feature = "file_buffered", issue = "none")]
pub fn create_buffered<P: AsRef<Path>>(path: P) -> io::Result<io::BufWriter<File>> {
File::create(path).map(io::BufWriter::new)
// Allocate the buffer *first* so we don't affect the filesystem otherwise.
let buffer = io::BufWriter::<Self>::try_new_buffer()?;
let file = File::create(path)?;
Ok(io::BufWriter::with_buffer(file, buffer))
}

/// Creates a new file in read-write mode; error if the file exists.
Expand Down
8 changes: 8 additions & 0 deletions std/src/io/buffered/bufreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ impl<R: Read> BufReader<R> {
BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
}

pub(crate) fn try_new_buffer() -> io::Result<Buffer> {
Buffer::try_with_capacity(DEFAULT_BUF_SIZE)
}

pub(crate) fn with_buffer(inner: R, buf: Buffer) -> Self {
Self { inner, buf }
}

/// Creates a new `BufReader<R>` with the specified buffer capacity.
///
/// # Examples
Expand Down
12 changes: 11 additions & 1 deletion std/src/io/buffered/bufreader/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! without encountering any runtime bounds checks.

use crate::cmp;
use crate::io::{self, BorrowedBuf, Read};
use crate::io::{self, BorrowedBuf, ErrorKind, Read};
use crate::mem::MaybeUninit;

pub struct Buffer {
Expand All @@ -36,6 +36,16 @@ impl Buffer {
Self { buf, pos: 0, filled: 0, initialized: 0 }
}

#[inline]
pub fn try_with_capacity(capacity: usize) -> io::Result<Self> {
match Box::try_new_uninit_slice(capacity) {
Ok(buf) => Ok(Self { buf, pos: 0, filled: 0, initialized: 0 }),
Err(_) => {
Err(io::const_io_error!(ErrorKind::OutOfMemory, "failed to allocate read buffer"))
}
}
}

#[inline]
pub fn buffer(&self) -> &[u8] {
// SAFETY: self.pos and self.cap are valid, and self.cap => self.pos, and
Expand Down
10 changes: 10 additions & 0 deletions std/src/io/buffered/bufwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ impl<W: Write> BufWriter<W> {
BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
}

pub(crate) fn try_new_buffer() -> io::Result<Vec<u8>> {
Vec::try_with_capacity(DEFAULT_BUF_SIZE).map_err(|_| {
io::const_io_error!(ErrorKind::OutOfMemory, "failed to allocate write buffer")
})
}

pub(crate) fn with_buffer(inner: W, buf: Vec<u8>) -> Self {
Self { inner, buf, panicked: false }
}

/// Creates a new `BufWriter<W>` with at least the specified buffer capacity.
///
/// # Examples
Expand Down
1 change: 1 addition & 0 deletions std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@
#![feature(slice_concat_trait)]
#![feature(thin_box)]
#![feature(try_reserve_kind)]
#![feature(try_with_capacity)]
#![feature(vec_into_raw_parts)]
// tidy-alphabetical-end
//
Expand Down

0 comments on commit 15d69c9

Please sign in to comment.