Optimize empty case in Vec::retain · model-checking/verify-rust-std@5d51099 (original) (raw)
Navigation Menu
- GitHub Copilot Write better code with AI
- GitHub Models New Manage and compare prompts
- GitHub Advanced Security Find and fix vulnerabilities
- Actions Automate any workflow
- Codespaces Instant dev environments
- Issues Plan and track work
- Code Review Manage code changes
- Discussions Collaborate outside of code
- Code Search Find more, search less
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Appearance settings
Commit 5d51099
Optimize empty case in Vec::retain
File tree
1 file changed
lines changed
1 file changed
lines changed
Lines changed: 6 additions & 0 deletions
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1711,6 +1711,12 @@ impl<T, A: Allocator> Vec<T, A> { | ||
1711 | 1711 | F: FnMut(&mut T) -> bool, |
1712 | 1712 | { |
1713 | 1713 | let original_len = self.len(); |
1714 | + | |
1715 | +if original_len == 0 { | |
1716 | +// Empty case: explicit return allows better optimization, vs letting compiler infer it | |
1717 | +return; | |
1718 | +} | |
1719 | + | |
1714 | 1720 | // Avoid double drop if the drop guard is not executed, |
1715 | 1721 | // since we may make some holes during the process. |
1716 | 1722 | unsafe { self.set_len(0) }; |