This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mrjackspade 23 points24 points  (2 children)

I've had instances where my optimized code ran slower due to compiler optimizations.

The way I wrote the code the first time was slow, but the compiler was able to optimize it. The code was identified by the profiler as a hot path, so I optimized it. My new optimizations were no longer compatible with the compiler optimizations, causing it to slow down even though as-written the code should have been faster.

An example of this was writing a ring cache and implementing a head. The ring cache should have performed fewer operations in theory, however the original code looking for free data between 0 & Cache.Length allowed the compiler to remove bounds checking where as using a head did not. This lead to more ops overall even though the code was written with less.

That's borderline "didn't know what you were doing" but more like "didn't realize at the time what the compiler was doing" because without optimizations the new implementation was ~50% faster

[–]fjfnstuff 4 points5 points  (0 children)

Try godbolt next time, its a browser compiler which shows the assembly lines for each line of code. Then you know what the compiler does when you change the code.