-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 allocation paths in RawVec #43815
Conversation
None of these require a significant amount of code and using `#[inline]` will allow constructors to get inlined, improving codegen at allocation callsites.
This was forgotten from rust-lang#42727 by accident, but these functions are rarely called and codegen can be improved in LLVM with the `#[cold]` tag.
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
I'd like to nominate this for beta eventually, but I'd prefer to wait and see the impact on perf.rust-lang.org before deciding to do so. |
"fatal runtime error: unsupported allocator request"
|
c9e34c4
to
be9505a
Compare
src/liballoc/raw_vec.rs
Outdated
use core::slice; | ||
use heap::Heap; | ||
use heap::{Alloc, Layout, Heap, AllocErr}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cannot build tidy
due to unused import.
[00:02:33] Compiling alloc v0.0.0 (file:///checkout/src/liballoc)
[00:02:36] error: unused import: `AllocErr`
[00:02:36] --> /checkout/src/liballoc/raw_vec.rs:16:33
[00:02:36] |
[00:02:36] 16 | use heap::{Alloc, Layout, Heap, AllocErr};
[00:02:36] | ^^^^^^^^
[00:02:36] |
[00:02:36] note: lint level defined here
[00:02:36] --> /checkout/src/liballoc/lib.rs:77:9
[00:02:36] |
[00:02:36] 77 | #![deny(warnings)]
[00:02:36] | ^^^^^^^^
[00:02:36] = note: #[deny(unused_imports)] implied by #[deny(warnings)]
[00:02:36]
[00:02:37] error: aborting due to previous error
[00:02:37]
[00:02:37] error: Could not compile `alloc`.
The `RawVec` type has a number of invariants that it upholds throughout its execution, and as a result many of the runtime checks imposed by using `Layout` in a "raw" fashion aren't actually necessary. For example a `RawVec`'s capacity is intended to always match the layout which "fits" the allocation, so we don't need any runtime checks when retrieving the current `Layout` for a vector. Consequently, this adds a safe `current_layout` function which internally uses the `from_size_align_unchecked` function. Along the same lines we know that most construction of new layouts will not overflow. All allocations in `RawVec` are kept below `isize::MAX` and valid alignments are also kept low enough that we're guaranteed that `Layout` for a doubled vector will never overflow and will always succeed construction. Consequently a few locations can use `from_size_align_unchecked` in addition when constructing the *new* layout to allocate (or reallocate), which allows for eliding some more runtime checks. Overall this should significant improve performance for an important function, `RawVec::double`. This commit removes four runtime jumps before `__rust_realloc` is called, as well as one after it's called.
be9505a
to
3a83165
Compare
Green! |
@bors r+ |
📌 Commit 3a83165 has been approved by |
Optimize allocation paths in RawVec Since the `Alloc` trait was introduced (#42313) and it was integrated everywhere (#42727) there's been some slowdowns and regressions that have slipped through. The intention of this PR is to try to tackle at least some of them, but they've been very difficult to quantify up to this point so it probably doesn't solve everything. This PR primarily targets the `RawVec` type, specifically the `double` function. The codegen for this function is now much closer to what it was before #42313 landed as many runtime checks have been elided.
☀️ Test successful - status-appveyor, status-travis |
Since the
Alloc
trait was introduced (#42313) and it was integrated everywhere (#42727) there's been some slowdowns and regressions that have slipped through. The intention of this PR is to try to tackle at least some of them, but they've been very difficult to quantify up to this point so it probably doesn't solve everything.This PR primarily targets the
RawVec
type, specifically thedouble
function. The codegen for this function is now much closer to what it was before #42313 landed as many runtime checks have been elided.