paint-brush
The 30-Day .NET Challenge - Day 18: AggressiveInlining Attributeby@ssukhpinder
154 reads

The 30-Day .NET Challenge - Day 18: AggressiveInlining Attribute

by Sukhpinder SinghApril 8th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

One of the techniques to improve application performance involves the use of the AggressiveInlining attribute. It influences the Just-In-Time (JIT) compiler’s behaviour to enhance the execution speed of critical methods. It can be used carefully as overuse or inappropriate use can lead to larger code size (code bloat)
featured image - The 30-Day .NET Challenge - Day 18: AggressiveInlining Attribute
Sukhpinder Singh HackerNoon profile picture


One of the techniques to improve application performance involves the use of the AggressiveInlining attribute. It influences the Just-In-Time (JIT) compiler’s behavior to enhance the execution speed of critical methods.

Learning Objectives

  • An example without an AggressiveInlining attribute
  • An example with an AgressiveInlining attribute
  • When to Use AggressiveInlining

Prerequisites for Developers


Getting Started

Example Without AggressiveInlining

Consider a simple method that multiplies its input by two:

    private int MultiplyByTwo(int value)
    {
        return value * 2;
    }

Without the AggressiveInlining attribute, the JIT compiler may or may not inline this method. The method is not inlined, the overhead of the method calls could impact the application's overall performance.

Example With AggressiveInlining

By marking the method with the AggressiveInlining attribute, it can explicitly signal the JIT compiler:

    using System.Runtime.CompilerServices;
    
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private int MultiplyByTwo(int value)
    {
        return value * 2;
    }

In this scenario, the JIT compiler is going to inline the method, reducing the overhead and improving the performance of code that frequently calls this method.

When to Use AggressiveInlining

While AggressiveInlining can be a powerful tool for optimizing methods, it should be used carefully.

Overuse or inappropriate use of the attribute can lead to larger code size (code bloat), which might negatively impact performance by affecting cache utilization.


It's best to reserve AggressiveInlining for small, critical methods where the benefits of inlining outweigh the potential drawbacks.

Complete Code

Add a class named “AggressiveInlining” with two methods as shown below highlighting the difference between a method with AggressiveInlining and one with not.

    public static class AggressiveInlining
    {
        public static int MultiplyByTwo(int value)
        {
            return value * 2;
        }
    
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int MultiplyByTwoWithAggressiveInlining(int value)
        {
            return value * 2;
        }
    }


Call from the main method as follows":

    #region Day 18: AggressiveInlining Attribute
    
    AggressiveInlining.MultiplyByTwo(10);
    AggressiveInlining.MultiplyByTwoWithAggressiveInlining(10);
    
    #endregion


You can find the complete code here: GitHub — ssukhpinder/30DayChallenge.Net

C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

Follow us: Youtube | X | LinkedIn | Dev.to Visit our other platforms: GitHub More content at C# Programming