Brief description: C # packing and unpacking
Concepts:
The value type and reference type are briefly described before packing and unpacking:
Value Type: original type (Sbyte, Byte, Short, Ushort, Int, Uint, Long, Ulong, Char, Float, Double, Bool, Decimal), enumeration (enum), structure (struct), is to allocate memory in the stack, initialization at the same time as the Declaration, to ensure that the data is not NULL;
Reference Type: Class, array, interface, Delegate, String, etc. allocate memory in the heap. The Initialization is null. The reference type requires garbage collection to recycle the memory. The value type is not used, the system will be released automatically when the limit is exceeded;
Packing: Convert the value type to the reference type to store the value type in the garbage collection heap, it is an implicit conversion of the value type to the object type or to any interface type implemented by this value type;
Unpacking: converts a reference type to a value type, from the object type to the value type, or from the interface type to the explicit conversion of the Value Type of this interface;
The binning and unboxing functions allow conversion between any value of the value type and the value of the Object type to link the value type with the reference type.
Example:
Int val = 100;
Object obj = val;
Console. WriteLine ("object value = {0}", obj );
This is a process of packing and converting the value type to the reference type.
Int val = 100;
Object obj = val;
Int num = (int) obj;
Console. WriteLine ("num: {0}", num );
This is a box-breaking process. It is the process of converting the value type to the reference type and then converting the reference type to the value type.
(Note :. in. NET, the data type is divided into value type and reference (not the same as the pointer of C ++) type. Correspondingly, the memory allocation is divided into two methods: Stack, second, heap (managed heap ). The value type is only allocated in the stack. The reference type allocates memory and managed heap, And the managed heap corresponds to garbage collection .)
When to pack
1. Call a method containing parameters of the Object type. This Object can be of any type. When you need to input a value type (such as Int32), you need to pack it;
2. A non-generic container also defines the element type as an Object to ensure universality. To add value-type data to a container, You need to pack the data;
Packing Efficiency
Packing:
1. First, allocate memory for the newly generated reference object from the managed heap;
2. copy the data of the value type to the allocated memory;
3. Return the address of the newly allocated object in the managed stack;
To allocate memory and copy data for one packing operation, the two operations affect the performance.
Unpack:
1. First, obtain the address of the field of the value type in the hosting heap. This step is a strict unpacking;
2. Copy the value in the referenced object to the value type instance on the thread stack;
In a strict sense, unpacking does not affect the performance, but the subsequent data copying operations will affect the performance as in boxing operations.
Theoretically,During packing, a new referenced object is generated, which may cause time loss, that is, the efficiency is reduced. You can use the overload function or the generic function to avoid packing.
For the optimization of the packing and unpacking code, since C # is implicit in both packing and unpacking, the fundamental method is to analyze the code, the most direct method of analysis is to understand how to view the decompiled IL code. For example, there may be extra packing in the loop body. You can simply use the advance packing method for optimization.