Skip to main content

Command Palette

Search for a command to run...

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

It influences the Just-In-Time (JIT) compiler’s behaviour to enhance the execution speed of critical methods.

Published
2 min read
Day 18 of 30-Day .NET Challenge: AggressiveInlining Attribute
S

Hi there 👋 I'm Sukhpinder Singh, a passionate self-taught .Net developer from India. Worked on 10+ projects for SQL database design. Developed and maintained over 20 .NET websites with 100% client satisfaction.

Skills C# | Microsoft Certified | Medium Blogger | .Net Core | Xamarin | ASP.Net | Angular


Introduction

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.

Learning Objectives

  • An example without an AggressiveInlining attribute

  • An example with an AgressiveInlining attribute

  • When to Use AggressiveInlining

Prerequisites for Developers

  • Basic understanding of C# programming language.

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

Complete Code on GitHub

GitHub — ssukhpinder/30DayChallenge.Net
Contribute to ssukhpinder/30DayChallenge.Net development by creating an account on GitHub.github.com


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

30 Day C# Challenge

Part 13 of 30

The series consists of a 30-day code challenge where I'll be diving deep into a different C# concept every single day. 🌟 I'll also be sharing my learnings and insights through engaging articles.

Up next

Day 17 of 30-Day .NET Challenge: Interlocked Class

The .Net provide a powerful tool called the “Interlocked” class for all atomic operations through which developers can reduce contention and improve t