HeadlinesBriefing favicon HeadlinesBriefing.com

Quadruple Code Speed with a 'Useless' If

Hacker News •
×

I was optimizing a domain‑specific compressor and faced a chunking problem: picking the most compact encoding for each chunk. The solution boils down to a shortest‑path search on a grid, where each cell points to the best next cell. The heavy loop that builds next_j is already well‑optimized, so the focus is the second loop that simply does j = next_j[i][j].

Although the body compiles to a single mov, modern CPUs can’t execute dependent instructions in parallel. Each iteration waits for the previous one because j threads through the loop, making the loop latency‑bound by memory access. A trick is to make the CPU think the assignment is conditional and thus ignore it until a branch misprediction occurs. By adding an if that is rarely true, the loop becomes throughput‑bound.

The compiler normally removes such useless branches, but declaring the accessed memory volatile forces the compiler to keep the branch. In benchmarks this cut the loop time from 320 µs to 80 µs, a 4× speedup (real data showed ~2×). A more elegant tweak would replace the 8‑element array with a value‑bitmask pair, but that may hurt performance.

Overall, a tiny, apparently useless branch can quadruple performance in tight loops where data is cached.