-1

In C#, if I hide away implementation behind a facade, or the "pimpl" pattern, does the compiler optimize away the intermediate function call if all it's doing is calling an equivalent method from another class with the same parameters?

This is with dynamically created classes.

For example:

class A
{
    internal bool Process(float value)
    {
        if (value > 0.5f)
        {
            return true;
        }
        return false;
    }
}
class B
{
    private A m_A = new A();
    internal bool Process(float value)
    {
        return m_A.Process(value);
    }
}

There's a chance the compiler might be able to replace calls to B.Process with A.Process.

Ignore the content of the classes. They're overly simplified to make a concise example.

  • 1
    You can use sharplab.io to take a look at the generated IL and JIT Asm code, both for Debug and Release. The results from your example code are pretty readable, even if you dont know anything about IL and JIT Asm. – pschill Sep 30 at 8:57
  • 5
    Oversimplified examples may not show the real behaviour, especially when "Just-In-Time" compiling is involved. I would recommend to profile this code: isolate your real code and put in a loop, where you can run it several million times. Then compare the running time to a profiled call to the "inner" Function. Note also that for most real programs, the difference can probably be neglected. – Doc Brown Sep 30 at 9:14
  • 1
    Is there a particular reason why the distinction matters to you if the runtime is unaffected either way? Method calls are a pre-compilation concept and aren't supposed to (always) be representative of compiled code. – Flater Sep 30 at 10:08
  • I'm pretty sure that what you care about is the particular compiler you are actually using. I don't believe there is any guarantee that C# compilers in general will or will not optimize in any particular situation. – mickeyf_supports_Monica Sep 30 at 11:38
  • I'm refactoring a messy class to use several helper classes, but with the original API maintained. This question was a general question about whether C# compilers these days can spot such potential optimizations to avoid superfluous function calls. Using sharplab.io (thanks @pschill), it would indicate that the IL produced does not optimize it away. Maybe it would rely on JIT. – pugdogfan Sep 30 at 13:44
-1

The semantics of your call is: If m_A = null then crash If value > 0.5f then return true Return false

If the compiler can make sure that this is what happens, it can optimise the call away.

  • The questions asks whether it does optimise them away (which I doubt because both are virtual, but have no evidence) rather than whether it can happen. Optimisation would change the nature of the crash (i.e. the stack-trace), though this is allowed for in-lining. – VisualMelon Sep 30 at 11:05
  • @VisualMelon: You say "both are virtual" but the example given has non-virtual instance methods. – Eric Lippert Sep 30 at 18:28
  • @EricLippert sorry, yes, meant instance. – VisualMelon Sep 30 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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