summary refs log tree commit diff
path: root/library/alloc/src/raw_vec.rs
AgeCommit message (Collapse)AuthorLines
2024-08-09Add an optimizer hint for the capacity that with_capacity_in returnsBen Kimock-1/+7
2024-08-09Hoist IS_ZST check out of RawVecInner::from_*_inBen Kimock-6/+15
2024-08-09Polymorphize RawVecBen Kimock-193/+361
2024-07-29Reformat `use` declarations.Nicholas Nethercote-2/+1
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-26Fix doc nitsJohn Arundel-3/+3
Many tiny changes to stdlib doc comments to make them consistent (for example "Returns foo", rather than "Return foo", per RFC1574), adding missing periods, paragraph breaks, backticks for monospace style, and other minor nits. https://github.com/rust-lang/rfcs/blob/master/text/1574-more-api-documentation-conventions.md#appendix-a-full-conventions-text
2024-07-01Avoid MIR bloat in inliningScott McMurray-0/+17
In 126578 we ended up with more binary size increases than expected. This change attempts to avoid inlining large things into small things, to avoid that kind of increase, in cases when top-down inlining will still be able to do that inlining later.
2024-04-12Avoid more NonNull-raw-NonNull roundtrips in VecBen Kimock-0/+11
2024-03-28Rename reserve_for_push to grow_one and fix comment.Cai Bear-3/+3
2024-03-28Remove len argument from RawVec::reserve_for_push because it's always equal ↵Cai Bear-2/+2
to capacity. Also make Vec::insert use reserve_for_push.
2024-03-27Less generic code for Vec allocationsKornel-13/+29
2024-03-10RawVec::into_box: avoid unnecessary intermediate referenceRalf Jung-2/+1
2024-03-09Rollup merge of #120504 - kornelski:try_with_capacity, r=AmanieuGuillaume Boisseau-23/+38
Vec::try_with_capacity Related to #91913 Implements try_with_capacity for `Vec`, `VecDeque`, and `String`. I can follow it up with more collections if desired. `Vec::try_with_capacity()` is functionally equivalent to the current stable: ```rust let mut v = Vec::new(); v.try_reserve_exact(n)? ``` However, `try_reserve` calls non-inlined `finish_grow`, which requires old and new `Layout`, and is designed to reallocate memory. There is benefit to using `try_with_capacity`, besides syntax convenience, because it generates much smaller code at the call site with a direct call to the allocator. There's codegen test included. It's also a very desirable functionality for users of `no_global_oom_handling` (Rust-for-Linux), since it makes a very commonly used function available in that environment (`with_capacity` is used much more frequently than all `(try_)reserve(_exact)`).
2024-03-07Rust is a proper name: rust → RustRalf Jung-1/+1
2024-03-01Move capacity_overflow function to make ui tests change lessKornel-9/+9
Code changes in raw_vec require blessing UI tests every time
2024-03-01try_with_capacity for Vec, VecDeque, StringKornel-0/+9
#91913
2024-03-01try_with_capacity for RawVecKornel-15/+21
2024-02-17Remove unnecessary unit bindinghi-rustin-2/+2
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
2024-02-08Reduce use of NonNull::new_unchecked in library/Ben Kimock-6/+7
2024-01-13libs: use `assert_unchecked` instead of intrinsicjoboet-4/+4
2024-01-11chore: remove unnecessary blank linehi-rustin-1/+0
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
2023-12-29Italicise "bytes" in the docs of some `Vec` methodsGurinder Singh-2/+2
because on a cursory read it's easy to miss that the limit is in terms of bytes not no. of elements. The italics should help with that.
2023-12-11add comment to RawVec::cap fieldThe 8472-0/+5
2023-12-11add more niches to rawvecThe 8472-15/+36
2023-11-05Auto merge of #117503 - kornelski:hint-try-reserved, r=workingjubileebors-4/+14
Hint optimizer about try-reserved capacity This is #116568, but limited only to the less-common `try_reserve` functions to reduce bloat in debug binaries from debug info, while still addressing the main use-case #116570
2023-11-02Hint optimizer about reserved capacityKornel-4/+14
2023-10-29Increase the reach of panic_immediate_abortBen Kimock-1/+1
2023-10-17Automatically enable cross-crate inlining for small functionsBen Kimock-0/+1
2023-07-13Eliminate ZST allocations in `Box` and `Vec`Amanieu d'Antras-10/+20
2023-04-12remove some unneeded importsKaDiWa-1/+0
2023-01-22simplify layout calculations in rawvecThe 8472-5/+12
2022-09-22Make ZST checks in core/alloc more readableScott McMurray-6/+6
There's a bunch of these checks because of special handing for ZSTs in various unsafe implementations of stuff. This lets them be `T::IS_ZST` instead of `mem::size_of::<T>() == 0` every time, making them both more readable and more terse. *Not* proposed for stabilization at this time. Would be `pub(crate)` except `alloc` wants to use it too. (And while it doesn't matter now, if we ever get something like 85836 making it a const can help codegen be simpler.)
2022-06-29alloc: fix `no_global_oom_handling` warningsMiguel Ojeda-0/+1
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-05-13Remove some unnecessary `rustc_allow_const_fn_unstable` attributes.Nicholas Nethercote-1/+0
2022-04-06formattingSparkyPotato-1/+1
2022-04-06cleanupSparkyPotato-8/+2
2022-04-06fix Vec leak with 0 capacitySparkyPotato-0/+7
2022-02-12Fix typoAlphyr-1/+1
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2022-02-12Fix `shrink` and `capacity_from_bytes`Benoît du Garreau-2/+3
2022-02-12Fix a layout miscalculation in alloc::RawVecBenoît du Garreau-3/+1
2022-01-19Improve estimation of capacity in Vec::from_iterAngelicosPhosphoros-1/+1
Closes #48994
2021-12-22RawVec: don't recompute capacity after allocating.Nicholas Nethercote-17/+18
Currently it sets the capacity to `ptr.len() / mem::size_of::<T>()` after any buffer allocation/reallocation. This would be useful if allocators ever returned a `NonNull<[u8]>` with a size larger than requested. But this never happens, so it's not useful. Removing this slightly reduces the size of generated LLVM IR, and slightly speeds up the hot path of `RawVec` growth.
2021-11-30Introduce `RawVec::reserve_for_push`.Nicholas Nethercote-0/+8
If `Vec::push`'s capacity check fails it calls `RawVec::reserve`, which then also does a capacity check. This commit introduces `reserve_for_push` which skips the redundant capacity check, for some slight compile time speed-ups. I tried lots of minor variations on this, e.g. different inlining attributes. This was the best one I could find.
2021-11-03Make RawVec private to allocThe8472-57/+4
RawVec was previously exposed for compiler-internal use (libarena specifically) in 1acbb0a9350560d951359cc359361b87992a6f2b Since it is unstable, doc-hidden and has no associated tracking issue it was never meant for public use. And since it is no longer used outside alloc itself it can be made private again. Also remove some functions that are dead due to lack of internal users.
2021-10-10Add #[must_use] to alloc constructorsJohn Kugelman-0/+3
2021-08-28Fix a typo in raw_vecterrarier2111-1/+1
2021-07-24Hide allocator details from TryReserveErrorKornel-10/+10
2021-06-30alloc: `RawVec<T, A>::shrink` can be in `no_global_oom_handling`.Miguel Ojeda-1/+0
Found in https://github.com/Rust-for-Linux/linux/pull/402. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2021-05-05alloc: Add unstable Cfg feature `no-global_oom_handling`John Ericson-1/+15
For certain sorts of systems, programming, it's deemed essential that all allocation failures be explicitly handled where they occur. For example, see Linus Torvald's opinion in [1]. Merely not calling global panic handlers, or always `try_reserving` first (for vectors), is not deemed good enough, because the mere presence of the global OOM handlers is burdens static analysis. One option for these projects to use rust would just be to skip `alloc`, rolling their own allocation abstractions. But this would, in my opinion be a real shame. `alloc` has a few `try_*` methods already, and we could easily have more. Features like custom allocator support also demonstrate and existing to support diverse use-cases with the same abstractions. A natural way to add such a feature flag would a Cargo feature, but there are currently uncertainties around how std library crate's Cargo features may or not be stable, so to avoid any risk of stabilizing by mistake we are going with a more low-level "raw cfg" token, which cannot be interacted with via Cargo alone. Note also that since there is no notion of "default cfg tokens" outside of Cargo features, we have to invert the condition from `global_oom_handling` to to `not(no_global_oom_handling)`. This breaks the monotonicity that would be important for a Cargo feature (i.e. turning on more features should never break compatibility), but it doesn't matter for raw cfg tokens which are not intended to be "constraint solved" by Cargo or anything else. To support this use-case we create a new feature, "global-oom-handling", on by default, and put the global OOM handler infra and everything else it that depends on it behind it. By default, nothing is changed, but users concerned about global handling can make sure it is disabled, and be confident that all OOM handling is local and explicit. For this first iteration, non-flat collections are outright disabled. `Vec` and `String` don't yet have `try_*` allocation methods, but are kept anyways since they can be oom-safely created "from parts", and we hope to add those `try_` methods in the future. [1]: https://lore.kernel.org/lkml/CAHk-=wh_sNLoz84AUUzuqXEsYH35u=8HV3vK-jbRbJ_B-JjGrg@mail.gmail.com/
2021-04-25get rid of min_const_fn references in library/ and rustdocRalf Jung-10/+4
2021-03-21fmt, change to coldBen Kimock-2/+6