Friday, January 30, 2009

Quiz question

Here's a fun puzzle I watched Dave Mandelin track down yesterday. Why would adding a guaranteed no-op such as
if (FALSE) { printf("can't happen\n"); }
or even
asm("noop\n");
to the body of a procedure cause a program to run measurably and consistently faster? I'll post the answer later today. (Or as much as I understand, anyway.)

3 comments:

Mitch said...

I vaguely recall reading something about the mozilla people making JS opcode implementations different sized so that the branch predictor had more meat to chew on. Maybe something to do with that? On the other hand, if I've already read about it then presumably it wouldn't have been a mystery.

Some kind of alignment thing? I can't think of how code would be affected by that, though.

Andrey Fedorov said...

A co-worker says "pipeline stall".

Slow Byte said...

Adding the first line of code in that exact form will do nothing if optimization is enabled (it will be removed by dead code elimination).

The asm instruction will change instruction scheduling, register allocation and code layout, affecting the instruction cache, branch predictor and the out-of-order superscalar execution engine of your modern CPU (register renamer, reorder buffer etc.).

On microbenchmarks such small changes can create eg. 3x performance differences (by my own experiments)