What are these spurious nop instructions doing in my C# code?

Raymond Chen


Prerequisites: Basic understanding of assembly language.
When you debug through some managed code at the assembly level, you may find that there are an awful lot of nop instructions scattered throughout your method. What are they doing there; isn’t the JIT smart enough to remove them? Isn’t this going to slow down execution of my program?
It is my understanding that¹ this nop instructions are inserted by the JIT because you’re running the program under the debugger. They are emitted specifically so that the debugger can set breakpoints in locations that you normally wouldn’t be able to. (For example, they might represent a line of code that got optimized out or merged with another line of code.)
Don’t worry. If there’s no debugger, the JIT won’t generate the dummy nops.
Nitpicker’s Corner

¹As with all statements of alleged fact, this statement is an interpretation of events based on observation and thought and does not establish a statement of the official position of the CLR JIT compiler team or Microsoft Corporation, and that interpretation may ultimately prove incorrect.

Raymond Chen
Raymond Chen

Follow Raymond