Skip to content

Commit

Permalink
Initialize memory for BufferedReader buffer
Browse files Browse the repository at this point in the history
It's passed to the underlying reader, so uninitialized memory == sad
times.

We might want to shrink the default buffer size as well. 64k is pretty
huge. Java uses 8k by default, and Go uses 4k for reference.
  • Loading branch information
sfackler committed Jan 13, 2015
1 parent 3d5fbae commit 89f1848
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/libstd/io/buffered.rs
Expand Up @@ -15,7 +15,7 @@
use cmp;
use fmt;
use io::{Reader, Writer, Stream, Buffer, DEFAULT_BUF_SIZE, IoResult};
use iter::{IteratorExt, ExactSizeIterator};
use iter::{IteratorExt, ExactSizeIterator, repeat};
use ops::Drop;
use option::Option;
use option::Option::{Some, None};
Expand Down Expand Up @@ -62,17 +62,11 @@ impl<R> fmt::Show for BufferedReader<R> where R: fmt::Show {
impl<R: Reader> BufferedReader<R> {
/// Creates a new `BufferedReader` with the specified buffer capacity
pub fn with_capacity(cap: uint, inner: R) -> BufferedReader<R> {
// It's *much* faster to create an uninitialized buffer than it is to
// fill everything in with 0. This buffer is entirely an implementation
// detail and is never exposed, so we're safe to not initialize
// everything up-front. This allows creation of BufferedReader instances
// to be very cheap (large mallocs are not nearly as expensive as large
// callocs).
let mut buf = Vec::with_capacity(cap);
unsafe { buf.set_len(cap); }
BufferedReader {
inner: inner,
buf: buf,
// We can't use the same trick here as we do for BufferedWriter,
// since this memory is visible to the inner Reader.
buf: repeat(0).take(cap).collect(),
pos: 0,
cap: 0,
}
Expand Down Expand Up @@ -166,7 +160,12 @@ impl<W> fmt::Show for BufferedWriter<W> where W: fmt::Show {
impl<W: Writer> BufferedWriter<W> {
/// Creates a new `BufferedWriter` with the specified buffer capacity
pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> {
// See comments in BufferedReader for why this uses unsafe code.
// It's *much* faster to create an uninitialized buffer than it is to
// fill everything in with 0. This buffer is entirely an implementation
// detail and is never exposed, so we're safe to not initialize
// everything up-front. This allows creation of BufferedWriter instances
// to be very cheap (large mallocs are not nearly as expensive as large
// callocs).
let mut buf = Vec::with_capacity(cap);
unsafe { buf.set_len(cap); }
BufferedWriter {
Expand Down

7 comments on commit 89f1848

@bors
Copy link
Contributor

@bors bors commented on 89f1848 Jan 14, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from Gankro
at sfackler@89f1848

@bors
Copy link
Contributor

@bors bors commented on 89f1848 Jan 14, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging sfackler/rust/bufferedreader-undef = 89f1848 into auto

@bors
Copy link
Contributor

@bors bors commented on 89f1848 Jan 14, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status: {"merge_sha": "d52398ef8cd93c6089ceacb176ae0dbe213d301e"}

@bors
Copy link
Contributor

@bors bors commented on 89f1848 Jan 14, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sfackler/rust/bufferedreader-undef = 89f1848 merged ok, testing candidate = d52398e

@bors
Copy link
Contributor

@bors bors commented on 89f1848 Jan 14, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d52398e

@bors
Copy link
Contributor

@bors bors commented on 89f1848 Jan 14, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d52398e

Please sign in to comment.