Get ready for NativeAOT in .NET 7

Maourice Gonzalez

dotnet-7-preview-2

.NET 6 is nowhere near done blowing our minds and here comes Microsoft with another preview of .NET 7. However this time we aren’t going to talk about what Preview 2 itself brings, but instead what it’s getting us ready for, and that is:

NativeAOT

No, you are not dreaming, NativeAOT is almost here. Ahead of Time Compilation builds everything upfront, to avoid needing to “JIT” when an app runs for the first time. The project is moving out of experimental status and onto the main runtime branch as of now. For quite some time the team has been working tirelessly to bring proper Native Ahead-of-Time compilation to dotnet. While partial AOT has always been present, full “true” support was never there out of the box.

There is still work to be done, but you can start getting ready by following these suggestions to avoid complications.

AOT Benefits

Ahead-of-Time compilation offers many benefits like:

  • faster startup times
  • mostly predictable performance regardless of the platform

JIT Benefits

Just-in-Time compilation also has its benefits with things like:

  • often faster performance as it targets the platform its about to run on
  • being able to dynamically adapt and re-compile to deliver improvements
  • smaller disk footprint

Both approaches have their place, the key here is choice, now developers will have the freedom to chose which model best fits their needs. While not for everyone, this feature will surely make a substantial amount of the developer community happy.

For those unaware, there are many places where AOT is implemented in some shape, form or fashion. One example of that is Xamarin.iOS which manages to achieve both quick app startup and considerably small file sizes. With Xamarin.Android it is a bit of a different story. The issue here has always been file size, even though startup time is drastically improved, you always end up with somewhat large binaries. In theory there are ways to improve this. For example by implementing full static compilation, however I am not sure why this was never achieved on Android while it was possible for iOS. If you happen to know the reason, please sound off in the comments below.

There are a ton of other improvements and new features as part of .NET 7 Preview 2. Improvements to ARM64, new Regex Source Generator and more! You can read about all of these more in depth by heading over to the official announcement blog post.