-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 pointer::is_aligned_to
#100169
Optimize pointer::is_aligned_to
#100169
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
(rust-highfive has picked a reviewer for you, use r? to override) |
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.
Sanity checked these are numerically equivalent in the Playground:
use std::iter::successors;
fn main() {
for addr in successors(Some(1u64), |x| x.checked_mul(2)) {
let mut i = successors(Some(1u64), |x| x.checked_mul(2));
while let Some(align) = i.next() && align <= addr {
for addr in successors(Some(addr), |x| x.checked_add(addr)).take(256) {
assert!(addr % align == addr & align - 1);
assert!(addr & align -1 == 0);
}
}
}
}
@bors r+ rollup |
…iaskrgr Rollup of 5 pull requests Successful merges: - rust-lang#100071 (deps: dedupe `annotate-snippets` crate versions) - rust-lang#100127 (Remove Windows function preloading) - rust-lang#100130 (Avoid pointing out `return` span if it has nothing to do with type error) - rust-lang#100169 (Optimize `pointer::as_aligned_to`) - rust-lang#100175 (ascii -> ASCII in code comment) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
// Cast is needed for `T: !Sized` | ||
self.cast::<u8>().addr() % align == 0 | ||
self.cast::<u8>().addr() & align - 1 == 0 |
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.
self.cast::<u8>().addr() & (align - 1) == 0
are easier to understand
pointer::as_aligned_to
pointer::is_aligned_to
This PR replaces
addr % align
withaddr & align - 1
, which is correct due toalign
being a power of two.Here is a proof that this makes things better: [godbolt].
This PR also removes
assume(align != 0)
, with the new impl it does not improve anything anymore ([godbolt], [original concern]).