Commit d2b1900
committed
Use
`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 won't lead to any
bugs, because the capacity 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. (Currently
`reserve(1)` will grow to the next power of 2.)
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.reserve instead of unchecked math in push
1 parent 62c525a commit d2b1900
1 file changed
+2
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
574 | 574 | | |
575 | 575 | | |
576 | 576 | | |
577 | | - | |
| 577 | + | |
578 | 578 | | |
579 | 579 | | |
580 | 580 | | |
| |||
632 | 632 | | |
633 | 633 | | |
634 | 634 | | |
| 635 | + | |
635 | 636 | | |
636 | 637 | | |
637 | 638 | | |
| |||
0 commit comments