<< Exhibition C # >> Chapter 2 NGWS Runtime Technology Basis (Revised)

zhaozj2021-02-08  370

Chapter 2 NGWS Runtime Technology Foundation

Since you have a comprehensive impression of C #, I also want you to appreciate the whole picture of NGWS Runtime. C # relies on the runtime provided by NGWS; therefore, it is necessary to know how to work at runtime and the concepts contained behind it.

Therefore, this chapter is divided into two parts - they are the foundation of all concepts and applications. Although the contents of the two are some overlap, it helps to deepen the understanding of the understanding.

2.1 NGWS Runtime

NGWS and NGWS Runtime provide you with a runtime environment. This runtime manages execution code and provides services that make programming easier. As long as your compiler supports this runtime, you will benefit from this managed execution environment.

You guess the C # compiler supports NGWS runtime is correct, but not only supports NGWS Runtime, VB, and C . These are called "managed code" for supporting runtime. The following is the benefits of your app from NGWS Runtime:

· Inter-language platform integration (through universal language specification)

· Automatic memory management (garbage collection)

· Inter-language platform abnormal processing (unified expansion)

· Enhance security (including type security)

· Version Support ("DLL Hell" Terminator)

· Component interaction simplification mode

Because NGWS Runtime provides all these benefits, the compiler must send the meta file and the managed code. The type in the meta file description code, it is with your code (similar to PE - PE is a variable position)

As you can see from many cross-language platform features, NGWS Runtime is primarily integrated, across a variety of platforms. This support is even allowed to give a C # class from a VB object (I will give an article to be discussed later).

A function that C # programmers will like is that they don't have to worry about memory management - that is to say, don't worry about the notorious memory leak. NGWS Runtime provides memory management, and the garbage collector releases them when the life of the objects and variables (no longer referenced). I really like this feature, because memory management in COM has always been a piece of heart disease.

You should encourage a management application or component. Because the management application contains metadata files, NGWS Runtime can take advantage of this information to ensure that your application has the various provisions it needs. The obvious effect produced is that because your code does not depend on the dependence, it may raise an interrupt.

The remainder of this chapter is divided into two sections, and each section discusses all aspects of NGWS Runtime until your C # application can perform.

· Intermediate language (Intermediate language, abbreviation IL) and metadata

· Instant Compiler (Just-Time Compliers, Jicts)

2.1.1 Intermediate language and metadata

The managed code generated by the C # compiler is not the original code, but it is an intermediate language (IL) code. This IL code itself becomes the entry of the managed execution process of NGWS Runtime. The obvious advantage of IL code is that it is a CPU, which means that you want to convert the IL code into the original code with a compiler on the target machine.

Although the IL code is generated by the compiler, it is not something that the compiler is provided to the runtime. The compiler also generates the metadata of the code, tells the runtime about more things about the code, such as various types of definitions, the names of various types, and other data. Basically, metadata is a type library, registry content, and other information for COM. Despite this, the metadata is directly combined with the execution code and is not separated.

IL and metadata stored in a file extended in the PE format (PE format for .exe and .dll files). When such a PE file is loaded, position and separate the metadata and IL from the file when running.

Now, I give a short directory for existing IL instructions. Although it is not a full list, you don't need you to know and understand, but it provides you with the basics that C # programming must have. · Arithmetic and logical operators

· Control flow

· Direct memory access

· Stack operation

· Parameters and local variables

· Stack allocation

· Object mode

· Instance type value

· Critical area

· Array

· Type position

· Instant Compiler (Jitters)

2.1.2 Instant Compiler (Jitters)

The managed code generated by C # or other compiler that generates the managed code is an IL code. Although the IL code is packaged in an effective PE file, you still can't execute it unless it is converted into a managed original code. This is when the NGWS runtime instant compiler (also known as jitters) is a big show.

Why do you feel more than instant compiled code? Why don't you compile the entire IL PE file into the original code? The answer is time - you need to compile the IL code to the code of the CPU specification. This compilation will be more efficient because some blocks have never been executed. For example, in my word processor, the mail merge function has never been compiled.

As a result, all processing processes are as follows: When a type is loaded, the loader creates a stub and connects it to each type. When a method is called for the first time, the stub gives the control over the JIT. JIT compiles IL as the original code and points the stub pointer to the original code loaded into the buffer. Then call the original code to be executed. In some positions, all IL is converted into original code, and Jitter is idle.

As I mentioned earlier, the JIT compiler has a lot, more than one. On the Windows platform, NGWS Runtime has 3 different JIT compilers.

JIT - This is the JIT compiler used by NGWS Runtime default. It is a background optimized compiler that implements data flow analysis in the front desk, and creates highly optimized managed original code as output results. JIT can use uncompected IL instruction sets, but the required resources will be considerable. The main limit is that the memory footprint, the resulting work set, and the time to implement the optimization.

Compared to the main JIT, ECONJIT's goal is to convert the IL to the host code. It allows the generated raw code to be loaded into the buffer, but the output code is not optimized as the code generated by the primary JIT. When the memory is tight, the advantage of the fast code generation scheme will disapperse. Permanently discarding useless, JIT's code, you can load larger IL programs into the code buffer. Because JIT compilation is fast, the execution speed is still very fast.

Prejit - Although it is based on the primary JIT, it is more like a traditional compiler. You have installed the NGWS component, which can run before you can compile the IL code into a managed original code. Of course, the final result is, faster load time and faster application start time (no more JIT compilation).

In the Jitters listed, there are two Jitters when running. But how do you decide which JIT want to use, how do it use memory? There is a small application called "JIT Compile Manager", which is stored in the bin directory in the NGWS SDK installation directory. When the program is executed, it adds an icon to the system task bar and double-click the icon to open the program dialog (see Figure 2.1).

Figure 2.1 JIT Compilation Manager allows you to set up a variety of related performance options

Although it is a small dialog, you have chosen the option functionality. Each option will be described below.

Use econojit Only Options - When the check box is not selected, NGWS Runtime uses the default normal JIT compiler. The difference between two Jitter has been explained earlier.

Max Code Pitch Overhead (%) Option - This setting is only reserved to ECONJIT. It controls the percentage of JIT compilation time and execution code time. If the set value is exceeded, the code buffer is expanded to shorten the time consumed by JIT compilation. Limit size of code cache option - This item is not selected. No choice This item means that the buffer will utilize the memory it can get. If you want to limit the buffer size, select this option, which will allow you to use the max size of cache (Bytes) option.

Max size of cache (bytes) option - Controls the maximum value of the buffer that accommodates the JIT code. Although you can limit this value very strictly, you should also be careful, you can't exceed the maximum of this buffer. Otherwise, the JIT compilation of the method will fail.

Optimize for size option - telling the JIT compiler, optimized to make the code smaller rather than can be implemented faster. This setting is turned off by default.

Enable Concurrent GC [Garbage Collection] - Waste Collection (GC) is running in the thread of user code by default. When the GC occurs, it may notice that the response has a slight delay. To prevent this phenomenon, open the current GC. Note that the current GC is slower than the standard GC, which is only valid when writing on Windows 2000 (The Time of Writing).

When you create an item with a C #, you may use different setup tests. When you create a UI-INTENSIVE application, you will see the maximum difference that allows the current GC.

2.2 Virtual Object System (VOS)

So far, you only see how NGWS runtime work, but does not understand the technical background of it work and why it works like this. This section is about the NGWS virtual object system (VOS).

The following is a rule of NGWS Runtime when forming a statement, use and management type model in VOS. The idea behind the VOS is to create a framework that cannot sacrifice performance when performing code, allowing cross-platform language integration and type.

The framework I mentioned is the basis of the runtime architecture. In order to help you know it better, I will have four regions. Understanding them is important when developing C # applications and components.

VOS Type System - provides a rich type of system, which intends to support full implementation of comprehensive programming languages.

Metadata - describes and references the type defined by the VOS type system. The permanent format of metadata is independent of the programming language, but the metadata is used as an interchange mechanism, which is between the virtual execution system of the tool and NGWS.

Universal Language Normative (CLS) - CLS defines subsets of the type in VOS, which also define conventional usage. If a class library complies with the rule of CLS, it ensures that the class library can be used on all other programming languages ​​that enable CLS.

Virtual Execution System (VES) - This is a VOS real-time implementation. VES is responsible for loading and executing the program written as NGWS.

These four parts form the NGWS Runtime architecture. Each portion is described in the following section.

2.2.1 VOS type system

The VOS type system provides a rich type of system, which is intended to support the full implementation of a variety of programming languages. Therefore, VOS must support object-oriented language and process programming languages.

There are now many types of approximate but a bit incompatible. Take the integer as an example, in VB, it is 16-bit long, and in C , it is 32 bits. There are more examples, especially in dates and times, and database data types. This incompatibility makes the application's creation and maintenance unnecessarily complex, especially when the program uses a variety of programming languages.

Another problem is that because there is some difference between programming languages, you cannot reuse the types created in another language in a language. (COM solves this problem in part of the binary standard interface). Today's code reuse is definitely limited.

The biggest obstacle to publishing applications is that the object model of various programming languages ​​is not uniform. There is almost every aspect: events, attributes, permanent preservation, etc. VOS will change this phenomenon. VOS defines the type of description value and specifies a contract that must be supported by all values ​​of the type. There are two values ​​and objects that support object-oriented and process programming languages.

For values, the type is stored in the expression, and the same operation is also implemented. The object is more powerful because it explicitly in the expression. Each object has an identification number distinguished from other objects. Supports different VOS types in Chapter 4, "C # Type".

2.2.2 yuan data

Although metadata is used to describe and reference the type defined by the VOS type system, it does not lock to this single target. When you write a program, by using the type declaration, you declare the type (assuming that they are numeric or reference types) is introduced to the NGWS Runtime type system. Type declarations are described in metadata stored inside the PE executable file.

Basically, metadata is used for various tasks: information to represent NGWS runtime purposes, such as positioning and loading classes, these classes in memory, resolve calls, translation IL is the original code, strengthen security and set up the context boundary .

You don't have to care about the generation of metadata. Metadata is generated by C # "Code-to-IL Compiler, not JIT compiler). The code transfer IL compiler sends binary metadata information to the PE file, which is sent in a standard manner, which is not like the C compiler, and creates their own modified names for the exit function.

The main advantages you obtained from metadata and executable code are, the same type of information is fixed with type, which will not spread a lot. It also helps to solve the problem that exists in CoM. Further, you can use a different repository in the same context, because the library is not only registered reference, but also included in the executable code.

2.2.3 Universal Language Specification

Universal Language Specification (CLS) is not a virtual object system (VOS) true part, it is special. CLS defines a subset in VOS, which also defines conventional usage that must comply with CLS.

So what is confused about this? If a class library complies with CLS rules, other programming languages ​​also follow CLS rules, then other programming languages ​​can also use the class library. CLS is an interactive operability of the language (Interoperability). Therefore, conventional usage must only follow external accessible items such as methods, attributes, and events, and more.

The advantage of what I have described is that you can do the following. Write a component with C #, derived it in VB, because the functionality added in VB is so powerful, derived from the VB class again in C #. As long as all external accessible items follow the CLS rules, it is feasible.

I don't care about the CLS protocol in this book. But when building your class library, you should pay attention to the CLS protocol. I have provided Table 2.1 to define an agreement rule for the type and external accessible item.

This list is incomplete. It contains only some important projects. I don't point out that there is a CLS protocol in this book, so there is a good idea: When you look for a CLS protocol, you should at least browse this table to find out which function is valid. Don't worry that you are not familiar with every meaning in this table, you will learn them in this book.

Table 2. Types and functions in the functional language specification

· BOOL

· Char

· Byte

· Short

Int

LONG

· Float

Double

String

· Object (Mother of all objects)

Arrays (array)

The number of dimensions of the array must be known (> = 1), and the minimum number of claims must be 0.

Element type must be a CLS type.

Type (Types)

Can be abstract or hidden.

Zero or more interfaces can be implemented. Different interfaces allow for a way to have the same name and signature.

A type can be derived accurately from a type. Allow members to be covered and hidden. You can have zero or more members, they are domain members, methods, events, or types.

Types can have zero or more constructor.

One type of accessibility can be a common or for NGWS components; however, only common members can think that part of the type interface.

All value types must be inherited from system value type. An exception is an enumeration - it must inherit from system enum.

Type member

Type members allow hidden or override other members in another type.

The types of parameters and return values ​​must be a CLS protocol type.

Constructor, methods, and attributes can be overloaded.

A type can have abstract members, but only when the type is not packaged.

method

One method can be static, virtual or instances.

Virtual and instance methods can be abstract or one implementation. The static method must always have an implementation.

The virtual method may be the last (or not).

Domain member

Can be static or non-static.

Static domain members can be described or only initialized.

Attributes

When getting and setting methods rather than using attribute syntax, the property can be disclosed.

The first parameter of the returned type and setting method must be the same CLS type-attribute type.

Attribute names must be different, different attribute types are used to distinguish it is not sufficient.

Since the use method implements attribute access, if PropertyName is a property defined in the same class, you cannot realize how to name Get_PropertyName and SET_PROPERTYNAME.

Properties can be indexed.

Property Access must follow this name format: get_proname, set_propname.

enumerate

Emphasis on the type must be byte, short, int or long.

Each member is an enumerable type of static description domain member

An enumeration cannot implement any interface.

You allow multiple domain members to set the same value.

An enumeration must inherit the system enumeration (implied in C #)

abnormal

Can be triggered and captured.

Custom exception must inherit the system anomaly.

interface

Other interfaces can be required.

An interface can define attributes, events, and virtual methods. The implementation depends on the derived class.

event

Either all methods and cancellation methods are performed or not. Each method uses a parameter, which is a class that is derived from the system delegate.

Custom attribute

You can use only the following types: Type, CHAR, CHAR, BOOL, BYTE, SHORT, INUM, LONG, FLOAT, DOUBLE, ENUM (a CLS type), And Object.

Representative Yuan (Delegates)

Can be created and activated

Identifier

The first letter of an identifier must come from a restriction set.

By size in a single range, it is impossible to uniquely distinguish two or more identifiers (case insensitive).

2.2.4 Virtual Execution System (VES)

The virtual execution system implements a virtual object system. Create VES by implementing an execution engine responsible for NGWS Runtime (Execution Engine, Abblast EE). This execution engine performs applications written and compiled by C #.

The following components are part of VES.

1. Intermediate Language (IL) - is designed to be easily compatible with a variety of compilers. In addition to this frame, both C , VB, and C # compilers can generate IL.

2, load the managed code - including the names, surface layers, and create JIT compilation, and create JIT compilation. By performing a regular check, including strengthening some access rules, the type loader also enhances security.

3, use JIT conversion IL into the original code - IL code is not designed to become a traditional interpretation byte code or tree code, IL conversion is a true compilation.

4, load metadata, check type security and method integrity

5, garbage collection (GC) and abnormal treatment - both are based on the stack format. The managed code allows you to dynamically track the stack. To dynamically identify each stack framework, Jitter or other compiler must provide a code manager.

6, depicting and checking the wrong service - both depend on the information generated by the source language compiler. Two mappings must be issued: a mapping from the source language structure to the address in the instruction stream, a mapping from the address to the position of the stack frame. These mappings are recalculated when the conversion from IL to the original code is performed. 7. Manage threads and contexts, and remote management -ves provide these services for managed code.

Although this list is not complete, it is enough to let you understand the low-level architecture based on VES based on runtime. There will definitely be a book that specializes in discussing runtime, and this book is only slightly in-depth to explore various topics.

2.3 small knot

This chapter, I take you to the world of running. Describes how it works when creating, compiling, and configuring C # programs. You have learned the intermediate language (IL), and how the metadata is used to describe the type that is compiled into IL. Metadata and IL are used for Jitter detection and execution code. You can even choose which JITTER to perform an application.

In the second part of this chapter, what is the theory that works at this way when it is runtime. You have learned the virtual object system (VOS) and the part that makes it. For class library designers, they are most interested in Universal Language Normative (CLS), which is based on VOS-based language coextrocity setting rules. Finally, you see how virtual execution system (VES) implements VOS through NGWS Runtime.

转载请注明原文地址:https://www.9cbs.com/read-809.html

New Post(0)