dx_over_dt
dx_over_dt

Reputation: 14328

When using abstract classes in C#, does AgressiveInlining do anything, and where should it be placed?

Suppose I have an abstract class and a class that implements it.

public abstract class BaseClass
{
    public void Outer()
    {
        for (int i = 0; i < 1000000; i++)
        {
            Inner();
        }
    }

    protected abstract void Inner();
}

public class MyClass : BaseClass
{
    protected override void Inner()
    {
        // do stuff
    }
}

Since Outer() calls Inner() so much and does little else, I'd like Inner() to be inline'd using [MethodImpl(MethodImplOptions.AggressiveInlining)].

public abstract class BaseClass
{
    public void Outer() // ...

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected abstract void Inner();
}

public class MyClass : BaseClass
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected override void Inner()
    {
        // do stuff
    }
}

Does this attribute go in BaseClass, MyClass, or both, or does the attribute not work at all in this case?

Upvotes: 0

Views: 516

Answers (1)

dx_over_dt
dx_over_dt

Reputation: 14328

Per Hans's comment, abstract members cannot be inline'd due to the nature of abstract classes and virtual calls.

Edit: per Ben's comment below, this is not true in general for all compilers, but it is true of JIT (which happened to be the context of my particular question).

Upvotes: 1

Related Questions