Rust Optimizations That C++ Can't Do (Version 2)
robert.ocallahan.org
robert.ocallahan.org
const long w = v;
for (int i = 0; i < 100; ++i) {
sum += w;
callback();
}
...then clang++ generates the following: .LBB0_1:
call rbx
dec ebp
jne .LBB0_1
imul rax, r14, 100
It optimised away all of the additions, calling callback in a loop and then multiplying w with 100. This is something the rust compiler doesn't currently do.What this boils down to is that there isn't a good way of telling the compiler that the content of a reference will never change in C++. As noted in the other thread (https://news.ycombinator.com/item?id=14040021), const says that the current context can't change it, but someone else might still be able to.