diff options
| author | Alisdair Owens <awo101@zepler.net> | 2015-07-10 17:34:07 +0100 |
|---|---|---|
| committer | Alisdair Owens <awo101@zepler.net> | 2015-07-15 21:30:18 +0100 |
| commit | 98f287240ff9518c1ea5519c5cd03dc2ba6d4452 (patch) | |
| tree | ccf70108ba915d7280d99035f647a663ae0c1711 /src/libstd/io | |
| parent | e4e93196e16030ebf7a20c473849534235d676f8 (diff) | |
| download | rust-98f287240ff9518c1ea5519c5cd03dc2ba6d4452.tar.gz rust-98f287240ff9518c1ea5519c5cd03dc2ba6d4452.zip | |
Add specializations of read_to_end for Stdin, TcpStream and File,
allowing them to read into a buffer containing uninitialized data, rather than pay the cost of zeroing.
Diffstat (limited to 'src/libstd/io')
| -rw-r--r-- | src/libstd/io/mod.rs | 11 | ||||
| -rw-r--r-- | src/libstd/io/stdio.rs | 4 |
2 files changed, 15 insertions, 0 deletions
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 50c44299dc7..d0453c2e776 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -906,6 +906,8 @@ mod tests { use io::prelude::*; use io; use super::Cursor; + use test; + use super::repeat; #[test] fn read_until() { @@ -1024,4 +1026,13 @@ mod tests { let mut buf = [0; 1]; assert_eq!(0, R.take(0).read(&mut buf).unwrap()); } + + #[bench] + fn bench_read_to_end(b: &mut test::Bencher) { + b.iter(|| { + let mut lr = repeat(1).take(10000000); + let mut vec = Vec::with_capacity(1024); + super::read_to_end(&mut lr, &mut vec); + }); + } } diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 62bbb939a71..d8b7c8a282c 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -18,6 +18,7 @@ use io::lazy::Lazy; use io::{self, BufReader, LineWriter}; use sync::{Arc, Mutex, MutexGuard}; use sys::stdio; +use sys_common::io::{read_to_end_uninitialized}; use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard}; use libc; @@ -277,6 +278,9 @@ impl<'a> Read for StdinLock<'a> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.inner.read(buf) } + fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { + unsafe { read_to_end_uninitialized(self, buf) } + } } #[stable(feature = "rust1", since = "1.0.0")] |
