| ▲ | wallstop 2 hours ago | |
What is missing here is any benchmarks backing up this argument for code structure. Of note, as of C#9 (and maybe prior), the dotnet runtime does this automatically whenever it is deemed safe. https://devblogs.microsoft.com/dotnet/performance-improvemen... The same technique is applied as an optimization, when deemed safe, in all current gen c compilers (gcc, llvm, etc). I'm very confused why neither measurements nor references to when this is done automatically in most modern languages is included in the article. | ||
| ▲ | Maxatar 23 minutes ago | parent | next [-] | |
Can't speak for C# but in C/C++ the optimization can rarely be applied safely due to aliasing. If any part of the data you're working with involves a char* then C/C++ optimizers refrain from doing these kinds of optimizations because of how difficult it is to guarantee the absence of mutability. | ||
| ▲ | cogman10 2 hours ago | parent | prev [-] | |
At least in JVM land, it's pretty easy to thwart that optimization. Particularly if the condition is on a mutable yet unchanged in the loop value. For example:
Even though `map` isn't mutated, it's hard enough for the JVM to detect and the underlying `get` functions are complex enough that it'll run the `get("foo")` every time, which can be quite expensive. | ||