munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.75k stars 1.03k forks source link

I thought C# is just compiled, not compiled & interpreted? #986

Closed TheVeryStarlk closed 2 years ago

TheVeryStarlk commented 3 years ago

In A Map of the Territory 2 . 3 Compilers and Interpreters, there's a picture that shows compiled and interpreted language, with an overlapping region showing languages that are both compiled and interpreted, and it happens that C# is one of them, which confuses me; Because I've heard there's JIT in .NET's VM and JIT means just in time compilation, so I don't see does "interpreter" fit in there?

kandersen commented 3 years ago

There is indeed both, and there is more than just compilation and interpretation going on.

There's a true compilation step in the execution of C#, from C# to CIL bytecode.

That bytecode is loaded by the VM, and depending on execution strategy, VM configuration and implementation, it will likely start interpreting the bytecode directly, to minimize startup time - then it will begin compiling pieces to native code as it discovers "hotspots", or perhaps optimize ("translate") bytecode to bytecode as it discovers that e.g. an interface only has a single implementation, so it can simplify the method dispatching at calls to that interface, remove arithmetic or bounds checks as runtime information convinces the VM that they are not needed etc, etc. The list goes on :)

Disclaimer: passing familiarity with the architecture of V8 - no familiarity with the internals of .net