How does Visual Studio Edit and Continue work?

5

Visual Studio allows you to edit the C #, VB.NET, or C ++ code in debug when you hit a breakpoint or click the break button (pause icon). The tool name is Edit and Continue . According to the documentation

  

is a productivity feature that allows you to make changes to your source code while your program is in break mode.

This tool does not work for all editions. Change a class attribute or class in C #, for example, you need to stop the debug and compile again. See "Supported code changes (C # and Visual Basic)" .

How does this run-time edit work? When I edit a snippet of code, does the whole project of the edited file and its dependencies recompile? Or is the edited portion just recompiled? Is there a way to "recompile" only one method?

    
asked by anonymous 25.10.2017 / 17:11

1 answer

5

I do not have authoritative information and I think it largely depends on the specific implementation that can change as needed.

In C # and VB.NET it's more or less easy the actual code that runs is generated at run time through JITter , so there is already a build in execution even when you will not use Edit and Continue . Of course it's a more limited build, it just takes a bytecode and generates executable code. JITter can compile only what matters, it can even recompile something if it feels it is needed (as far as I know .NET does not do this, but could as optimize during execution when it identifies a hot path ) .

I believe that bytecode recompilation occurs only in the modified method, but this depends on the implementation.

Source recompilation in bytecode could occur only in this method, but you have a chance to be at least in the whole file of it to see if everything is right there in that source. The .NET Compiler Platform is designed to be able to compile only parts of a code, even a smaller part than a method , although this will be useful in very specific circumstances.

There is probably metadata insertion and even dead instructions ( nop ) in generating the code to facilitate recompilation .

Once the recompilation is done, JITter will use some strategy to replace the code, or putting it in the same memory area (perhaps even making a detour), most likely, or updating the references contained in every application for the method at the new address.

As you have already noted, it is only possible to change imperative code (with limitations), you can not change the data structures. But you can create new ones.

In C ++ a larger infrastructure is needed because it does not have a JITter. which means that you have to put a JITter in the application.

    
25.10.2017 / 17:31