Skip to content

Commit

Permalink
Optimize empty case in Vec::retain
Browse files Browse the repository at this point in the history
  • Loading branch information
jcsp committed Jul 26, 2024
1 parent 355efac commit 7772b8f
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,11 @@ impl<T, A: Allocator> Vec<T, A> {
F: FnMut(&mut T) -> bool,
{
let original_len = self.len();

if original_len == 0 {
return;
}

// Avoid double drop if the drop guard is not executed,
// since we may make some holes during the process.
unsafe { self.set_len(0) };
Expand Down

0 comments on commit 7772b8f

Please sign in to comment.