-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Let compiler assume, that Layout::align()
is always a power of two
#75281
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
Good idea. Let's just try. @bors try @rust-timer queue |
Awaiting bors try build completion |
⌛ Trying commit 82cbd18 with merge c6f0e225a4165554133f9072026caea2e61861ff... |
☀️ Try build successful - checks-actions, checks-azure |
Queued c6f0e225a4165554133f9072026caea2e61861ff with parent c989ac1, future comparison URL. |
Finished benchmarking try commit (c6f0e225a4165554133f9072026caea2e61861ff): comparison url. Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up. @bors rollup=never |
A couple of cases have regressed by at most a few percent. I would almost say the differences are all just noise. But I am not sure, I don't really have experience with interpreting these perf results. But yeah, apart from the Should we ping some people who can help interpret those results? |
I could remove the debug-assertion and do another perf-run. Actually it just double checking if @Amanieu You may want to take a look at this?
Me neither, I always hope everything is green 😄 |
I don't think we gain anything from this: in the standard library we are careful to always use |
The debug assert shouldn't matter (std in perf doesn't have them on), but the assume and check is likely causing more code to be generated or inclined - e.g. query counts here changed quite a bit: https://perf.rust-lang.org/detailed-query.html?commit=c6f0e225a4165554133f9072026caea2e61861ff&base_commit=c989ac132a81503ceaaa1054da0a3a8c5c305ef0&benchmark=issue-46449-opt&run_name=full |
I don't think this is a true. Layout is stable and anyone implementing
I also think this is the main reason, why there are a very few small regressions. Is there a change, that |
I do use it in a crate of mine (and already for quite some time). However, I do not call I am not quite sure how to proceed with this PR :/ |
I'm personally tending towards closing this PR. I don't think we should be complicating the code with optimizations that don't actually improve performance in practice. |
It's very fast to align a pointer to a power of two or round it down to the previous power of two using bit operations. However, this optimiziation is only useful if you have to check
While this is true for most allocation code, there a still allocations in the hot path using primitive, very fast allocators like stack allocators (e.g. fn alloc(layout) -> NonNull<[u8]> {
let new_ptr = self.ptr.checked_sub(layout.size()).ok_or(AllocErr)?;
let aligned_ptr = new_ptr & !(layout.align() - 1);
if unlikely(aligned_ptr < self.start()) {
return Err(AllocErr);
}
let ptr = unsafe { NonNull::new_unchecked(ptr as *mut u8) };
let memory = NonNull::slice_from_raw_parts(ptr, self.ptr - aligned_ptr);
self.ptr = aligned_ptr;
Ok(memory)
} |
Considering the disappointing perf results, I'm going to close this. |
A perf-run may be useful. I don't expect a great speed up there though, as
Layout
s created withLayout::new()
are known at compile time and this is always (almost?) used instd
.Fixes #75264