Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4c01025
std: thread_local::register_dtor fix proposal for FreeBSD.
devnexen Jan 27, 2024
ad1e629
Make duplicate lang items fatal
Noratrieb Jan 29, 2024
a360ecd
Delete now-useless test
Noratrieb Jan 29, 2024
1f89e90
Add test for duplicate lang items
Noratrieb Jan 29, 2024
f6b21e9
Remove the `abi_amdgpu_kernel` feature
clubby789 Jan 30, 2024
0b2579a
Make `PatternColumn` generic in `Cx`
Nadrieril Jan 24, 2024
cb0e8c5
Limit the use of `PlaceCtxt`
Nadrieril Jan 24, 2024
83e88c6
Repurpose `MatchCtxt` for usefulness only
Nadrieril Jan 24, 2024
6ef8362
Make `PatternColumn` part of the public API
Nadrieril Jan 24, 2024
5903142
Separate `PlaceCtxt` from `UsefulnessCtxt`
Nadrieril Jan 24, 2024
dfbbdda
check `RUST_BOOTSTRAP_CONFIG` in `profile_user_dist` test
onur-ozkan Jan 21, 2024
75f670d
rustdoc: Correctly handle attribute merge if this is a glob reexport
GuillaumeGomez Jan 30, 2024
024364a
Add regression test for #120487
GuillaumeGomez Jan 30, 2024
4225a1e
Don't hash lints differently to non-lints.
nnethercote Jan 30, 2024
6efddac
Provide more context on derived obligation error primary label
estebank Jan 29, 2024
c780fe6
document `FromIterator for Vec` allocation behaviors
the8472 Jan 25, 2024
39dc315
Apply suggestions from code review
the8472 Jan 26, 2024
bcb709b
Rollup merge of #120207 - onur-ozkan:120202-fix, r=clubby789
Nadrieril Jan 31, 2024
0eaa32f
Rollup merge of #120321 - Nadrieril:cleanup-cx, r=compiler-errors
Nadrieril Jan 31, 2024
03daaa6
Rollup merge of #120355 - the8472:doc-vec-fromiter, r=cuviper
Nadrieril Jan 31, 2024
a7d5382
Rollup merge of #120430 - devnexen:fix_tls_dtor_fbsd, r=cuviper
Nadrieril Jan 31, 2024
0313eb2
Rollup merge of #120469 - estebank:issue-40120, r=TaKO8Ki
Nadrieril Jan 31, 2024
032596e
Rollup merge of #120472 - Nilstrieb:die, r=compiler-errors
Nadrieril Jan 31, 2024
be4f8e2
Rollup merge of #120490 - nnethercote:Diagnostic-hashing, r=estebank
Nadrieril Jan 31, 2024
573e7f1
Rollup merge of #120495 - clubby789:remove-amdgpu-kernel, r=oli-obk
Nadrieril Jan 31, 2024
4eaf4c2
Rollup merge of #120501 - GuillaumeGomez:glob-reexport-attr-merge-bug…
Nadrieril Jan 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
document FromIterator for Vec allocation behaviors
  • Loading branch information
the8472 committed Jan 30, 2024
commit c780fe6b277aea25a8889e292492c4519715c48e
45 changes: 45 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2784,6 +2784,51 @@ impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A> {
}
}

/// Collects an iterator into a Vec, commonly called via [`Iterator::collect()`]
///
/// # Allocation behavior
///
/// In general `Vec` does not guarantee any particular grow/allocation stategy.
/// That also applies to this trait impl.
///
/// **Note:** This section covers implementation details and is therefore exempt from
/// stability guarantees.
///
/// Vec may use any or none of the following strategies,
/// depending on the supplied iterator:
///
/// * preallocate based on [`Iterator::size_hint()`]
/// * and panic if the number of items is not outside the provided lower/upper bounds
/// * use an amortized growth strategy similar to `pushing` one item at a time
/// * perform the iteration in-place on the original allocation backing the iterator
///
/// The last case warrants some attention. It is an optimization that in many cases reduces peak memory
/// consumption and improves cache locality. But when a large number of big, short-lived
/// allocations are created, only a small fraction of their items gets collected, no further use
/// is made of the spare capacity and the resulting `Vec` is moved into a longer-lived structure
/// this can lead to the large allocations having their lifetimes unnecessarily extended which
/// can result in increased memory footprint.
///
/// In cases where this is an issue the excess capacity can be discard with [`Vec::shrink_to()`],
/// [`Vec::shrink_to_fit()`] or by collecting into [`Box<[T]>`][owned slice] instead which additionally reduces
/// the size of the longlived struct.
///
/// [owned slice]: Box
///
/// ```rust
/// # use std::sync::Mutex;
/// static LONG_LIVED: Mutex<Vec<Vec<u16>>> = Mutex::new(Vec::new());
///
/// // many short-lived allocations
/// for i in 0..100 {
/// let big_temporary: Vec<u16> = (0..1024).collect();
/// // discard most items
/// let mut result: Vec<_> = big_temporary.into_iter().filter(|i| i % 100 == 0).collect();
/// // without this a lot of unused capacity might be moved into the global
/// result.shrink_to_fit();
/// LONG_LIVED.lock().unwrap().push(result);
/// }
/// ```
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> FromIterator<T> for Vec<T> {
Expand Down