A detailed look at performance improvements in .NET
.NET 4.6 brings some CLR features related to performance improvements. Some of these features will take effect automatically, while others, such as SIMD and Async Local Storage, require changes to the way applications are written. Certain changes.
SIMD
The Mono team has always been proud of their support for SIMD, the single instruction stream multiple data stream feature. SIMD is a CPU instruction set that can perform the same operation on up to 8 values at the same time. With the launch of .NET CLR version 4.6, Windows developers can finally use this feature.
In order to actually observe the effect of SIMD, you can refer to this example. Suppose you need to add two arrays in the form c[i] = a[i] + b[i] to get a third array. By using SIMD, you can write code in the following way:
for (int i = 0; i < size; i += Vector.Count) { Vectorv = new Vector(A,i) + new Vector(B,i); v.CopyTo(C,i); }
Note how this loop increments by the value of Vector
This method seems a bit cumbersome, so Microsoft also provides a series of auxiliary classes, including:
Matrix3x2 structure
Matrix4x4 structure
Plane structure
Quaternion structure
Vector class
Vector(T) structure
Vector2 structure
Vector3 structure
Vector4 structure
Assembly uninstall
I'm afraid most developers don't know this: .NET often loads the same assembly twice. The condition for this to happen is that .NET first loads the IL version of an assembly and subsequently loads the NGEN version (i.e. the precompiled version) of the same assembly. This approach 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 will automatically clear the memory occupied by the corresponding IL version.
Garbage collection
Earlier we discussed the garbage collection delay mode introduced in .NET 4.0. Although this method is much more reliable than letting the GC stop completely for a period of time, it is still not sufficient for many GC scenarios. complete.
In .NET 4.6, you will be able to temporarily suspend the garbage collector in a more sophisticated way. The new TryStartNoGCRegion method allows you to specify how much memory is needed in the heap for small objects and large objects.
If there is insufficient memory, the runtime will return false or stop running until enough memory is obtained through GC cleaning. You can control this behavior by passing a flag to TryStartNoGCRegion. If you successfully enter a GC-free area (GC is not allowed until the end of the process), then the EndNoGCRegion method must be called at the end of the process.
The official documentation does not state whether this method is thread-safe, but considering the working principle of GC, you should try to avoid having two processes try to change the GC state at the same time.
Another improvement to the GC is the way it handles pinned objects (i.e. objects that cannot be moved once allocated). Although this aspect is somewhat vaguely described in the documentation, when you fix the position of an object, it usually fixes the positions of its adjacent objects as well. Rich Lander wrote in the article:
The GC will handle pinned objects in a more optimized way, so the GC can compress the memory around the pinned objects more effectively. For large-scale applications that use a large number of pins, this change will greatly improve the performance of the application.
The GC also shows better intelligence in how to use memory in earlier generations, Rich continued to write:
The way Generation 1 objects are promoted to Generation 2 objects has also been improved to use memory more efficiently. Before allocating new memory space for a generation, the GC will first try to use available space. At the same time, a new algorithm is used when creating objects using the available space area, so that the size of the newly allocated space is closer to the size of the object than before.
Asynchronous local storage
The last improvement is not directly related to performance, but it can still achieve optimization results through effective use. In the days before asynchronous APIs became popular, developers could leverage thread local storage (TLS) to cache information. TLS acts like a global object to a specific thread, which means you can directly access context information and cache it without having to explicitly pass some context object.
In async/await mode, thread local storage becomes useless. Because every time await is called, it is possible to jump to another thread. And even if you manage to avoid this situation, other code may jump to your thread and interfere with the information in TLS.
The new version of .NET introduces the asynchronous local storage (ALS) mechanism to solve this problem. ALS is semantically equivalent to thread local storage, but it can make corresponding jumps with the call of await. This function will be implemented through the AsyncLocal generic class, which will internally call the CallContext object to save data.
The above is the detailed content of A detailed look at performance improvements in .NET. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

What are the performance improvements and advantages brought by PHP8? Since its inception in 1995, PHP has been one of the most popular server-side scripting languages. PHP8 is the latest version of PHP, which will be officially released starting from the end of 2020. PHP8 brings many exciting new features and improvements, especially in terms of performance. This article will introduce some of the key performance improvements and advantages of PHP8 and provide specific code examples. JIT compiler PHP8 introduces the JIT (Just-In-Time) compiler, which is a

If you are a .NET developer, you must be aware of the importance of optimizing functionality and performance in delivering high-quality software. By making expert use of the provided resources and reducing website load times, you not only create a pleasant experience for your users but also reduce infrastructure costs.

In terms of high-concurrency request processing, .NETASP.NETCoreWebAPI performs better than JavaSpringMVC. The reasons include: AOT early compilation, which reduces startup time; more refined memory management, where developers are responsible for allocating and releasing object memory.

With the continuous development of data science and deep learning, Python is one of the mainstream programming languages, and its scientific computing library numpy is also constantly innovating. Recently, numpy has released a new version that contains some new features and performance improvements. In this post, we’ll take a deep dive into the new version of numpy and introduce some of its important features and improvements. Shuffle function improvement Before numpy1.17.0, the shuffle function would reorder the array elements in random order. Ran

How to use VueRouterLazy-Loading routing and its effect on improving page performance. As front-end applications become more and more complex, the management of front-end routing has become more and more important. As a mainstream front-end framework, Vue.js's built-in VueRouter provides very powerful routing management functions, which can help us build flexible and efficient single-page applications. Among them, VueRouterLazy-Loading is a very important and practical function.

C#.NET interview questions and answers include basic knowledge, core concepts, and advanced usage. 1) Basic knowledge: C# is an object-oriented language developed by Microsoft and is mainly used in the .NET framework. 2) Core concepts: Delegation and events allow dynamic binding methods, and LINQ provides powerful query functions. 3) Advanced usage: Asynchronous programming improves responsiveness, and expression trees are used for dynamic code construction.
