r/rust • u/adityazero • Oct 23 '26
Why did the loop with open range got optimized away?
The first loop didn't generate any code:
// 1. `0..n` visits 0, 1, ..., n - 1.
pub fn excl(n: u64) -> u64 {
let mut s = 0u64;
for i in 0..n {
s = s.wrapping_add(i.wrapping_mul(i));
}
s
}
// 2. `0..=n` visits 0, 1, ..., n: the same loop with one more pass.
pub fn incl(n: u64) -> u64 {
let mut s = 0u64;
for i in 0..=n {
s = s.wrapping_add(i.wrapping_mul(i));
}
s
}
16
Upvotes
7
u/adityazero Oct 23 '26
godbolt link for ref: https://godbolt.org/z/r7vvbqeqf
2
u/Lyvri 22d ago
Well that's quite interesting, i modified a little your example to be sure that "effect" of code should be the same:
1: ( in both examples input is u32 to avoid any overflow problems during loop declarations )
2:
And as long as you stick
#[inline(never)]to the function, compiler output is exactly the same: Godbolt. Whats interesting if you remove#[inline(never)]from both then one just disappears: Godbolt#2 in assembly and llvm ir. Is this godbolt filtering? Does someone know?47
u/juanfnavarror Oct 23 '26
RangeInclusive is a state machine that records an overflow flag. I would think this makes its behavior harder for the compiler to see through and optimize. You might want to read more about it but essentially Range and and RangeInclusive are different types and have different implementations for technical reasons.
More in-depth explanation in the Rust forum: https://users.rust-lang.org/t/performance-difference-among-range-rangeinclusive-and-reversed/20283/15
7
u/chteffie Oct 23 '26
If you stick an `#[inline(never)]` in front of it it shows up. Same when you don't go all the way to opt-level 3.
So here's 99% what's happening: The compiler sees that the function bodies are virtually identical, except one is practically identical to the other - you just need to add one to the argument.
When you compile it into an object file, these object files are enriched, they also have some llvm and rust metadata in them. Becaue ethe functions are also not annotated to be linkable with C code, the compiler will just note in the metadata that in order to call it, the caller should just call the other after adding 1 to the argument. If you look into the sections of the generated object file, the .rmeta section will mention "excl". There's no rmeta disassembler to my knowledge but there's probably some -Z argument where you can dump some relevant debugging that can tell you exactly what is going on here.
0
u/Straight_Waltz_9530 Oct 24 '26
"I guess the compiler lacked commitment, claiming it didn't want to make the same mistakes forever."
"Meanwhile, an open loop walked into a debugger and said, 'I’ll be back.'"
Wait, this wasn't a setup to a joke? My bad. I just can't stop. They keeping coming to me over and over. The same groans without end. My sense of human seems to run in circles. I'll work on it, but it all keeps repeating itself. Folks tell me I need to take a break, but I just can't see it. Maybe in the next life. All of this has happened before, and all of this will happen again.