diff options
| author | bcoopers <coopersmithbrian@gmail.com> | 2015-03-29 19:08:53 -0400 |
|---|---|---|
| committer | bcoopers <coopersmithbrian@gmail.com> | 2015-03-29 19:08:53 -0400 |
| commit | 45c10db41f2af5919621ff69f5dc090cc917c1d3 (patch) | |
| tree | f200f094811cb05d82cd2d218c62bb41dd45aa85 /src/libstd | |
| parent | 92f3d9a6b46a116a48b0dd35b66c8f66786296d6 (diff) | |
| download | rust-45c10db41f2af5919621ff69f5dc090cc917c1d3.tar.gz rust-45c10db41f2af5919621ff69f5dc090cc917c1d3.zip | |
Clarified and simplified algorithm for increasing size of buffer in
read_to_end()
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/mod.rs | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 830a88bb6c9..3de9a068926 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -101,15 +101,14 @@ 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 len = start_len; - let mut cap_bump = 16; + let min_cap_bump = 16; let ret; loop { if len == buf.len() { if buf.capacity() == buf.len() { - if cap_bump < DEFAULT_BUF_SIZE { - cap_bump *= 2; - } - buf.reserve(cap_bump); + // reserve() rounds up our request to the nearest power of two, so after the first + // time the capacity is exceeded, we double our capacity at each call to reserve. + buf.reserve(min_cap_bump); } let new_area = buf.capacity() - buf.len(); buf.extend(iter::repeat(0).take(new_area)); |
