33

I'm using C#.NET for a web application. I've read that JIT compilation happens at run-time, which means(correct me if I'm wrong) that the compilation will happen when the request hits IIS.

Another compilation happens using csc.exe during the build phase of the solution using MSBuild to convert high-level code to CIL.

If there was no JIT and we wanted to use AOT, where would AOT fit in all this?

My question is at what point in the entire phase from building code to the first request, AOT compilation happens? (The platform/framework does not matter)

RBT
  • 24,161
  • 21
  • 159
  • 240
chaudharyp
  • 3,394
  • 3
  • 26
  • 42
  • 1
    The "building" is when the AOT compilation happens as just like C, C++ apps the C# code would be compiled directly to machine code for the specific platform that was specified during "building". – shashi Sep 18 '15 at 17:18
  • 2
    Well, the platform/framework do matter. The only complete pure AOT in .NET happens weeks before the typical user runs the program. On a Store server in Redmond, using .NET Native. Doesn't have anything to do with a web app, you can only use Ngen. Reflection code and some generics are still jitted. – Hans Passant Sep 18 '15 at 18:02

2 Answers2

66

After a lot of Googling and research, I found out that my basic understanding of the compilers was wrong.

A compiler is a program that converts a program in language X to a program in language Y. The language Y can be anything(native machine code, intermediate code/bytecode, some other language Z, or the same language itself).

A compiler is not necessarily a program that converts a program in language X to m-code.

For example, a compilation happens from high-level C# code to CIL using a compiler csc.exe. (how could I miss that? duh!)

Also, the time in Ahead-Of-Time and Just-In-Time compilers refers to the runtime. So, in Ahead-Of-Time compiler, the compilation happens before the program is run, usually added as a build step. While in Just-In-Time compiler, the compilation keeps happening while the program is being run.

To put it in C#.NET perspective and to answer my question, the CIL is generated by MSBuild using csc.exe without any consideration whether the CLR uses JIT or AOT compiler. It is at the time of running the program that JIT or AOT compiler comes into action. AOT compiler compiles entire assemblies(in CIL, or language X) into native machine code(language Y) before the program is run. JIT compiler compiles individual methods and classes(in CIL, or language X) into native machine code(language Y) when the methods are called.

CLR provides JIT compiler by default but it also supports AOT compilation using Native Image Generator(Ngen.exe).

chaudharyp
  • 3,394
  • 3
  • 26
  • 42
  • 2
    Good research. Note that the term AOT is usually used when compilation happens at install time. A good example is the new Android Runtime (ART) in Android 5.0+, which compiles apps when they are installed instead of JITing. @shashi commented on your original post about Windows Store apps. MS has been AOT compiling .NET Windows Phone apps, but instead of compiling on the phone at install time, it's on the Windows Store server before it's downloaded and installed by a user. Apple does something similar with Apple Watch apps. – LearningFast Mar 16 '16 at 01:11
  • When exactly happens this: *AOT compiler compiles entire assemblies(in CIL, or language X) into native machine code(language Y) before the program is run* – kravemir May 09 '17 at 13:20
0

Ahead-of-time (AOT) compilation refers to an umbrella of technologies which generate code at application build time, instead of run-time. AOT is not new to .NET.

Existing AOT-compiled .NET assemblies contain platform-specific data structures and native code to frontload work typically done at runtime. Precompiling these artefacts saves time at startup (e.g. ReadyToRun), and enables access to no-JIT platforms (e.g. iOS). If precompiled artefacts are not present, .NET either falls back to JIT or interpretation (depending on the platform).

Reference: https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-3/#what-is-native-aot

Refer to this blog, This is one of the best-detailed blogs I have ever seen. https://medium.com/@MStrehovsky/fight-the-global-warming-compile-your-c-apps-ahead-of-time-9997e953645b

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197