"Turn" analysis of. NET Core Composition System

Source: Internet
Author: User
Tags dotnet

The previous article describes the status of. NET core in the overall. NET platform, and its relationship to the. NET Framework (source link), which details the composition of the. NET Core framework and the main functions of each module, and how to implement cross-platform.

Describes the system composition of. NET Core, the top tier is the application layer, which is the development of a framework set based on the UI application, including the ASP (for creating Web apps), and the UWP (for creating the Windows10 app).

The middle tier is a public library (COREFX) that implements the. NET standard library, which includes common system-level operations such as files, networks, and so on.

Under Corefx is the runtime environment,. NET Core contains two runtimes (CoreCLR, Corert), and CoreCLR is a runtime based on the instant compiler program (Just in Time Compiler,jit). It uses the cross-platform open source compiler Ryujit, and Corert is the runtime using the advance compiler (Ahead of Time Compiler,aot), which can either use Ryujit to implement AOT compilation or use other AOT compilers. Because AOT compiles IL into machine code, it also has better start-up speed and energy saving on mobile devices.

Finally, I want to mention an open source cross-platform source code compiler Roslyn, which differs from just two compilers, JIT and AOT compilers are primarily used to compile IL machine code, and Roslyn is to compile C # or vb.net code into a program intermediate language (intermediate Language,il).

Roslyn compiler

The Roslyn compiler is used to compile C # or vb.net code into an assembly (assembly), and its compilation process is a pipelined process with a total of 4 steps, as described in the procedure.

  A. Parser (parsing)

Parses the source code according to the syntax.

  B. Declaration (statement)

To generate metadata for code (metadata), metadata is a collection of data tables that describe the data types and members defined in the current code, as well as the types and members of the references.

  C. Bind (BIND)

The generated IL code is bound together with the metadata that describes it, generating a managed module (managed module).

  D. Emit (generated)

Merges one or more managed modules into the Build assembly (assembly).

RyuJIT compiler

In the program to run a certain method, the first need to convert the compiled IL to the machine code of the machine, and this task is given to the Ryujit. It is a new generation of JIT compilers that, for the first time, implemented the AMD64 architecture, Ryujit able to generate code faster than JIT64 (the previous generation compiler) to improve program efficiency.

CoreCLR & Corert

Both. NET core runtime (CoreCLR) and. NET Core runtime (Corert) are the runtime (runtime) of. NET core, which provides core functionality similar to the. NET Framework CLR (memory management, programs Load, security, exceptions, thread management, etc.), can be used by all languages that are intended for the runtime.

Unlike Corert and CoreCLR, Corert provides a set of AOT mechanisms that can compile. NET core programs into native code and run on the host machine without relying on. Net. In addition to the two runtime most of the function code is shared, such as GC. The AOT optimization brings many benefits:

    • Compiled into a single file containing all dependencies, including Corert, without the need to install the framework

    • Machine code on startup, no machine code required and no JIT compiler to load

    • Other optimization compilers can be used, including LLILC, IL to CPP

Corert There are two ways to generate machine code, the first use is to directly compile IL into machine code, by default, RyuJIT as an AOT compiler to compile IL into machine code, another way is to compile C # code into C + + code, and then call the corresponding platform C + + compiler optimization compiled into machine code.

  Compiling machine code using RyuJIT

dotnet restoredotnet build--native--ilcpath <repo_root>\bin\product\windows_nt.x64.debug\packaging\publish1

  Compile to generate C + + code

dotnet restoredotnet build--native--cpp--ilcpath <repo_root>\bin\product\windows_nt.x64.debug\ Packagingpublish1--CPPCOMPILERFLAGS/MTD

Corert also has shortcomings, it needs to compile for different platforms once; but it allows engineers to not publish to platforms they do not want to support (such as a game that only supports the desktop and does not support mobile phones).

Note: Both of these names are not available in the. NET Core RC2 version, which is officially removed from the current version, and is not known until the official version of June 27 is issued.

Corefx (. NET Core Libraries)

Corefx mainly contains several public libraries, such as System.Collections, System.IO, System.Xml, etc. Corefx is the implementation of the. NET Standard library, and the same. NET Framework 4.6.3 is also based on the. NET Standard library implementation. They are currently based on the. NET standard Library1.6 version, as detailed in the following table:

. NET Core code Development, deployment, and operational procedures

You can see that using JIT compiling and using AOT to compile source code and run programs are two different processes.

If you are using the JIT compiler to deploy a program with only the assemblies of the program packaged as IL, the compiler compiles the IL to the target machine code (Native code) before the method executes the first time, and AOT compilation compiles the source code directly to the target machine code at compile time.

The AOT compiles the source code into machine code and has the following features:

    • Replace the reflection with static code, for example, if a value type is not overridden by the Equals method of Valuetype.equals, the default is equal, and the reflection is used to find the filedinfo to determine whether the type is equal, The value is then compared for equality. In AOT compilation, you can only compare value for equality because it replaces reflection.

    • Dependent third-party class libraries and. NET libraries are packaged into the final compiled program.

    • The packaged program runs on a compact version of the runtime (Corert) primarily contains the garbage collector, and the runtime is packaged in an app file.

    • Although the reflection code is replaced at compile time, when the dynamic reflection code is powerless, the runtime throws an exception if the corresponding metadata and implementation are not found when the dynamic reflection call is encountered. The workaround is to configure the run-time Directive file (runtime, Directive file) to specify the assembly to use before compiling.

Summarize

This section describes the architecture of. NET core, including the addition of multiple compilers and the Corefx of the. NET Standard library, which, overall, has a significant improvement in performance and development efficiency compared to the previous. NET Framework. The key is for the first time. NET of the complete cross-platform capabilities of the underlying technology stack.

. NET core is based on cross-platform capabilities and does not migrate GUI-highly-related APIs into. NET core, so Windows Forms or Windows Presentation Foundation (WPF) are not ported to. N The ET core. NET Core supports the console application (console application) as well as the class library type of project.

However, Microsoft uses. NET Core in its Universal Windows Platform (UWP) development platform and uses. NET Native technology to increase its performance to very close to native code speed.

ASP. NET core drives its managed Environment Kestrel Server in a console application to support the operation of an ASP. NET Core program.

"Turn" analysis of. NET Core Composition System

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.