Skip to content

Commit

Permalink
don't mention the number of allocations
Browse files Browse the repository at this point in the history
this is primarily about unused capacity having its lifetime extended
  • Loading branch information
the8472 committed Jan 29, 2024
1 parent c92f814 commit 8888bfc
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2803,11 +2803,11 @@ impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A> {
/// * 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.
/// consumption and improves cache locality. But when 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 discarded with [`Vec::shrink_to()`],
/// [`Vec::shrink_to_fit()`] or by collecting into [`Box<[T]>`][owned slice] instead, which additionally reduces
Expand All @@ -2819,8 +2819,7 @@ impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A> {
/// # use std::sync::Mutex;
/// static LONG_LIVED: Mutex<Vec<Vec<u16>>> = Mutex::new(Vec::new());
///
/// // many short-lived allocations
/// for i in 0..100 {
/// for i in 0..10 {
/// 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();
Expand Down

0 comments on commit 8888bfc

Please sign in to comment.