6

I'm writing a performance-critical application in C# and the core operation is weight calculations. The function looks like this:

public void ForEachWeight(Action<int, int, float> action)
{
    for (int lower = 0; lower < LowerLayerSize; lower++) {
        for (int upper = 0; upper < UpperLayerSize; upper++) {
            action(lower, upper, Weights[(lower * UpperLayerSize) + upper]);
        }
    }
}

And it gets called in dozens of places with various simple functions like:

if (activationMethod == ActivationMethod.Binary) {
    ForEachWeight((lower, upper, weight) => upperLayer.Values[upper] += weight;
} else {
    ForEachWeight((lower, upper, weight) => upperLayer.Values[upper] += lowerLayer.Values[lower] * weight);
}

All the calls are done from within the same class so all the variables are accessible. Can C# inline these function calls? I'm thinking for example the above calls can be inlined to something like this:

if (activationMethod == ActivationMethod.Binary) {
    for (int lower = 0; lower < LowerLayerSize; lower++) {
        for (int upper = 0; upper < UpperLayerSize; upper++) {
            upperLayer.Values[upper] += weight;
        }
    }
} else {
    for (int lower = 0; lower < LowerLayerSize; lower++) {
        for (int upper = 0; upper < UpperLayerSize; upper++) {
            upperLayer.Values[upper] += lowerLayer.Values[lower] * weight);
        }
    }
}

If it doesn't it means tens of thousands of extra function calls which I think are a huge overhead. If this gives even a 10% speed boost it's totally worth it. I can't easily benchmark by manually inlining them for comparison since I'd have to rewrite a lot of code.

So my question is CAN C# inline these calls? And if so, how can I find out IF they have been inlined?

3
  • See this answer.
    – Ryan Frame
    Mar 30, 2013 at 2:21
  • Inlining of that function won't buy you much. It only pays for functions that do practically nothing. Mar 30, 2013 at 2:50
  • The fact that you are using delegates makes it highly unlikely.
    – leppie
    Mar 30, 2013 at 4:38

2 Answers 2

11

Inlining happens at JIT compilation, so you need to inspect code at runtime to observe inlining. This can be done using the debugger, but keep in mind that if the CLR detects the presence of a debugger various optimizations are disabled, so you need to attach the debugger after the method has been compiled.

Please see these answers for some info on how you can find the method using WinDbg/SOS.

3

You can use MethodImplOptions.AggressiveInlining inside this attribute : http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimplattribute.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.