Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upUse `reserve` instead of unchecked math in `push` #112
Conversation
|
Maybe worth pointing out in the commit message that |
`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.
|
@bors-servo r=emilio |
|
|
bors-servo
added a commit
that referenced
this pull request
Aug 7, 2018
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 -->
|
|
mbrubeck
added a commit
to mbrubeck/rust-smallvec
that referenced
this pull request
Aug 22, 2018
Merged
bors-servo
added a commit
that referenced
this pull request
Aug 22, 2018
Version 0.6.5 Change log: * #115 - add `into_inner` method * #117 - add `from_buf_and_len` and `from_buf_and_len_unchecked` * #118 - optimize `from_slice` * Some code cleanup and testing improvements (#112, #113, #114, #120) cc @llogiq <!-- 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/121) <!-- Reviewable:end -->
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
mbrubeck commentedAug 6, 2018
•
edited
pushcurrently uses this line to reserve space in the vector:This risks overflowing
usize. In practice this can't happen currently, becausecapcan't be larger thanisize::MAXbecause 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 (becausereserveuses checked arithmetic), and will make it easier to change the growth strategy in the future.This does not regress any of the
pushbenchmarks. Markingreserveas inline is necessary to preventinsertbenchmarks from regressing because of a change in the optimizer's inlining decisions there.This change is