rust/src/libcore
Mazdak Farrokhzad a3b6e8ef99
Rollup merge of #62451 - SimonSapin:new_uninit, r=RalfJung
Add APIs for uninitialized Box, Rc, and Arc. (Plus get_mut_unchecked)

Assigning `MaybeUninit::<Foo>::uninit()` to a local variable is usually free, even when `size_of::<Foo>()` is large. However, passing it for example to `Arc::new` [causes at least one copy](https://youtu.be/F1AquroPfcI?t=4116) (from the stack to the newly allocated heap memory) even though there is no meaningful data. It is theoretically possible that a Sufficiently Advanced Compiler could optimize this copy away, but this is [reportedly unlikely to happen soon in LLVM](https://youtu.be/F1AquroPfcI?t=5431).

This PR proposes two sets of features:

* Constructors for containers (`Box`, `Rc`, `Arc`) of `MaybeUninit<T>` or `[MaybeUninit<T>]` that do not initialized the data, and unsafe conversions to the known-initialized types (without `MaybeUninit`). The constructors are guaranteed not to make unnecessary copies.

* On `Rc` and `Arc`, an unsafe `get_mut_unchecked` method that provides `&mut T` access without checking the reference count. `Arc::get_mut` involves multiple atomic operations whose cost can be non-trivial. `Rc::get_mut` is less costly, but we add `Rc::get_mut_unchecked` anyway for symmetry with `Arc`.

  These can be useful independently, but they will presumably be typical when the new constructors of `Rc` and `Arc` are used.

  An alternative with a safe API would be to introduce `UniqueRc` and `UniqueArc` types that have the same memory layout as `Rc` and `Arc` (and so zero-cost conversion to them) but are guaranteed to have only one reference. But introducing entire new types feels “heavier” than new constructors on existing types, and initialization of `MaybeUninit<T>` typically requires unsafe code anyway.

Summary of new APIs (all unstable in this PR):

```rust
impl<T> Box<T> { pub fn new_uninit() -> Box<MaybeUninit<T>> {…} }
impl<T> Box<MaybeUninit<T>> { pub unsafe fn assume_init(self) -> Box<T> {…} }
impl<T> Box<[T]> { pub fn new_uninit_slice(len: usize) -> Box<[MaybeUninit<T>]> {…} }
impl<T> Box<[MaybeUninit<T>]> { pub unsafe fn assume_init(self) -> Box<[T]> {…} }

impl<T> Rc<T> { pub fn new_uninit() -> Rc<MaybeUninit<T>> {…} }
impl<T> Rc<MaybeUninit<T>> { pub unsafe fn assume_init(self) -> Rc<T> {…} }
impl<T> Rc<[T]> { pub fn new_uninit_slice(len: usize) -> Rc<[MaybeUninit<T>]> {…} }
impl<T> Rc<[MaybeUninit<T>]> { pub unsafe fn assume_init(self) -> Rc<[T]> {…} }

impl<T> Arc<T> { pub fn new_uninit() -> Arc<MaybeUninit<T>> {…} }
impl<T> Arc<MaybeUninit<T>> { pub unsafe fn assume_init(self) -> Arc<T> {…} }
impl<T> Arc<[T]> { pub fn new_uninit_slice(len: usize) -> Arc<[MaybeUninit<T>]> {…} }
impl<T> Arc<[MaybeUninit<T>]> { pub unsafe fn assume_init(self) -> Arc<[T]> {…} }

impl<T: ?Sized> Rc<T> { pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {…} }
impl<T: ?Sized> Arc<T> { pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {…} }
```
2019-08-17 22:57:29 +02:00
..
benches Improve ptr_rotate performance, tests, and benchmarks 2019-08-06 10:42:48 -05:00
char Handle cfg(bootstrap) throughout 2019-08-14 05:39:53 -04:00
fmt Make fmt-internal functions private 2019-08-17 08:59:36 +01:00
future Use associated_type_bounds where applicable - closes #61738 2019-08-08 22:39:15 +02:00
hash Make built-in derives opaque macros 2019-08-17 08:59:36 +01:00
iter Rollup merge of #62737 - timvermeulen:cycle_try_fold, r=scottmcm 2019-08-17 11:13:42 +02:00
mem Auto merge of #63575 - Centril:rollup-anlv9g5, r=Centril 2019-08-15 00:32:05 +00:00
num rustbuild: work around the stdarch cfg(bootstrap) bug. 2019-08-16 20:12:10 +03:00
ops Add note suggesting to borrow a String argument to find 2019-07-25 10:11:03 -07:00
prelude Remove __rust_unstable_column 2019-08-15 22:58:57 +03:00
ptr Doc nit 2019-08-17 21:40:35 +02:00
slice Use Result::unwrap_or_else instead of matching 2019-08-10 17:16:58 +00:00
str Use associated_type_bounds where applicable - closes #61738 2019-08-08 22:39:15 +02:00
sync Remove unneeded feature attr from atomic integers doctests 2019-06-04 13:24:39 +00:00
task Provide map_ok and map_err method for Poll<Option<Result<T, E>>> 2019-08-13 15:15:33 +05:30
tests Rollup merge of #62737 - timvermeulen:cycle_try_fold, r=scottmcm 2019-08-17 11:13:42 +02:00
unicode Make some items in core::unicode private 2019-08-05 23:50:47 +01:00
alloc.rs Fix clippy::clone_on_copy warnings 2019-07-18 15:14:56 +02:00
any.rs Handle cfg(bootstrap) throughout 2019-08-14 05:39:53 -04:00
array.rs FixedSizeArray: Add missing links in doc comments. 2019-08-01 20:24:05 +07:00
ascii.rs Implement Clone, Display for ascii::EscapeDefault 2019-08-09 22:45:06 -04:00
borrow.rs be more direct about borrow requirenments 2019-04-03 11:41:24 +03:00
Cargo.toml bump libcore tests to rand 0.7 2019-08-04 14:50:32 +02:00
cell.rs Auto merge of #62748 - luca-barbieri:optimize-refcell-borrow, r=RalfJung 2019-07-27 09:32:44 +00:00
clone.rs Make built-in derives opaque macros 2019-08-17 08:59:36 +01:00
cmp.rs Make built-in derives opaque macros 2019-08-17 08:59:36 +01:00
convert.rs Use associated_type_bounds where applicable - closes #61738 2019-08-08 22:39:15 +02:00
default.rs Make built-in derives opaque macros 2019-08-17 08:59:36 +01:00
ffi.rs Make VaListImpl<'f> invariant over the 'f lifetime 2019-07-14 18:14:15 -07:00
hint.rs Remove derives Encodable/Decodable and unstabilize attribute #[bench] 2019-07-31 21:27:59 +03:00
internal_macros.rs std: Remove internal definitions of cfg_if! macro 2019-06-10 10:58:44 -07:00
intrinsics.rs rustbuild: work around the stdarch cfg(bootstrap) bug. 2019-08-16 20:12:10 +03:00
iter_private.rs Remove licenses 2018-12-25 21:08:33 -07:00
lib.rs Handle cfg(bootstrap) throughout 2019-08-14 05:39:53 -04:00
macros.rs Make built-in derives opaque macros 2019-08-17 08:59:36 +01:00
marker.rs Make built-in derives opaque macros 2019-08-17 08:59:36 +01:00
option.rs Rollup merge of #62459 - timvermeulen:result_sum_internal_iteration, r=scottmcm 2019-08-06 15:36:27 +02:00
panic.rs libcore: deny more... 2019-04-19 01:37:12 +02:00
panicking.rs libcore: deny more... 2019-04-19 01:37:12 +02:00
pin.rs Rollup merge of #63350 - iluuu1994:use-associated-type-bounds, r=Centril 2019-08-10 08:13:19 +02:00
raw.rs Fix more tests after revert of rustdoc cfg(test) feature 2019-06-09 18:15:53 +01:00
result.rs Use internal iteration in the Sum and Product impls of Result and Option 2019-07-29 02:40:50 +02:00
time.rs fix tests 2019-07-30 16:41:03 +00:00
tuple.rs libcore => 2018 2019-04-18 14:47:35 +09:00
unit.rs libcore => 2018 2019-04-18 14:47:35 +09:00