-
Notifications
You must be signed in to change notification settings - Fork 13.4k
included range in for loop gives more assembly code than excluded range #102462
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
@rustbot label -A-diagnostics +A-codegen +I-heavy |
This regressed between 1.42 and 1.43: Godbolt link related: #45222 |
(If you have two nested loop, the code is bad even in older compilers). |
The 1.42 codegen is tricky, as it seems at first that it doesn't account for the case where |
Simpler example without the unnecessary memory indirections: https://rust.godbolt.org/z/GPvrbvbYY External iteration over inclusive ranges is well known to optimize badly. Optimization may be viable in this particular case though. |
This is the fold we'd need: https://alive2.llvm.org/ce/z/JsWRvT |
Came across this weirdness today; it's still a problem in December 2023: pub fn inclusive() -> usize {
(2..=100).step_by(2).sum::<usize>()
}
pub fn exclusive() -> usize {
(2..101).step_by(2).sum::<usize>()
} produces assembly with 46 lines and 3 lines, respectively. Was going to create a new bug for it, but I'm pretty sure it's just another version of this one. |
@rustbot claim |
The code snippet:
compiled with:
Produces functions that both take up 25 lines of assembler (not necessarily 25 instructions). So no diference between inclusive and exclusive ranges. I am unsure if this is the update to rust 1.85.0, the move to an ARM processor or to MacOS. |
Uh oh!
There was an error while loading. Please reload this page.
Given the following code where source will be excluded
The assembly output is:
if source will be included the assembly code will increase more than
The assembly output is:
I tested on rustc 1.64.0 with -C opt-level=3
The text was updated successfully, but these errors were encountered: