Book Image

Improving your C# Skills

By : Ovais Mehboob Ahmed Khan, John Callaway, Clayton Hunt, Rod Stephens
Book Image

Improving your C# Skills

By: Ovais Mehboob Ahmed Khan, John Callaway, Clayton Hunt, Rod Stephens

Overview of this book

This Learning Path shows you how to create high performing applications and solve programming challenges using a wide range of C# features. You’ll begin by learning how to identify the bottlenecks in writing programs, highlight common performance pitfalls, and apply strategies to detect and resolve these issues early. You'll also study the importance of micro-services architecture for building fast applications and implementing resiliency and security in .NET Core. Then, you'll study the importance of defining and testing boundaries, abstracting away third-party code, and working with different types of test double, such as spies, mocks, and fakes. In addition to describing programming trade-offs, this Learning Path will also help you build a useful toolkit of techniques, including value caching, statistical analysis, and geometric algorithms. This Learning Path includes content from the following Packt products: • C# 7 and .NET Core 2.0 High Performance by Ovais Mehboob Ahmed Khan • Practical Test-Driven Development using C# 7 by John Callaway, Clayton Hunt • The Modern C# Challenge by Rod Stephens
Table of Contents (26 chapters)
Title Page
Copyright and Credits
About Packt
Contributors
Preface
8
What to Know Before Getting Started
17
Files and Directories
18
Advanced C# and .NET Features
Index

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.

Note

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