Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Vec::insert for the case where index == len. #98755

Merged
merged 1 commit into from
Jul 3, 2022

Commits on Jun 30, 2022

  1. Optimize Vec::insert for the case where index == len.

    By skipping the call to `copy` with a zero length. This makes it closer
    to `push`.
    
    I did this recently for `SmallVec`
    (servo/rust-smallvec#282) and it was a big perf win in
    one case. Although I don't have a specific use case in mind, it seems
    worth doing it for `Vec` as well.
    
    Things to note:
    - In the `index < len` case, the number of conditions checked is
      unchanged.
    - In the `index == len` case, the number of conditions checked increases
      by one, but the more expensive zero-length copy is avoided.
    - In the `index > len` case the code now reserves space for the extra
      element before panicking. This seems like an unimportant change.
    nnethercote committed Jun 30, 2022
    Configuration menu
    Copy the full SHA
    679c5ee View commit details
    Browse the repository at this point in the history