Commit feb42bd
bors-servo
Auto merge of #112 - mbrubeck:reserve, r=emilio
Use `reserve` instead of unchecked math in `push`
`push` currently uses this line to reserve space in the vector:
```
self.grow(cmp::max(cap * 2, 1))
```
This risks overflowing `usize`. In practice this can't happen currently, because `cap` can't be larger than `isize::MAX` because of invariants upheld in liballoc, but this is not easy to see.
Replacing this with `self.reserve(1)` is clearer, easier to reason about safety (because `reserve` uses checked arithmetic), and will make it easier to change the growth strategy in the future.
This does not regress any of the `push` benchmarks. Marking `reserve` as inline is necessary to prevent `insert` benchmarks from regressing because of a change in the optimizer's inlining decisions there.
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-smallvec/112)
<!-- Reviewable:end -->1 file changed
+2
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
560 | 560 | | |
561 | 561 | | |
562 | 562 | | |
563 | | - | |
| 563 | + | |
564 | 564 | | |
565 | 565 | | |
566 | 566 | | |
| |||
618 | 618 | | |
619 | 619 | | |
620 | 620 | | |
| 621 | + | |
621 | 622 | | |
622 | 623 | | |
623 | 624 | | |
| |||
0 commit comments