Quadrupling Code Performance with a Useless If Statement

Quadrupling code performance with a "useless" if

While optimizing a compressor, I discovered that adding a seemingly useless conditional check could quadruple loop performance. By tricking the CPU's branch predictor, I broke the dependency chain that was slowing down execution. Although compilers usually remove such code, using a volatile cast forced the optimization, proving that sometimes misleading the machine yields the best results.

Stupid compiler doesn't realize integers have hardware provenance.
  1. nonadhocproblem

    I've also been working on a compressor recently, and the same general idea (letting the compiler know that a data dependency occurs very infrequently, and it should therefore assume none exists to exploit ILP) has allowed me to speed up my decompression by a lot:

    https://github.com/welcome-to-the-sunny-side/misa77/blob/777...

  2. throawayonthe

    lobsters comment points out [[unlikely]] works here for clang

    https://clang.godbolt.org/z/r4xYWfPfe

    edit: oh the article also mentions it now :)

  3. akoboldfrying

    This is really surprising! I've never considered the possibility that using an equality test to skip a write that would be a no-op could break a dependency and thus lead to higher perf overall if the "equal" outcome occurs often enough. This might be applicable in many situations where you "edit" some data in-place, but most of the time there are few or no changes.

More from this day

2026-07-13