mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 11:48:30 +00:00
![]() Optimize empty case in Vec::retain While profiling some code that happens to call Vec::retain() in a tight loop, I noticed more runtime than expected in retain, even in a bench case where the vector was always empty. When I wrapped my call to retain in `if !myvec.is_empty()` I saw faster execution compared with doing retain on an empty vector. On closer inspection, Vec::retain is doing set_len(0) on itself even when the vector is empty, and then resetting the length again in BackshiftOnDrop::drop. Unscientific screengrab of a flamegraph illustrating how we end up spending time in set_len and drop:  |
||
---|---|---|
.. | ||
benches | ||
src | ||
tests | ||
Cargo.toml |