| ▲ | Karliss 16 hours ago | |
Not exactly real world, but real code example demonstrating strict aliasing rule in action for C++. https://godbolt.org/z/WvMb34Kea Rust should have even more opportunities of this due to restrictions it has for writable references. There are 2 main differences between versions with and without strict aliasing. Without strict aliasing compiler can't assume that the result accumulator doesn't change during the loop and it has to repeatedly read/write it each iteration. With strict aliasing it can just read it to register, do the looping and write the result back at the end once. Second effect is that with strict aliasing enabled compiler can vectorize the loop processing 4 floats at the same time, most likely the same uncertainty of counter prevents vecotorization without strict aliasing. If you want something slightly simpler example you can disable vectorization by adding '-fno-tree-vectorize'. With it disabled there is still difference in handling of counter. Using restrict pointers and multiple same type input arrays it would probably be possible to make something closer to real world example. | ||
| ▲ | steveklabnik 16 hours ago | parent [-] | |
Note that Rust does not do strict aliasing, its model is different. Also note that C++ does not have restrict, formally speaking, though it is a common compiler extension. It's a C feature only! | ||