On 7/30/07, Dinesh Joshi dinesh.a.joshi@gmail.com wrote:
Exactly. So does this happen even with optimization turned off? If it does then shouldn't my code in query two cause a miss in the instruction pipeline all the time?
Again, we come to the implementation part. Depends on the processor on which you run the code on. It all depends on the branch prediction logic. But here branch prediction doesn't come into picture! The JMP instruction is an unconditional jump. Your code will generate assembly like this:
Firstly, good example.
True, the implementation is processor-specific. But AFAIK almost all processors in the market today implement instruction pipelining. Differences lie in the number of instructions pre-fetched and the branch prediction technique (anything else that impacts this example?). Branch prediction does not figure here since the while(true) becomes an unconditional jump. The actual number of instructions pre-fetched only tell us about the degree to which the impact occurs in case of a miss; I'm not concerned about the degree, I'm only concerned about whether it does make an impact.
The code inside the while block remains keeps cycling in the pipeline until the conditional branch somewhere in between evaluates to true, which causes a jump to the statement outside the loop. The entire loop code will have to be unloaded since all of it is a miss. Bigger the loop, larger the miss (to the extent of the max number of instructions pre-fetched).