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 29, 2024
1 parent a152820 commit 5d51099
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,12 @@ impl<T, A: Allocator> Vec<T, A> {
F: FnMut(&mut T) -> bool,
{
let original_len = self.len();

if original_len == 0 {
// Empty case: explicit return allows better optimization, vs letting compiler infer it
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 5d51099

Please sign in to comment.