TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# AggressiveInlining: MethodImpl Attribute

Benchmark and test the AggressiveInlining option. Use MethodImplOptions.AggressiveInlining.
AggressiveInlining. The JIT compiler logically determines which methods to inline. But sometimes we know better than it does. With AggressiveInlining, we give the compiler a hint. We tell it that the method should be inlined.AttributeOptimization
Example. This example benchmarks a method with no attribute, and with AggressiveInlining. The method body contains several lines of useless code. This makes the method large in bytes, so the JIT compiler may decide not to inline it.Benchmark

And: We apply the MethodImplOptions.AggressiveInlining option to Method2. This is an enum.

Result: We see that with no options, the method calls required seven nanoseconds each.

But: With inlining specified (with AggressiveInlining), the calls required less than one nanosecond each.

C# program that uses AggressiveInlining using System; using System.Diagnostics; using System.Runtime.CompilerServices; class Program { const int _max = 10000000; static void Main() { // ... Compile the methods. Method1(); Method2(); int sum = 0; var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { sum += Method1(); } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { sum += Method2(); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.Read(); } static int Method1() { // ... No inlining suggestion. return "one".Length + "two".Length + "three".Length + "four".Length + "five".Length + "six".Length + "seven".Length + "eight".Length + "nine".Length + "ten".Length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] static int Method2() { // ... Aggressive inlining. return "one".Length + "two".Length + "three".Length + "four".Length + "five".Length + "six".Length + "seven".Length + "eight".Length + "nine".Length + "ten".Length; } } Output 7.34 ns No options 0.32 ns MethodImplOptions.AggressiveInlining
Discussion. This is a micro-optimization. For critical algorithms, using a micro-optimization on hot code is often worthwhile. But this is the uncommon case. Most code needs little optimization.
When to use AggressiveInlining. To which methods should we apply this option? I have found that larger methods, such as the one in the example, benefit from this sort of inlining.

And: If a method is called only once in an enclosing method, it may help to inline it even if it is large.

However: If a large method is called in many places in a program, inlining it will reduce locality of reference and may ruin performance.

Locality of Reference
Summary. Method calls have a cost, but also a benefit. They cause an activation record to be added to the evaluation stack. But they also reduce instruction count. Our challenge is to find the best balance between these considerations.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf