Skip to content

Commit 960e6c9

Browse files
committed
detect EOF earlier
The initial probe-for-empty-source by stack_buffer_copy only detected EOF if the source was empty but not when it was merely small which lead to additional calls to read() after Ok(0) had already been returned in the stack copy routine
1 parent a0c55a6 commit 960e6c9

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

std/src/io/copy.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,13 @@ impl<A: Allocator> BufferedWriterSpec for Vec<u8, A> {
264264
fn copy_from<R: Read + ?Sized>(&mut self, reader: &mut R) -> Result<u64> {
265265
let mut bytes = 0;
266266

267-
// avoid allocating before we have determined that there's anything to read
268-
if self.capacity() == 0 {
269-
bytes = stack_buffer_copy(&mut reader.take(DEFAULT_BUF_SIZE as u64), self)?;
270-
if bytes == 0 {
271-
return Ok(0);
267+
// avoid inflating empty/small vecs before we have determined that there's anything to read
268+
if self.capacity() < DEFAULT_BUF_SIZE {
269+
let stack_read_limit = DEFAULT_BUF_SIZE as u64;
270+
bytes = stack_buffer_copy(&mut reader.take(stack_read_limit), self)?;
271+
// fewer bytes than requested -> EOF reached
272+
if bytes < stack_read_limit {
273+
return Ok(bytes);
272274
}
273275
}
274276

0 commit comments

Comments
 (0)