Book Image

C# 7 and .NET Core 2.0 High Performance

By : Ovais Mehboob Ahmed Khan
Book Image

C# 7 and .NET Core 2.0 High Performance

By: Ovais Mehboob Ahmed Khan

Overview of this book

While writing an application, performance is paramount. Performance tuning for realworld applications often involves activities geared toward fnding bottlenecks; however, this cannot solve the dreaded problem of slower code. If you want to improve the speed of your code and optimize an application's performance, then this book is for you. C# 7 and .NET Core 2.0 High Performance begins with an introduction to the new features of what?explaining how they help in improving an application's performance. Learn to identify the bottlenecks in writing programs and highlight common performance pitfalls, and learn strategies to detect and resolve these issues early. You will explore multithreading and asynchronous programming with .NET Core and learn the importance and effcient use of data structures. This is followed with memory management techniques and design guidelines to increase an application’s performance. Gradually, the book will show you the importance of microservices architecture for building highly performant applications and implementing resiliency and security in .NET Core. After reading this book, you will learn how to structure and build scalable, optimized, and robust applications in C#7 and .NET.
Table of Contents (11 chapters)
5
Designing Guidelines for .NET Core Application Performance

New improvements in .NET Core 2.0

The most recent version of .NET Core, 2.0, comes with a number of improvements. .NET Core 2.0 is the fastest version of all times and can run on multiple platforms including various Linux distros, macOS (operating system), and Windows.

Distros stands for Linux distribution (often abbreviated as distro), and it is an operating system made from a software collection, which is based upon the Linux kernel and, often, a package management system.

Performance improvements

.NET Core is more robust and performance efficient and, since it's open source, the Microsoft team with other community members are bringing more improvements.

The following are the improvements that are part of .NET Core 2.0.

RyuJIT compiler in .NET Core

RyuJIT is a next-generation JIT compiler that is a complete rewrite of the Just In Time (JIT) compiler and generates a lot more efficient native machine code. It is twice as fast as the previous 64-bit compiler and provides 30% faster compilation. Initially, it runs on only X64 architectures, but now it supports X86 as well and developers can use the RyuJIT compiler for both X64 and X86. .NET Core 2.0 uses RyuJIT for both X86 and X64 platforms.

Profile guided optimization

Profile-guided optimization (PGO) is a compilation technology used by C++ compiler to generate optimized code. It applies to the internal native compiled components of the runtime and JIT. It performs compilation in two steps, which are as follows:

  1. It records the information about code execution.
  2. From this information, it generates better code.

The following diagram depicts the life cycle of how the code is compiled:

In .NET Core 1.1, Microsoft already released the PGO for Windows X64 architecture, but in .NET Core 2.0, this has been added for both Windows X64 and X86 architectures. Also, as per observatory results, it was noted that the actual startup time is mostly taken by coreclr.dll and clrjit.dll for Windows. Alternatively, on Linux, there are libcoreclr.so and libclrjit.so, respectively.

Comparing RyuJIT with the old JIT compiler known as JIT32, RyuJIT is more efficient in code generation. The startup time of the JIT32 was faster than the RyuJIT; however, the code is not efficient. To overcome the initial startup time taken by the RyuJIT compiler, Microsoft used PGO, which brought the performance closer to JIT32 performance and achieved both efficient code and performance on startup.

For Linux, the compiler toolchain is different for each distro, and Microsoft is working on a separate Linux version of .NET that uses the PGO optimizations applicable to all distros.

Simplified packaging

With .NET Core, we can add libraries to our project from NuGet. All framework and third-party libraries can be added as NuGet packages. With a large sized application that refers many libraries, adding each library one by one is a cumbersome process. .NET Core 2.0 has simplified the packaging mechanism and introduced meta-packages that can be added as one single package that contains all the assemblies that are linked to it.

For example, if you wanted to work on ASP.NET Core in .NET Core 2.0, you just have to add one single package, Microsoft.AspNetCore.All, using NuGet.

The following is the command that will install this package into your project:

Install-Package Microsoft.AspNetCore.All -Version 2.0.0