0

Generally, I read that large methods benefit from some sort of inlining and the C# compiler does this sort of micro-optimizations automatically. I understand that if a method is called just one time in an enclosing method, performance might improve when inlined even if the method itself is large. However, if a large method is called in many places, performance could decrease if it is inlined, because it reduces the locality of reference. I know that all method calls have a cost such as adding to evaluation stack etc., So, my question is how do we find reduction in instruction count when inlined and the performance impact to determine if a method can benefit from inlining manually? The idea is to inline method calls selectively and manually for performance improvements. Any ideas and thoughts on this subject will be appreciated.

  • 1
    How much readability and maintainability are you willing to sacrifice for a minor performance gain? Before continuing, see how much of a performance gain you can expect (if any) by pasting the desired methods inside the calling method. – helb Jan 16 at 16:08
  • @helb The software project I am currently involved in performs fairly intensive tasks that IMHO can benefit from optimizations - I want to determine if manual optimization would yield better results or allowing the JIT compiler to do the optimizations. The point of my post is to figure out which way to go. Hope that clarifies my intensions. – Xami Yen Jan 16 at 19:05
5

As this question is asked on softwareengineering.stackexchange.com, where code quality is a major concern, my short answer is: don't waste your time with the question what the performance impact is of manually inlining methods unless you plan to create your own compiler/runtime system.

Let the compiler and runtime system decide about inlining and similar micro-optimizations. As a software engineer, you should aim at readable code with highest priority. Optimization generally makes code less readable (like manually inlining code, creating huge monster methods), and most of the time has a performance impact that you won't notice.

So:

  • Write the most readable code possible.
  • Check whether you have any performance problem.
  • If not, you're fine.
  • If you have performance problems, use a profiler to find the real bottleneck (most probably, it won't be anything around inline-able methods).
  • Optimize the bottleneck, and only the bottleneck.
  • Verify that the optimization really improved something. Otherwise roll back to the original, more readable code.
  • If necessary, repeat that cycle.
  • Points mentioned are well taken and are clearly articulated and appreciated. However, according my tech lead writing most efficient code by optimizing it is part of a software engineers’ job. Since our product requires high performance code, I was asked to find ways to optimize it. The tasks you mentioned are already being undertaken such as using a profiler to identify bottlenecks etc. Whether or not I manually optimize the code, I still would want to know to do it if I ever need to do it. – Xami Yen Jan 16 at 19:13
  • Does identified bottleneck involves method inlining. – Fabio Jan 20 at 9:00
1

As you've outlined, the compiler will generally make best guesses. There is however the AggressiveInlining method attribute if you wish to force inlining (see also MSDN) . As with any performance tuning, the only way to measure any potential benefits is to benchmark.

  • Thanks. Here is one of the SO questions I reviewed and the answers made me think sometimes manual optimizations, albeit in rare circumstances, can be of help. Link – Xami Yen Jan 16 at 19:16

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.