From 45c10db41f2af5919621ff69f5dc090cc917c1d3 Mon Sep 17 00:00:00 2001 From: bcoopers Date: Sun, 29 Mar 2015 19:08:53 -0400 Subject: [PATCH 1/4] Clarified and simplified algorithm for increasing size of buffer in read_to_end() --- src/libstd/io/mod.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 830a88bb6c95b..3de9a06892627 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -101,15 +101,14 @@ fn append_to_string(buf: &mut String, f: F) -> Result fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { 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)); From 2982fe39ad93e29709a2e1414a4228718c8de28a Mon Sep 17 00:00:00 2001 From: bcoopers Date: Sun, 29 Mar 2015 19:23:46 -0400 Subject: [PATCH 2/4] 80 character line limit --- src/libstd/io/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 3de9a06892627..bc3791b99d07a 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -106,8 +106,9 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result loop { if len == buf.len() { if buf.capacity() == buf.len() { - // 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. + // 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(); From 8d3e55908ae0e51f04c170133c9f9739886b8e2e Mon Sep 17 00:00:00 2001 From: bcoopers Date: Sun, 29 Mar 2015 19:29:11 -0400 Subject: [PATCH 3/4] Clearer wording --- src/libstd/io/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index bc3791b99d07a..c4ec2a7ca17f9 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -106,9 +106,9 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result loop { if len == buf.len() { if buf.capacity() == buf.len() { - // 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. + // reserve() rounds up our request such that every request + // (with maybe the exception of the first request) for the + // same amount of space doubles our capacity. buf.reserve(min_cap_bump); } let new_area = buf.capacity() - buf.len(); From 240734c31e529557583a0dc8e97abf858b4a375d Mon Sep 17 00:00:00 2001 From: bcoopers Date: Mon, 30 Mar 2015 13:59:32 -0400 Subject: [PATCH 4/4] Only zero at most 64k at a time. We still use the doubling reallocation strategy since extend() calls reserve() and/or push() for us. --- src/libstd/io/mod.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index c4ec2a7ca17f9..6b03fb45c779d 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -101,18 +101,14 @@ fn append_to_string(buf: &mut String, f: F) -> Result fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { let start_len = buf.len(); let mut len = start_len; - let min_cap_bump = 16; + let mut new_write_size = 16; let ret; loop { if len == buf.len() { - if buf.capacity() == buf.len() { - // reserve() rounds up our request such that every request - // (with maybe the exception of the first request) for the - // same amount of space doubles our capacity. - buf.reserve(min_cap_bump); + if new_write_size < DEFAULT_BUF_SIZE { + new_write_size *= 2; } - let new_area = buf.capacity() - buf.len(); - buf.extend(iter::repeat(0).take(new_area)); + buf.extend(iter::repeat(0).take(new_write_size)); } match r.read(&mut buf[len..]) {