Skip to content

Commit 68f2934

Browse files
authored
Rollup merge of #82728 - calebsander:refactor/bufreader-buf, r=m-ou-se
Avoid unnecessary Vec construction in BufReader As mentioned in #80460, creating a `Vec` and calling `Vec::into_boxed_slice()` emits unnecessary calls to `realloc()` and `free()`. Updated the code to use `Box::new_uninit_slice()` to create a boxed slice directly. I think this also makes it more explicit that the initial contents of the buffer are uninitialized. r? ``@m-ou-se``
2 parents ee796c6 + 9425e30 commit 68f2934

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

library/std/src/io/buffered/bufreader.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ impl<R: Read> BufReader<R> {
9292
#[stable(feature = "rust1", since = "1.0.0")]
9393
pub fn with_capacity(capacity: usize, inner: R) -> BufReader<R> {
9494
unsafe {
95-
let mut buffer = Vec::with_capacity(capacity);
96-
buffer.set_len(capacity);
97-
inner.initializer().initialize(&mut buffer);
98-
BufReader { inner, buf: buffer.into_boxed_slice(), pos: 0, cap: 0 }
95+
let mut buf = Box::new_uninit_slice(capacity).assume_init();
96+
inner.initializer().initialize(&mut buf);
97+
BufReader { inner, buf, pos: 0, cap: 0 }
9998
}
10099
}
101100
}

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@
289289
#![feature(needs_panic_runtime)]
290290
#![feature(negative_impls)]
291291
#![feature(never_type)]
292+
#![feature(new_uninit)]
292293
#![feature(nll)]
293294
#![feature(nonnull_slice_from_raw_parts)]
294295
#![feature(once_cell)]

0 commit comments

Comments
 (0)