<p>Given an empty method body, will the JIT optimize out the call (I know the C# compiler won't). How would I go about finding out? What tools should I be using and where should I be looking?</p>

<p>Since I'm sure it'll be asked, the reason for the empty method is a preprocessor directive.</p>

<hr>

<p>@Chris:
Makes sense, but it could optimize out calls to the method. So the method would still exist, but static calls to it could be removed (or at least inlined...)</p>

<p>@Jon:
That just tells me the language compiler doesn't do anything. I think what I need to do is run my dll through ngen and look at the assembly.</p>

[{'answer_id': 11791, 'author': 'Chris Smith', 'author_id': 322, 'author_profile': 'https://Stackoverflow.com/users/322', 'pm_score': 2, 'selected': False, 'text': '<p>No, empty methods are never optimized out. Here are a couple reasons why:</p>\n\n<ul>\n<li>The method could be called from a\nderived class, perhaps in a\ndifferent assembly </li>\n<li>The method could\nbe called using Reflection (even if\nit is marked private)</li>\n</ul>\n\n<p>Edit: Yes, from looking at that (exellent) code project doc the JITer will eliminate calls to empty methods. But the methods themselves will still be compiled and part of your binary for the reasons I listed.</p>\n'}, {'answer_id': 11792, 'author': 'Andrew Grant', 'author_id': 1043, 'author_profile': 'https://Stackoverflow.com/users/1043', 'pm_score': 1, 'selected': False, 'text': '<p>All things being equal, yes it should be optimized out. The JIT inlines functions where appropriate and there are few things more appropriate than empty functions :)</p>\n\n<p>If you really want to be sure then change your empty method to throw an exception and print out the stack trace it contains.</p>\n'}, {'answer_id': 11794, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 4, 'selected': False, 'text': '<p>This chap has quite a good treatment of JIT optimisations, do a search on the page for \'method is empty\', it\'s about half way down the article -</p>\n\n<p><a href="http://www.codeproject.com/KB/dotnet/JITOptimizations.aspx" rel="noreferrer">http://www.codeproject.com/KB/dotnet/JITOptimizations.aspx</a></p>\n\n<p>Apparently empty methods do get optimised out through inlining what is effectively no code.</p>\n\n<p>@Chris: I do realise the that the methods will still be part of the binary and that these are JIT optimisations :-). On a semi-related note, Scott Hanselman had quite an interesting article on inlining in Release build call stacks:</p>\n\n<p><a href="http://www.hanselman.com/blog/ReleaseISNOTDebug64bitOptimizationsAndCMethodInliningInReleaseBuildCallStacks.aspx" rel="noreferrer">http://www.hanselman.com/blog/ReleaseISNOTDebug64bitOptimizationsAndCMethodInliningInReleaseBuildCallStacks.aspx</a></p>\n'}, {'answer_id': 12078, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 4, 'selected': False, 'text': "<p>I'm guessing your code is like:</p>\n\n<pre><code>void DoSomethingIfCompFlag() {\n#if COMPILER_FLAG\n    //your code\n#endif\n}\n</code></pre>\n\n<p>This won't get optimised out, however:</p>\n\n<pre><code>partial void DoSomethingIfCompFlag();\n\n#if COMPILER_FLAG\npartial void DoSomethingIfCompFlag() {\n    //your code\n}\n#endif\n</code></pre>\n\n<p>The first empty method is partial, and the C#3 compiler will optimise it out.</p>\n\n<hr>\n\n<p>By the way: this is basically what partial methods are for.  Microsoft added code generators to their Linq designers that need to call methods that by default don't do anything. </p>\n\n<p>Rather than force you to overload the method you can use a partial.  </p>\n\n<p>This way the partials are completely optimised out if not used and no performance is lost, rather than adding the overhead of the extra empty method call.</p>\n"}]
['https://Stackoverflow.com/questions/11783', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/34/']