Requirements: When looking at the .NET source code, I found that some methods have added the [MethodImpl(MethodImplOptions.AggressiveInlining)] feature, but I saw that the MethodImpl feature can affect the behavior of the JIT compiler. Some tests were carried out based on information on the Internet.
MethodImplOptions configuration
Code:
Inline function
In computer science, an inline function (sometimes called an online function or a compile-time expansion function) is a programming language construct used to suggest that the compiler inline extensions (sometimes called online extensions) to some special functions; This means that the compiler inserts and replaces the specified function body in place of each place (context) where the function is called, saving the additional time spent on each call to the function. However, when choosing to use inline functions, it is necessary to weigh the space occupied by the program and the efficiency of program execution, because too many complex functions for inline expansion will bring large storage resource expenses. It is also important to note that inline extensions of recursive functions may cause infinite compilation of some compilers.
MethodImplOptions.AggressiveInlining
Function: It is recommended that the JIT compiler inline the method as much as possible. Explanation: Inline refers to replacing a function call with the function body itself, thereby reducing the overhead of the function call. Applicable scenarios: Suitable for small, frequently called methods (e.g., attribute accessors, simple mathematical calculations, etc.). Note: This is just a "suggestion", the JIT compiler may decide whether to actually inline or not, depending on the actual situation.
The results of the test "inline" and "not inline" are as follows:
The test code is as follows:
It was found that the CPU usage after inline was much lower than that of non-inline calls, in fact, alsoDon't be too anxious, today's compilers are very smart, even without adding this feature, the compiler may help you automatically optimize。 |