CLR via note 5.3 bin and unboxing of value types

Source: Internet
Author: User

1. Packing

In order to convert a value type to a reference type, a mechanism called boxing (Boxing) is used.

1. Allocate memory in the managed heap. The amount of memory allocated is the amount of memory required for each field of a value type plus two additional members (type object pointer and Synchronous block index) for all objects of the managed heap.

2. The value type of the field is copied to the newly allocated heap memory.

3. Returns the address of the object. Now, this address is a reference to an object, and the value type is now a reference type.

2. Unpacking

All the fields contained in the boxed object must be copied to the value type variable, which is on the thread stack. The CLR completes this copy operation in two steps.

The first step is to get the addresses of the individual fields in the boxed object. This process is known as unboxing (unboxing).

The second step is to copy the values contained in these fields from the heap to the stack-based value type instance.

Unpacking does not directly reverse the boxing process. The cost of unpacking is much lower than boxing. Unpacking is actually the process of getting a pointer to the original value type (data field) contained in an object. In fact, the pointer is pointing to the unboxed part of the boxed instance. So, unlike boxing, unpacking does not require any bytes to be copied in memory. Once you know this important distinction, one of the things you should know is that you will be asked to do a copy of the field once the unpacking operation occurs.

Obviously, boxing and unboxing/copy operations can adversely affect the speed and memory consumption of your application, so be aware of when the compiler produces code to automate these operations, and try to write code manually to avoid the automatic generation of code.

As an example:
1          Public Static voidMain ()2         {3Int32 v =5;//to create an unboxed value type variable4Object o = v;//o referencing a boxed Int32 containing a value of 55v =123;//to modify an unboxed value to 1236 7Console.WriteLine (v +", "+ (Int32) o);//Show "123, 5"8}

Let's guess how many times this code has been boxed? If it is 3 times, will it be an accident?

Let's take a closer look at the code and understand what's going on.

The first boxing operation is the fourth row, and the unboxed value type instance (v) is re-given to the reference type.

The second and third is actually a concatenation of strings, WriteLine requires a string object, so you have to take some way to merge the data items to create a string.

In order to create a string,c# compiler to generate code to invoke the static method Concat of a String object. The method has several overloaded versions, and all versions perform the same operation, the only difference being the number of parameters, and this compiler chooses the following version of the Concat method:

public static String Concat (object arg0, Object Arg1, Object arg2);

That is, the second boxing is the unboxed value type V into the type of object, the third is the strong turn to Int32, unboxing after unpacking, and the new boxed instance of the memory address passed to the concat arg2 parameter. The Concat method invokes the ToString method of each object specified, and connects the string representation of each object. The string object returned from Concat is passed to the WriteLine method to display the final result.

It should be noted that if you want to write a call to WriteLine as follows, the resulting IL code will have higher execution efficiency:

Console.WriteLine (v + "," + O); Show "123, 5"

This is almost exactly the same as the previous version, except that the variable O (Int32) Forced transformation was removed, avoiding a single unboxing and one-time boxing, which would have a higher efficiency.

Recommended method:

Console.WriteLine (v.tostring () + "," + O); Show "123, 5"

The ToString method is now called for the unboxed value type instance V, which returns a string. The string object is already a reference type, so it can be passed directly to the Concat method without any boxing operations.

CLR via note 5.3 bin and unboxing of value types

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.