about summary refs log tree commit diff
path: root/library/alloc/src
AgeCommit message (Collapse)AuthorLines
2022-08-18add some Miri-only testsRalf Jung-0/+19
2022-08-18add miri-test-libstd support to libstdRalf Jung-0/+1
2022-08-13address review comments, add tracking issueThe 8472-7/+8
2022-08-13add Vec::push_within_capacity - fallible, does not allocateThe8472-0/+44
2022-08-12Adjust cfgsMark Rousskov-3/+3
2022-08-10Guarantee `try_reserve` preserves the contents on errorYOSHIOKA Takuma-4/+8
Update doc comments to make the guarantee explicit. However, some implementations does not have the statement though. * `HashMap`, `HashSet`: require guarantees on hashbrown side. * `PathBuf`: simply redirecting to `OsString`. Fixes #99606.
2022-07-27Auto merge of #98553 - the8472:next_chunk_opt, r=Mark-Simulacrumbors-2/+43
Optimized vec::IntoIter::next_chunk impl ``` x86_64v1, default test vec::bench_next_chunk ... bench: 696 ns/iter (+/- 22) x86_64v1, pr test vec::bench_next_chunk ... bench: 309 ns/iter (+/- 4) znver2, default test vec::bench_next_chunk ... bench: 17,272 ns/iter (+/- 117) znver2, pr test vec::bench_next_chunk ... bench: 211 ns/iter (+/- 3) ``` On znver2 the default impl seems to be slow due to different inlining decisions. It goes through `core::array::iter_next_chunk` which has a deep call tree.
2022-07-26Auto merge of #99574 - durin42:allocator-patch-redux, r=nikicbors-1/+4
codegen: use new {re,de,}allocator annotations in llvm This obviates the patch that teaches LLVM internals about _rust_{re,de}alloc functions by putting annotations directly in the IR for the optimizer. The sole test change is required to anchor FileCheck to the body of the `box_uninitialized` method, so it doesn't see the `allocalign` on `__rust_alloc` and get mad about the string `alloca` showing up. Since I was there anyway, I added some checks on the attributes to prove the right attributes got set. r? `@nikic`
2022-07-26Optimized vec::IntoIter::next_chunk implThe 8472-2/+43
``` test vec::bench_next_chunk ... bench: 696 ns/iter (+/- 22) x86_64v1, pr test vec::bench_next_chunk ... bench: 309 ns/iter (+/- 4) znver2, default test vec::bench_next_chunk ... bench: 17,272 ns/iter (+/- 117) znver2, pr test vec::bench_next_chunk ... bench: 211 ns/iter (+/- 3) ``` The znver2 default impl seems to be slow due to inlining decisions. It goes through `core::array::iter_next_chunk` which has a deeper call tree.
2022-07-26codegen: use new {re,de,}allocator annotations in llvmAugie Fackler-1/+4
This obviates the patch that teaches LLVM internals about _rust_{re,de}alloc functions by putting annotations directly in the IR for the optimizer. The sole test change is required to anchor FileCheck to the body of the `box_uninitialized` method, so it doesn't see the `allocalign` on `__rust_alloc` and get mad about the string `alloca` showing up. Since I was there anyway, I added some checks on the attributes to prove the right attributes got set. While we're here, we also emit allocator attributes on __rust_alloc_zeroed. This should allow LLVM to perform more optimizations for zeroed blocks, and probably fixes #90032. [This comment](https://github.com/rust-lang/rust/issues/24194#issuecomment-308791157) mentions "weird UB-like behaviour with bitvec iterators in rustc_data_structures" so we may need to back this change out if things go wrong. The new test cases require LLVM 15, so we copy them into LLVM 14-supporting versions, which we can delete when we drop LLVM 14.
2022-07-26Rollup merge of #98710 - mojave2:string, r=JohnTitorYuki Okushi-5/+5
correct the output of a `capacity` method example The output of this example in std::alloc is different from which shown in the comment. I have tested it on both Linux and Windows.
2022-07-24Support vec zero-alloc optimization for tuples and byte arraysAngelicosPhosphoros-17/+46
* Implement IsZero trait for tuples up to 8 IsZero elements; * Implement IsZero for u8/i8, leading to implementation of it for arrays of them too; * Add more codegen tests for this optimization. * Lower size of array for IsZero trait because it fails to inline checks
2022-07-22Remove redundant lifetime bound from `impl Borrow for Cow`Frank Steffahn-1/+0
The lifetime bound `B::Owned: 'a` is redundant and doesn't make a difference, because `Cow<'a, B>` comes with an implicit `B: 'a`, and associated types will outlive lifetimes outlived by the `Self` type (and all the trait's generic parameters, of which there are none in this case), so the implicit `B: 'a` implies `B::Owned: 'a` anyway. The explicit lifetime bound here does however end up in documentation, and that's confusing in my opinion, so let's remove it ^^
2022-07-21Box::from(slice): Clarify that contents are copiedAlan Wu-1/+1
A colleague mentioned that they interpreted the old text as saying that only the pointer and the length are copied. Add a clause so it is more clear that the pointed to contents are also copied.
2022-07-21Rollup merge of #99413 - steffahn:btree_dropck, r=m-ou-seMatthias Krüger-4/+26
Add `PhantomData` marker for dropck to `BTreeMap` closes #99408
2022-07-18Rollup merge of #99198 - RalfJung:alloc-null-ptr, r=JohnTitorDylan DPC-1/+4
add missing null ptr check in alloc example `alloc` can return null on OOM, if I understood correctly. So we should never just deref a pointer we get from `alloc`.
2022-07-18Add `PhantomData` marker for dropck to `BTreeMap`Frank Steffahn-4/+26
closes #99408
2022-07-16Rollup merge of #99317 - yanchith:borrow-vec-ta-as-slice-t, r=Mark-SimulacrumMatthias Krüger-2/+2
Borrow Vec<T, A> as [T] Hello all, When `Vec` was parametrized with `A`, the `Borrow` impls were omitted and currently `Vec<T, A>` can't be borrowed as `[T]`. This PR fixes that. This was probably missed, because the `Borrow` impls are in a different file - `src/alloc/slice.rs`. We briefly discussed this here: https://github.com/rust-lang/wg-allocators/issues/96 and I was told to go ahead and make a PR :) I tested this by building the toolchain and building my code that needed the `Borrow` impl against it, but let me know if I should add any tests to this PR.
2022-07-16Parametrize a few more thingsyanchith-16/+19
2022-07-16Mark lifetimes explicitlyyanchith-10/+10
2022-07-16Parametrize BinaryHeap with Allocatoryanchith-45/+111
2022-07-16Borrow Vec<T, A> as [T]yanchith-2/+2
2022-07-16Rollup merge of #99277 - joshtriplett:stabilize-core-cstr-alloc-cstring, ↵Yuki Okushi-9/+7
r=Mark-Simulacrum Stabilize `core::ffi::CStr`, `alloc::ffi::CString`, and friends Stabilize the `core_c_str` and `alloc_c_string` feature gates. Change `std::ffi` to re-export these types rather than creating type aliases, since they now have matching stability.
2022-07-16Rollup merge of #99270 - rhysd:issue-99269, r=Mark-SimulacrumYuki Okushi-0/+1
Add `#[must_use]` to `Box::from_raw` Fixes #99269
2022-07-15Stabilize `core::ffi::CStr`, `alloc::ffi::CString`, and friendsJosh Triplett-9/+7
Stabilize the `core_c_str` and `alloc_c_string` feature gates. Change `std::ffi` to re-export these types rather than creating type aliases, since they now have matching stability.
2022-07-15add `#[must_use]` to `Box::from_raw`rhysd-0/+1
2022-07-15Rollup merge of #99113 - WaffleLapkin:arc_simplify, r=Mark-SimulacrumDylan DPC-12/+11
Simplify [a]rc code a little Nothing interesting, just make [a]rc code a little nicer by using `byte_sub` and `let`-`else`.
2022-07-14add missing null ptr check in alloc exampleRalf Jung-1/+4
2022-07-14add code examplesDuarte Nunes-1/+50
2022-07-14Rollup merge of #98315 - joshtriplett:stabilize-core-ffi-c, r=Mark-SimulacrumDylan DPC-1/+0
Stabilize `core::ffi:c_*` and rexport in `std::ffi` This only stabilizes the base types, not the non-zero variants, since those have their own separate tracking issue and have not gone through FCP to stabilize.
2022-07-13Stabilize `core::ffi:c_*` and rexport in `std::ffi`Josh Triplett-1/+0
This only stabilizes the base types, not the non-zero variants, since those have their own separate tracking issue and have not gone through FCP to stabilize.
2022-07-13rustdocDuarte Nunes-1/+1
2022-07-13typoDuarte Nunes-2/+2
2022-07-13changes to wordingDuarte Nunes-9/+15
2022-07-13docs: be less harsh in wording for Vec::from_raw_partsDuarte Nunes-5/+15
In particular, be clear that it is sound to specify memory not originating from a previous `Vec` allocation. That is already suggested in other parts of the documentation about zero-alloc conversions to Box<[T]>. Incorporate a constraint from `slice::from_raw_parts` that was missing but needs to be fulfilled, since a `Vec` can be converted into a slice.
2022-07-10Use `byte_sub` in [a]rc implMaybe Waffle-12/+11
2022-07-08Intra-doc-link-ify reference to Clone::clone_fromest31-1/+1
2022-07-03Auto merge of #98755 - nnethercote:faster-vec-insert, r=cuviperbors-6/+9
Optimize `Vec::insert` for the case where `index == len`. By skipping the call to `copy` with a zero length. This makes it closer to `push`. I did this recently for `SmallVec` (https://github.com/servo/rust-smallvec/pull/282) and it was a big perf win in one case. Although I don't have a specific use case in mind, it seems worth doing it for `Vec` as well. Things to note: - In the `index < len` case, the number of conditions checked is unchanged. - In the `index == len` case, the number of conditions checked increases by one, but the more expensive zero-length copy is avoided. - In the `index > len` case the code now reserves space for the extra element before panicking. This seems like an unimportant change. r? `@cuviper`
2022-07-03Auto merge of #98673 - pietroalbini:pa-bootstrap-update, r=Mark-Simulacrumbors-42/+6
Bump bootstrap compiler r? `@Mark-Simulacrum`
2022-07-01Rollup merge of #98585 - cuviper:covariant-thinbox, r=thomccDylan DPC-6/+27
Make `ThinBox<T>` covariant in `T` Just like `Box<T>`, we want `ThinBox<T>` to be covariant in `T`, but the projection in `WithHeader<<T as Pointee>::Metadata>` was making it invariant. This is now hidden as `WithOpaqueHeader`, which we type-cast whenever the real `WithHeader<H>` type is needed. Fixes the problem noted in <https://github.com/rust-lang/rust/issues/92791#issuecomment-1104636249>.
2022-07-01update cfg(bootstrap)sPietro Albini-42/+6
2022-07-01Optimize `Vec::insert` for the case where `index == len`.Nicholas Nethercote-6/+9
By skipping the call to `copy` with a zero length. This makes it closer to `push`. I did this recently for `SmallVec` (https://github.com/servo/rust-smallvec/pull/282) and it was a big perf win in one case. Although I don't have a specific use case in mind, it seems worth doing it for `Vec` as well. Things to note: - In the `index < len` case, the number of conditions checked is unchanged. - In the `index == len` case, the number of conditions checked increases by one, but the more expensive zero-length copy is avoided. - In the `index > len` case the code now reserves space for the extra element before panicking. This seems like an unimportant change.
2022-06-30correct the output of a `capacity` method examplemojave2-5/+5
2022-06-29alloc: fix `no_global_oom_handling` warningsMiguel Ojeda-3/+8
Rust 1.62.0 introduced a couple new `unused_imports` warnings in `no_global_oom_handling` builds, making a total of 5 warnings: ```txt warning: unused import: `Unsize` --> library/alloc/src/boxed/thin.rs:6:33 | 6 | use core::marker::{PhantomData, Unsize}; | ^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused import: `from_fn` --> library/alloc/src/string.rs:51:18 | 51 | use core::iter::{from_fn, FusedIterator}; | ^^^^^^^ warning: unused import: `core::ops::Deref` --> library/alloc/src/vec/into_iter.rs:12:5 | 12 | use core::ops::Deref; | ^^^^^^^^^^^^^^^^ warning: associated function `shrink` is never used --> library/alloc/src/raw_vec.rs:424:8 | 424 | fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError> { | ^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: associated function `forget_remaining_elements` is never used --> library/alloc/src/vec/into_iter.rs:126:19 | 126 | pub(crate) fn forget_remaining_elements(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^ ``` This patch cleans them so that projects compiling `alloc` without infallible allocations do not see the warnings. It also enables the use of `-Dwarnings`. The couple `dead_code` ones may be reverted when some fallible allocation support starts using them. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2022-06-27Implement `Send` and `Sync` for `ThinBox<T>`Josh Stone-0/+8
Just like `Box<T>`, `ThinBox<T>` owns its data on the heap, so it should implement `Send` and `Sync` when `T` does.
2022-06-27Make `ThinBox<T>` covariant in `T`Josh Stone-6/+27
Just like `Box<T>`, we want `ThinBox<T>` to be covariant in `T`, but the projection in `WithHeader<<T as Pointee>::Metadata>` was making it invariant. This is now hidden as `WithOpaqueHeader`, which we type-cast whenever the real `WithHeader<H>` type is needed.
2022-06-24Rollup merge of #98039 - tnballo:master, r=thomccYuki Okushi-13/+117
Fix `panic` message for `BTreeSet`'s `range` API and document `panic` cases Currently, the `panic` cases for [`BTreeSet`'s `range` API](https://doc.rust-lang.org/std/collections/struct.BTreeSet.html#method.range) are undocumented and produce a slightly wrong `panic` message (says `BTreeMap` instead of `BTreeSet`). Panic case 1 code: ```rust use std::collections::BTreeSet; use std::ops::Bound::Excluded; fn main() { let mut set = BTreeSet::new(); set.insert(3); set.insert(5); set.insert(8); for &elem in set.range((Excluded(&3), Excluded(&3))) { println!("{elem}"); } } ``` Panic case 1 message: ``` thread 'main' panicked at 'range start and end are equal and excluded in BTreeMap', /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/alloc/src/collections/btree/search.rs:105:17 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` Panic case 2 code: ```rust use std::collections::BTreeSet; use std::ops::Bound::Included; fn main() { let mut set = BTreeSet::new(); set.insert(3); set.insert(5); set.insert(8); for &elem in set.range((Included(&8), Included(&3))) { println!("{elem}"); } } ``` Panic case 2: ``` thread 'main' panicked at 'range start is greater than range end in BTreeMap', /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/alloc/src/collections/btree/search.rs:110:17 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` This PR fixes the output messages to say `BTreeSet`, adds the relevant unit tests, and updates the documentation for the API.
2022-06-23Fix BTreeSet's range API panic message, documenttnballo-13/+117
2022-06-23Rollup merge of #98364 - RalfJung:arc-clone, r=Mark-SimulacrumMichael Goulet-8/+9
clarify Arc::clone overflow check comment I had to read this twice to realize that this is explaining that the code is technically unsound, so move that into a dedicated paragraph and make the wording a bit more explicit.
2022-06-23Rollup merge of #96173 - jmaargh:jmaargh/with-capacity-doc-fix, r=Dylan-DPCMichael Goulet-93/+129
Fix documentation for `with_capacity` and `reserve` families of methods Fixes #95614 Documentation for the following methods - `with_capacity` - `with_capacity_in` - `with_capacity_and_hasher` - `reserve` - `reserve_exact` - `try_reserve` - `try_reserve_exact` was inconsistent and often not entirely correct where they existed on the following types - `Vec` - `VecDeque` - `String` - `OsString` - `PathBuf` - `BinaryHeap` - `HashSet` - `HashMap` - `BufWriter` - `LineWriter` since the allocator is allowed to allocate more than the requested capacity in all such cases, and will frequently "allocate" much more in the case of zero-sized types (I also checked `BufReader`, but there the docs appear to be accurate as it appears to actually allocate the exact capacity). Some effort was made to make the documentation more consistent between types as well.