Skip to content

Commit

Permalink
Auto merge of #128234 - jcsp:retain-empty-case, r=tgross35
Browse files Browse the repository at this point in the history
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:
![image](https://github.com/user-attachments/assets/ebc72ace-84a0-4432-9b6f-1b3c96d353ba)
  • Loading branch information
bors committed Jul 30, 2024
2 parents 7ca96fa + 2c3b70d commit 1212657
Showing 0 changed files with 0 additions and 0 deletions.

0 comments on commit 1212657

Please sign in to comment.