Skip to content

Commit

Permalink
Rollup merge of #87644 - Flying-Toast:vec-remove-note, r=the8472
Browse files Browse the repository at this point in the history
Recommend `swap_remove` in `Vec::remove` docs

I was able to increase the performance (by 20%!) of my project by changing a `Vec::remove` call to `Vec::swap_remove` in a hot function. I think we should explicitly put a note in the Vec::remove docs to guide people in the right direction so they don't make a similar oversight.
  • Loading branch information
JohnTitor authored Aug 2, 2021
2 parents e924e76 + 9a2e3f3 commit 0c9b35b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,12 @@ impl<T, A: Allocator> Vec<T, A> {
/// Removes and returns the element at position `index` within the vector,
/// shifting all elements after it to the left.
///
/// Note: Because this shifts over the remaining elements, it has a
/// worst-case performance of O(n). If you don't need the order of elements
/// to be preserved, use [`swap_remove`] instead.
///
/// [`swap_remove`]: Vec::swap_remove
///
/// # Panics
///
/// Panics if `index` is out of bounds.
Expand Down

0 comments on commit 0c9b35b

Please sign in to comment.