Monday, January 30, 2012

nop – No Operation

If you ever looked at your code compiled in DebugMode Versus ReleaseMode you most certainly saw a few differences. One of those differences is the higher presence of the nop IL instruction in the DebugMode. What does this instruction do? Well, nothing "meaningful"...
Take a look at the following simple example:

(click to enlarge)

As you can see, I have a breakpoint set on the bracket. If we compile this in DebugMode you can see that the compiler inserted a nop instruction right after the anonymous methods "signature":

(click to enlarge)

And in release mode, it didn't:

(click to enlarge)

The compiler inserts nop instructions as placeholders for the non-executing lines of code (since non-executing lines of code are not translated into IL instructions). This is the reason why we can set breakpoints on brackets. The nop instruction does not perform any "meaningful" work yet it can consume a processing cycle.