. NET 4.6 Performance Improvement and. net4.6 Performance Improvement

Source: Internet
Author: User

. NET 4.6 Performance Improvement and. net4.6 Performance Improvement

. NET 4.6 has some CLR features related to performance improvement. Some of these features will take effect automatically, while others, such as SIMD and Async Local Storage) you need to make some changes to the method of writing the application.

SIMD

The Mono team has always been proud of their support for the SIMD, that is, the single command stream multi-data flow feature. SIMD is a CPU instruction set that can perform the same operation on a maximum of 8 values at the same time. With the release of. net clr version 4.6, Windows developers can finally use this feature.

To observe the SIMD effect, you can refer to this example. Suppose you need to add two arrays in the form of c [I] = a [I] + B [I] to get the third array. By using SIMD, you can write code as follows:

for (int i = 0; i < size; i += Vector.Count) {     Vector v = new Vector(A,i) + new Vector(B,i);     v.CopyTo(C,i); }

Note how this loop increments by the value of Vector <int>. Count. Depending on the CPU type, the value may be 4 or 8 .. Net jit compiler will generate corresponding code based on different CPUs, and add the array in batches with 4 or 8 values.

This method seems cumbersome, So Microsoft also provides a series of helper classes, including:

  • Matrix3x2 Structure
  • Matrix4x4 Structure
  • Plane structure
  • Quaternion Structure
  • Vector
  • Vector (T) Structure
  • Vector2 Structure
  • Vector3 Structure
  • Vector4 Structure
Uninstall assembly

I'm afraid most developers do not know this:. NET often loads the same Assembly twice. In this case, the condition is that. NET first loads the IL version of an assembly, and then loads the NGEN version (pre-compiled version) of the same assembly ). This method is a serious waste of physical memory, especially for large 32-bit applications such as Visual Studio.

In. NET 4.6, once the CLR loads the NGEN version of an assembly, it automatically clears the memory occupied by the corresponding IL version.

Garbage Collection

We discussed earlier. the garbage collection lagging time model introduced in. NET 4.0 is much more reliable than that of completely stopping GC for a period of time, but for many GC scenarios, this method is still incomplete.

In. NET 4.6, you will be able to temporarily stop the operations of the garbage collector in a more precise way, the new TryStartNoGCRegion method allows you to specify the amount of memory required in the heap of small objects and large objects.

If the memory is insufficient, the system returns false or stops running until sufficient memory is obtained through GC cleaning. You can control this line by entering a flag for TryStartNoGCRegion. If you successfully enter a region without GC (GC is not allowed before the process ends ), the EndNoGCRegion method must be called at the end of the process.

The official documentation does not indicate whether the method is thread-safe. However, considering the working principle of GC, you should try to avoid the practice of trying to change the GC status of two processes at the same time.

Another improvement of GC is that it processes pinned objects (objects that cannot be moved once assigned. Although the description in this document is not detailed, when you fix the location of an object, it usually also fixes the location of its adjacent object. Rich Lander wrote in the article:

GC will process pinned objects in a more optimized way, So GC can compress the memory around the pinned object more effectively. For a large number of large-scale applications that use the pin method, this change will greatly improve the application performance.

GC also shows better intelligence on how to use the memory of earlier generations, Rich continues to write:

The method for upgrading 1st generation objects to 2nd generation objects has also been improved to use the memory more effectively. Before allocating new memory space to a generation, GC attempts to use available space. At the same time, a new algorithm is used to create objects using the available space area, so that the size of the newly allocated space is closer to the size of the objects.

Asynchronous Local Storage

The last improvement is not directly related to performance, but it can still achieve the optimization through effective utilization. When asynchronous APIs are not popular, developers can use local thread storage (TLS) to cache information. TLS is like a global object for a specific thread, which means you can directly access the context information and cache it without explicitly passing a certain context object.

In async/await mode, local thread storage becomes useless. Because every time await is called, it is possible to jump to another thread. Even if you are lucky enough to avoid this situation, other code may jump to your thread and interfere with the information in TLS.

New version. NET introduces the asynchronous local storage (ALS) mechanism to solve this problem. ALS is equivalent to local thread storage in terms of semantics, but it can jump with the call of await. This function is implemented through the AsyncLocal generic class, which internally calls the CallContext object to save data.

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.