Auto merge of #98755 - nnethercote:faster-vec-insert, r=cuviper
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` (https://github.com/servo/rust-smallvec/pull/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. r? `@cuviper`
This commit is contained in:
commit
f99f9e48ed
1 changed files with 9 additions and 6 deletions
|
@ -1379,9 +1379,6 @@ impl<T, A: Allocator> Vec<T, A> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let len = self.len();
|
let len = self.len();
|
||||||
if index > len {
|
|
||||||
assert_failed(index, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
// space for the new element
|
// space for the new element
|
||||||
if len == self.buf.capacity() {
|
if len == self.buf.capacity() {
|
||||||
|
@ -1393,9 +1390,15 @@ impl<T, A: Allocator> Vec<T, A> {
|
||||||
// The spot to put the new value
|
// The spot to put the new value
|
||||||
{
|
{
|
||||||
let p = self.as_mut_ptr().add(index);
|
let p = self.as_mut_ptr().add(index);
|
||||||
// Shift everything over to make space. (Duplicating the
|
if index < len {
|
||||||
// `index`th element into two consecutive places.)
|
// Shift everything over to make space. (Duplicating the
|
||||||
ptr::copy(p, p.offset(1), len - index);
|
// `index`th element into two consecutive places.)
|
||||||
|
ptr::copy(p, p.offset(1), len - index);
|
||||||
|
} else if index == len {
|
||||||
|
// No elements need shifting.
|
||||||
|
} else {
|
||||||
|
assert_failed(index, len);
|
||||||
|
}
|
||||||
// Write it in, overwriting the first copy of the `index`th
|
// Write it in, overwriting the first copy of the `index`th
|
||||||
// element.
|
// element.
|
||||||
ptr::write(p, element);
|
ptr::write(p, element);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue