-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Don't emit divide-by-zero panic paths in StepBy::len
#123564
Conversation
I happened to notice today that there's actually two such calls emitted in the assembly: <https://rust.godbolt.org/z/1Wbbd3Ts6> Since they're impossible, hopefully telling LLVM that will also help optimizations elsewhere.
iter: &mut I, | ||
step_minus_one: usize, | ||
) -> impl FnMut() -> Option<I::Item> + '_ { | ||
move || iter.nth(step_minus_one) |
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.
As I went through I also tried to be consistent about naming things step_minus_one
when they're not the original step
, since it was confusing to have the mix.
@@ -500,7 +518,7 @@ macro_rules! spec_int_ranges_r { | |||
fn spec_next_back(&mut self) -> Option<Self::Item> | |||
where Range<$t>: DoubleEndedIterator + ExactSizeIterator, | |||
{ | |||
let step = (self.step + 1) as $t; | |||
let step = self.original_step().get() as $t; |
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.
Like how here the two step
s in the previous code meant very different things, but since the let step
is the width (not the "what to pass to nth") it's still called step
.
fn first_size(step: usize) -> impl Fn(usize) -> usize { | ||
move |n| if n == 0 { 0 } else { 1 + (n - 1) / (step + 1) } | ||
fn first_size(step: NonZeroUsize) -> impl Fn(usize) -> usize { | ||
move |n| if n == 0 { 0 } else { 1 + (n - 1) / step } |
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.
Thank you, Div<NonZeroUsize>
, for just magically doing the right thing here 🎉
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.
Looks very good to me! I'm not a big fan of step_minus_one
, but couldn't come up with a better name. The comment about nth
made it click, so it's fine.
@bors r+ |
…iaskrgr Rollup of 9 pull requests Successful merges: - rust-lang#122781 (Fix argument ABI for overaligned structs on ppc64le) - rust-lang#123367 (Safe Transmute: Compute transmutability from `rustc_target::abi::Layout`) - rust-lang#123518 (Fix `ByMove` coroutine-closure shim (for 2021 precise closure capturing behavior)) - rust-lang#123547 (bootstrap: remove unused pub fns) - rust-lang#123564 (Don't emit divide-by-zero panic paths in `StepBy::len`) - rust-lang#123578 (Restore `pred_known_to_hold_modulo_regions`) - rust-lang#123591 (Remove unnecessary cast from `LLVMRustGetInstrProfIncrementIntrinsic`) - rust-lang#123632 (parser: reduce visibility of unnecessary public `UnmatchedDelim`) - rust-lang#123635 (CFI: Fix ICE in KCFI non-associated function pointers) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#123564 - scottmcm:step-by-div-zero, r=joboet Don't emit divide-by-zero panic paths in `StepBy::len` I happened to notice today that there's actually two such calls emitted in the assembly: <https://rust.godbolt.org/z/1Wbbd3Ts6> Since they're impossible, hopefully telling LLVM that will also help optimizations elsewhere.
I happened to notice today that there's actually two such calls emitted in the assembly: https://rust.godbolt.org/z/1Wbbd3Ts6
Since they're impossible, hopefully telling LLVM that will also help optimizations elsewhere.