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

Add Read::size_hint and pre-allocate in read_to_end #45928

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ impl Read for File {
self.inner.read(buf)
}

fn size_hint(&self) -> io::Result<usize> {
let position = self.inner.seek(SeekFrom::Current(0))?;
Ok(self.metadata()?.len().saturating_sub(position) as usize)
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand All @@ -473,6 +478,10 @@ impl<'a> Read for &'a File {
self.inner.read(buf)
}

fn size_hint(&self) -> io::Result<usize> {
(**self).size_hint()
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand Down
6 changes: 6 additions & 0 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ impl<R: Read> Read for BufReader<R> {
Ok(nread)
}

#[inline]
fn size_hint(&self) -> io::Result<usize> {
let buffered_len = self.cap - self.pos;
Ok(buffered_len.saturating_add(self.inner.size_hint()?))
}

// we can't skip unconditionally because of the large buffer case in read.
unsafe fn initializer(&self) -> Initializer {
self.inner.initializer()
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ impl<T> Read for Cursor<T> where T: AsRef<[u8]> {
Ok(n)
}

fn size_hint(&self) -> io::Result<usize> {
Ok((self.inner.as_ref().len() as u64).saturating_sub(self.pos) as usize)
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand Down
15 changes: 15 additions & 0 deletions src/libstd/io/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R {
(**self).read(buf)
}

#[inline]
fn size_hint(&self) -> io::Result<usize> {
(**self).size_hint()
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
(**self).initializer()
Expand Down Expand Up @@ -92,6 +97,11 @@ impl<R: Read + ?Sized> Read for Box<R> {
(**self).read(buf)
}

#[inline]
fn size_hint(&self) -> io::Result<usize> {
(**self).size_hint()
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
(**self).initializer()
Expand Down Expand Up @@ -181,6 +191,11 @@ impl<'a> Read for &'a [u8] {
Ok(amt)
}

#[inline]
fn size_hint(&self) -> io::Result<usize> {
Ok(self.len())
}

#[inline]
unsafe fn initializer(&self) -> Initializer {
Initializer::nop()
Expand Down
53 changes: 42 additions & 11 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,17 @@ fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize>
fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
let start_len = buf.len();
let mut g = Guard { len: buf.len(), buf: buf };
let ret;

let size_hint = r.size_hint()?;
if size_hint > 0 {
unsafe {
g.buf.reserve(size_hint.saturating_add(1));
let capacity = g.buf.capacity();
g.buf.set_len(capacity);
r.initializer().initialize(&mut g.buf[g.len..]);
}
}

loop {
if g.len == g.buf.len() {
unsafe {
Expand All @@ -378,20 +388,12 @@ fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize>
}

match r.read(&mut g.buf[g.len..]) {
Ok(0) => {
ret = Ok(g.len - start_len);
break;
}
Ok(0) => return Ok(g.len - start_len),
Ok(n) => g.len += n,
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
Err(e) => {
ret = Err(e);
break;
}
Err(e) => return Err(e),
}
}

ret
}

/// The `Read` trait allows for reading bytes from a source.
Expand Down Expand Up @@ -553,6 +555,19 @@ pub trait Read {
Initializer::zeroing()
}

/// Return an estimate of how many bytes would be read from this source until EOF,
/// or zero if that is unknown.
///
/// This is used by [`read_to_end`] and [`read_to_string`] to pre-allocate a memory buffer.
///
/// [`read_to_end`]: #method.read_to_end
/// [`read_to_string`]: #method.read_to_string
#[unstable(feature = "read_size_hint", issue = /* FIXME */ "0")]
#[inline]
fn size_hint(&self) -> Result<usize> {
Ok(0)
}

/// Read all bytes until EOF in this source, placing them into `buf`.
///
/// All bytes read from this source will be appended to the specified buffer
Expand Down Expand Up @@ -1729,6 +1744,14 @@ impl<T: Read, U: Read> Read for Chain<T, U> {
self.second.read(buf)
}

fn size_hint(&self) -> Result<usize> {
if self.done_first {
self.second.size_hint()
} else {
Ok(self.first.size_hint()?.saturating_add(self.second.size_hint()?))
}
}

unsafe fn initializer(&self) -> Initializer {
let initializer = self.first.initializer();
if initializer.should_initialize() {
Expand Down Expand Up @@ -1927,6 +1950,14 @@ impl<T: Read> Read for Take<T> {
Ok(n)
}

fn size_hint(&self) -> Result<usize> {
if self.limit == 0 {
Ok(0)
} else {
Ok(cmp::min(self.limit, self.inner.size_hint()? as u64) as usize)
}
}

unsafe fn initializer(&self) -> Initializer {
self.inner.initializer()
}
Expand Down