More effective tive C #: use generic

Source: Internet
Author: User

Since the generics added by. NET 2.0, the methods and methods for coding are greatly affected. Generics are not only applied to collections, but also have a huge impact on interfaces and extraction algorithms.

The generic class definition can be fully compiled into the msil type. For any type parameter that meets the constraints, the Code contained in the generic type must be completely legal. All the generic types specified by all type parameters are called closed generic types. Only some generic types of some type parameters are called open generic types.

It is incorrect to simply use generics to reduce the program size, factors that affect the program size include the number of type parameters used in the program and the number of closed generic types created.

The generics in Il can be seen as part of an actual type definition. Il reserves placeholders for initializing a complete generic instance. The JIT compiler will complete the definition of the closed generic type when generating machine code at runtime. This solution brings about a conflict: the disadvantage is that multiple closed generic types increase the overhead of code processing, and the advantage is that the time/space overhead of data storage reduces.

Different closed generic types may lead to different final runtime forms of code generation. When multiple closed generic types are created, the JIT compiler and CLR are optimized to reduce the pressure on storage. The IL-form assembly is loaded to the data page in the memory. The generated machine code is placed in the read-only code page only after the JIT compiler converts the Il code into machine instructions.

No matter whether it is generic or not, each type executes the above process. For non-generic types, the relationship between the class il code and the machine code it generates is one-to-one, the emergence of generics makes the conversion process slightly more complex. When JIT converts a generic class, the JIT compiler checks the current type parameter, generate specific commands based on the information. The JIT compiler will perform a series of Optimizations to this process so that different types of parameters can use the same machine code.

For generic classes generated by reference types, the JIT compiler generates a unique machine code version. Let's look at the following code.

 1         private static void InitRefTypeGen()
2 {
3 List<string> stringList = new List<string>();
4 List<Stream> openFiles = new List<Stream>();
5 List<Employee> empList = new List<Employee>();
6 }

The machine code of the above Code is identical at runtime.

For generic classes generated by value types, the JIT compiler creates machine codes of different versions for different types of parameters. Let's look at the following code.

 1         private static void InitValTypeGen()
2 {
3 List<double> doubleList = new List<double>();
4 List<int> intList = new List<int>();
5 List<Name> nameList = new List<Name>();
6 }

The machine code of the above Code is different at runtime.

When JIT needs to compile a generic definition during runtime and at least one type parameter is a value type, this process can be divided into two steps: 1. the compiler creates a new il class to indicate the closed generic type. 2. JIT compiles the code into x86 commands. These two steps are necessary because JIT does not generate a complete x86 command for a class when it is loaded, instead, the compilation starts only when each method in the class is called for the first time. In this way, it is necessary for the framework to execute a replacement step in the Il code, and then compile the code as needed like a normal class definition.

This also means that the additional memory usage during the runtime will be divided into the following two parts: 1. Save a copy of The IL definition for each closed generic type with the value type as the parameter. 2. Save a copy of the machine code of the called method for each value type as the closed type of the parameter.

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.