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``
This commit is contained in:
commit
68f2934a15
@ -92,10 +92,9 @@ impl<R: Read> BufReader<R> {
|
|||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn with_capacity(capacity: usize, inner: R) -> BufReader<R> {
|
pub fn with_capacity(capacity: usize, inner: R) -> BufReader<R> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut buffer = Vec::with_capacity(capacity);
|
let mut buf = Box::new_uninit_slice(capacity).assume_init();
|
||||||
buffer.set_len(capacity);
|
inner.initializer().initialize(&mut buf);
|
||||||
inner.initializer().initialize(&mut buffer);
|
BufReader { inner, buf, pos: 0, cap: 0 }
|
||||||
BufReader { inner, buf: buffer.into_boxed_slice(), pos: 0, cap: 0 }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -289,6 +289,7 @@
|
|||||||
#![feature(needs_panic_runtime)]
|
#![feature(needs_panic_runtime)]
|
||||||
#![feature(negative_impls)]
|
#![feature(negative_impls)]
|
||||||
#![feature(never_type)]
|
#![feature(never_type)]
|
||||||
|
#![feature(new_uninit)]
|
||||||
#![feature(nll)]
|
#![feature(nll)]
|
||||||
#![feature(nonnull_slice_from_raw_parts)]
|
#![feature(nonnull_slice_from_raw_parts)]
|
||||||
#![feature(once_cell)]
|
#![feature(once_cell)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user