Funny random performance changes

Yesterday I had a really funny and surprising observation.

Playing again a bit with my tiny chess engine (the EGUI tiny-chess version), I added

[profile.dev]
opt-level = 3

to Cargo toml, just to be able to use default debug builds, but still can test the optimized code.

The funny fact was, that now debug build worked significantly faster than all release builds I had seen before. For release builds, it was typically for more than a year that the first reply after e2e4 took 2.5 sec for a base depth of 7 ply. All may tiny optimizations have never significantly changed that. But now the debug build took only 1.9 sec. Of course it is a bit hard to believe, so one might assume just a bug or something like that. But then I found out, that adding

[profile.release]
debug-assertions = true

gives a similar speed increase. While actually significantly increasing executable size.

I know that random performance changes are common. Reasons might be the way and structure how code is loaded into RAM, alignment of code and data and cache, and a lot more.

But still I find this surprising.

1 Like

My guess is that the assertion guarantees an invariant that enables an optimization for the following code.

4 Likes

Debug assertions add a ton of new codepaths -- every addition needs a new panic (unless the check can be optimized out) -- so a large increase in binary size isn't surprising.

But all those branches can also have branch prediction in your CPU, so even though there are more instructions it might not be any slower, and as was said, now LLVM knows that if things don't panic, then all the math doesn't overflow, which gives it more information to make optimizations based on that fact that aren't possible when that math wraps.

6 Likes