mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-01 11:13:43 +00:00
auto merge of #18108 : mahkoh/rust/buffered_reader, r=alexcrichton
This optimizes `read` for the case in which the number of bytes requested is larger than the internal buffer. Note that the first comparison occurs again right afterwards and should thus be free. The second comparison occurs only in the cold branch.
This commit is contained in:
commit
045bc283ec
@ -104,6 +104,9 @@ impl<R: Reader> Buffer for BufferedReader<R> {
|
|||||||
|
|
||||||
impl<R: Reader> Reader for BufferedReader<R> {
|
impl<R: Reader> Reader for BufferedReader<R> {
|
||||||
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
||||||
|
if self.pos == self.cap && buf.len() >= self.buf.capacity() {
|
||||||
|
return self.inner.read(buf);
|
||||||
|
}
|
||||||
let nread = {
|
let nread = {
|
||||||
let available = try!(self.fill_buf());
|
let available = try!(self.fill_buf());
|
||||||
let nread = cmp::min(available.len(), buf.len());
|
let nread = cmp::min(available.len(), buf.len());
|
||||||
@ -409,13 +412,19 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_buffered_reader() {
|
fn test_buffered_reader() {
|
||||||
let inner = MemReader::new(vec!(0, 1, 2, 3, 4));
|
let inner = MemReader::new(vec!(5, 6, 7, 0, 1, 2, 3, 4));
|
||||||
let mut reader = BufferedReader::with_capacity(2, inner);
|
let mut reader = BufferedReader::with_capacity(2, inner);
|
||||||
|
|
||||||
let mut buf = [0, 0, 0];
|
let mut buf = [0, 0, 0];
|
||||||
let nread = reader.read(buf);
|
let nread = reader.read(buf);
|
||||||
|
assert_eq!(Ok(3), nread);
|
||||||
|
let b: &[_] = &[5, 6, 7];
|
||||||
|
assert_eq!(buf.as_slice(), b);
|
||||||
|
|
||||||
|
let mut buf = [0, 0];
|
||||||
|
let nread = reader.read(buf);
|
||||||
assert_eq!(Ok(2), nread);
|
assert_eq!(Ok(2), nread);
|
||||||
let b: &[_] = &[0, 1, 0];
|
let b: &[_] = &[0, 1];
|
||||||
assert_eq!(buf.as_slice(), b);
|
assert_eq!(buf.as_slice(), b);
|
||||||
|
|
||||||
let mut buf = [0];
|
let mut buf = [0];
|
||||||
|
Loading…
Reference in New Issue
Block a user