<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx</link><description>I was asked recently exactly what optimizations the C# compiler performs when you specify the optimize switch. Before I answer that, I want to make sure that something is perfectly clear. The compiler’s “usage” string is not lying when it says: /debug[+|-]</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9727246</link><pubDate>Thu, 11 Jun 2009 21:00:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9727246</guid><dc:creator>carlos</dc:creator><description>&lt;P&gt;An awful lot of these optimizations (maybe all) are language-independent. &amp;nbsp;Do you share the optimizer with VB? &amp;nbsp;Indeed, it looks like you could write an optimizer that reads IL and writes better IL. &amp;nbsp;Other than compilation efficiency, is there any reason this isn't done?&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;You are not the first person to notice this. :-)&amp;nbsp;We could do that. Or, one of the things we are considering for future versions of C# and VB is to have the compilers output a common "semantic tree" format which can be fed into a common metadata emitter and/or IL generator. However, it's not yet clear whether this&amp;nbsp;would be&amp;nbsp;a net win from a cost-vs-benefit analysis. We have two IL generators that work perfectly well; why throw away&amp;nbsp;much of&amp;nbsp;that existing, debugged code and spend a few months writing a common infrastructure&amp;nbsp;in order to achieve a false economy of shared code? It seems potentially penny-wise, pound-foolish. We'll make the decision that is right for the business case. -- Eric&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9727810</link><pubDate>Fri, 12 Jun 2009 00:08:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9727810</guid><dc:creator>pete.d</dc:creator><description>&lt;p&gt;I apologize for a comment about the format of the post, rather than about the post itself, but...&lt;/p&gt;
&lt;p&gt;Very often in this blog, I find that once text formatted as &amp;quot;code&amp;quot; appears, all of the text following that remains formatted as &amp;quot;code&amp;quot; rather than reverting back to the normal format.&lt;/p&gt;
&lt;p&gt;I looked at the HTML source, and I can see that although most of the HTML is valid XML, with each &amp;lt;p&amp;gt; tag having a corresponding &amp;lt;/p&amp;gt; close tag, when the &amp;lt;span class=&amp;quot;code&amp;quot;&amp;gt;&amp;lt;/span&amp;gt; shows up, the &amp;lt;p&amp;gt; element is NOT properly closed.&lt;/p&gt;
&lt;p&gt;In this particular post, the example would be the first section of text where the compiler options are described. &amp;nbsp;The entire paragraph is contained by a &amp;lt;span class=&amp;quot;code&amp;quot;&amp;gt; element, and there is an open &amp;lt;p&amp;gt; tag, but no close &amp;lt;p&amp;gt; tag.&lt;/p&gt;
&lt;p&gt;Whose bug is this? &amp;nbsp;I'm not sure. &amp;nbsp;The DOCTYPE for the page isn't XHTML, so technically a &amp;lt;p&amp;gt; tag need not have a close tag. &amp;nbsp;On the other hand, when a &amp;lt;p&amp;gt; tag doesn't have a close tag, I believe that the &amp;lt;p&amp;gt; element is considered to continue until the next block element, which in this case is the next &amp;lt;p&amp;gt; element. &amp;nbsp;Because the &amp;lt;p&amp;gt; tag for the compiler options text is inside the &amp;lt;span class=&amp;quot;code&amp;quot;&amp;gt; element, this means that logically, the &amp;lt;p&amp;gt; element and &amp;lt;span&amp;gt; element overlap, which isn't allowed even in HTML (unclosed elements are allowed, but otherwise the same hierarchical rules apply).&lt;/p&gt;
&lt;p&gt;To further confuse the issue, some browsers figure it out (e.g. Opera) and others don't (e.g. Safari). &amp;nbsp;Presumably in the case where the text is displayed as expected, the HTML parser auto-closes any unclosed elements contained within an element that is being closed.&lt;/p&gt;
&lt;p&gt;Anyway, I thought I'd mention this, in the hopes that it's simple to fix the problem. I have no idea how the HTML is actually being generated, and I suppose if it's auto-generated by some utility, it might be hard to request and/or have implemented a bug-fix so that the &amp;lt;p&amp;gt; element is correctly closed even when contained within a &amp;lt;span&amp;gt; element. But, if it's being hand-generated, or generated by something that is easily fixed, it sure would be helpful to those of us using certain browsers. &amp;nbsp;:)&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9728098</link><pubDate>Fri, 12 Jun 2009 01:56:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9728098</guid><dc:creator>configurator</dc:creator><description>&lt;P&gt;@pete.d: I think it parses it as &amp;lt;span&amp;gt; &amp;lt;p&amp;gt;code&amp;lt;/p&amp;gt; &amp;lt;p&amp;gt;text&amp;lt;/p&amp;gt; ..... and a &amp;lt;/span&amp;gt; at the end.&lt;/P&gt;
&lt;P&gt;Now about the post,&lt;/P&gt;
&lt;P&gt;Isn't callvirt more 'complicated', because it needs to use the right method in the virtual whatchamacallit-tables (vtables?), thus making the optimization of using callvirt instead of call counter-productive?&lt;/P&gt;
&lt;P&gt;(I assume the answer is 'no, its not', otherwise you wouldn't make this optimization, but I'd like to know why not)&lt;/P&gt;
&lt;P&gt;[[ "callvirt"&amp;nbsp;takes a method token as its argument; the jitter looks up the method token in the metadata tables&amp;nbsp;to see if it is actually a virtual method. If it is, then it generates a null check and a vtable call, if not then it generates a null check and a direct call. -- Eric ]]&lt;/P&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9728126</link><pubDate>Fri, 12 Jun 2009 02:06:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9728126</guid><dc:creator>Thomas Levesque</dc:creator><description>&lt;P&gt;"* We omit generating code for things like int foo = 0; because we know that the memory allocator will initialize fields to default values."&lt;/P&gt;
&lt;P&gt;That's actually an interesting point... If this kind of instruction is omitted, then why does the compiler require that all local variables be initialized before use ? I mean, I would probably do it anyway because it's better to be explicit, but it seems strange that we are compelled to write code that's going to be eliminated anyway...&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;Because using uninitialized locals accidentally is a common source of bugs. -- Eric &lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9728239</link><pubDate>Fri, 12 Jun 2009 02:44:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9728239</guid><dc:creator>Travis Simon</dc:creator><description>&lt;p&gt;I've always developed using debug, no optimisation and deployed w/o debug and optimised (because it's the 'Right Thing To Do'). However, I've never really tested the performance characteristics, since there was basically no cost in doing the optimisation.&lt;/p&gt;
&lt;p&gt;However, reading your post, it sounds like the /optimise flag doesn't do much optimising at all. Is there much of a difference for the majority of cases? If so, are the gains spread across the scenarios, or is there one or two optimisations that give the majority of the speedups?&lt;/p&gt;
&lt;p&gt;Interesting post, by the way.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9728278</link><pubDate>Fri, 12 Jun 2009 02:53:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9728278</guid><dc:creator>TheCPUWizard</dc:creator><description>&lt;p&gt;Configurator....&lt;/p&gt;
&lt;p&gt;I think it quite clearly explained that the JIT makes the determination if &amp;quot;CallVirt&amp;quot; is actually &amp;quot;virtual&amp;quot; (in the sense of using the &amp;quot;v-table&amp;quot;) or is a direct call.&lt;/p&gt;
&lt;p&gt;Travis....&lt;/p&gt;
&lt;p&gt;Hopefully you &amp;quot;over-simpified&amp;quot;...Shipping a different Build configuration [noDebug/Optimizd] than the one you subject to all of your testing [Debug/NoOptimize] is dangerous. Years back I established a policy of doing all work in the &amp;quot;shipping&amp;quot; configuration [I do not call it &amp;quot;release&amp;quot; since that is just a name, and does not describe the actual settings], and only switching to a &amp;quot;diagnostic&amp;quot; [again I do not call it &amp;quot;Debug&amp;quot; for the same reason] if there is a bug that I simply can not pin down in a reasonable time.&lt;/p&gt;
&lt;p&gt;The amount of grief this has saved me over the years [I did the same thing with C++ and with C before that] is priceless.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9728299</link><pubDate>Fri, 12 Jun 2009 03:04:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9728299</guid><dc:creator>Darrell</dc:creator><description>&lt;P&gt;[quote]That’s pretty much it. These are very straightforward optimizations; there’s no inlining of IL, no loop unrolling, no interprocedural analysis whatsoever. We let the jitter team worry about optimizing the heck out of the code when it is actually spit into machine code; that’s the place where you can get real wins.[/quote]&lt;/P&gt;
&lt;P&gt;I would have thought that it would make more of a difference to do these types of optimizations in the IL - the less the jitter needs to do, the faster things can start up. &amp;nbsp;These optimizations can't be free.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;But the C# compiler knows nothing about the target environment. The optimizations you want to make on compact framework running on a cell phone are very different than the optimizations you want to make on a 64 bit server. So let the jitter worry about it; the jitter knows how to optimize for the current environment. -- Eric &lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9729258</link><pubDate>Fri, 12 Jun 2009 06:20:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9729258</guid><dc:creator>Dmitry Zaslavsky</dc:creator><description>&lt;P&gt;WOW!&lt;/P&gt;
&lt;P&gt;&amp;gt; We attempt to eliminate generation of "protected" regions. For instance, if a try block is empty then clearly the catch blocks are not reachable and the finally can just be an ordinary code region.&lt;/P&gt;
&lt;P&gt;This is dangerous! In fact that is how some people create "protected" regions in the first place.&lt;/P&gt;
&lt;P&gt;Code that is executed within finally block will not be interrupted by Abort exception for example.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;You are correct. I was misremembering. We eliminate catch blocks, but the finally blocks live on as finally blocks. I've corrected the text; thanks for the note. -- Eric &lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9729421</link><pubDate>Fri, 12 Jun 2009 06:49:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9729421</guid><dc:creator>Kevin Page</dc:creator><description>&lt;P&gt;"We attempt to eliminate generation of "protected" regions. For instance, if a try block is empty then clearly the catch blocks are not reachable and the finally can just be an ordinary code region."&lt;/P&gt;
&lt;P&gt;I've seen code like this in Microsoft's own implementation of the ASP.NET Cache:&lt;/P&gt;
&lt;P&gt;try{}&lt;BR&gt;finally&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;Monitor.Enter(this._lock);&lt;BR&gt;&amp;nbsp;acquiredLock = true;&lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;Does this optimization mean that the above code might not run the "finally" block in a protected region?&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;See above. -- Eric &lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9729469</link><pubDate>Fri, 12 Jun 2009 06:56:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9729469</guid><dc:creator>Joe Chung</dc:creator><description>&lt;p&gt;+1 ThreadAbortExceptions do not interrupt finally blocks so don't replace finally blocks with ordinary code regions.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9730280</link><pubDate>Fri, 12 Jun 2009 08:36:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9730280</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;P&gt;&amp;gt;Or, one of the things we are considering for future versions of C# and VB is to have the compilers output a common "semantic tree" format which can be fed into a common metadata emitter and/or IL generator.&lt;/P&gt;
&lt;P&gt;Now that you mention it, I remembered that .NET 4.0 beta1 has a whole lot of goodies in System.Linq.Expression - it's owned by C# team, isn't it? My first impression after looking at it is that it actually pretty much covers the "semantic tree" for a single method, including things such as lambdas with all the nitty-gritty done behind the scenes. The only thing that seems to be missing in terms of C# feature coverage there are iterator methods. And it can generate output into a MethodBuilder too, so it's almost a compiler writer's toolbox...&lt;/P&gt;
&lt;P&gt;... almost - because it can only generate static methods (why oh why?), and seemingly cannot generate a bunch of cross-calling methods at a time.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;To generate non-static methods we need to generate them on some class. What class? We only have expression trees and statement trees; what we need are declaration trees. I would very much like to have the ability to represent types as trees. Then we would have a "compiler writer's toolbox" as you say. Whether we will get to that point or not, I don't know; guessing would be speculation about the feature sets of unannounced projects that have no budgets at this time. That kind of guessing is seldom productive. -- Eric &lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9730315</link><pubDate>Fri, 12 Jun 2009 08:41:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9730315</guid><dc:creator>pete.d</dc:creator><description>&lt;P&gt;I admit, I don't get the concern about ThreadAbortException. Personally, I would have been happy if Thread.Abort(), Thread.Suspend(), and Thread.Resume() had just been left out of .NET altogether. There are so many other ways that Thread.Abort() can produce unpredictable results, I can't say that I'd be all that worried about the code in a finally block winding up unprotected.&lt;/P&gt;
&lt;P&gt;Now, if there's some _other_ important benefit to protected code regions I'm overlooking, then perhaps that's a reason for concern. But I'm surprised anyone, never mind three people, care about "correctness" in the context of a ThreadAbortException. &amp;nbsp;IMHO, by definition any code using ThreadAbortException is incorrect, regardless of what the compiler's doing. &amp;nbsp;:p&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;I don't like thread aborts either. But wishing doesn't make them go away. We've got to write a code generator for a language that targets the environment we're given, and the environment we're given has somewhat goofy thread abort semantics. -- Eric &lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9730696</link><pubDate>Fri, 12 Jun 2009 09:33:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9730696</guid><dc:creator>Joren</dc:creator><description>&lt;P&gt;@Thomas&lt;/P&gt;
&lt;P&gt;Fields are initialized to default values, but I'm not sure the same is guaranteed to happen for local variables, since they are allocated to the stack.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;If you ever wish to become sure, then I encourage you to read the CLI spec, Partition II, section 24.4.4 and Partition III, section 1.8.1.1. -- Eric&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9730900</link><pubDate>Fri, 12 Jun 2009 09:55:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9730900</guid><dc:creator>Chris Nahr</dc:creator><description>&lt;p&gt;Eric was clearly in error about the try/finally issue. &amp;nbsp;The runtime is guaranteed to execute all finally blocks before a thread is aborted, so the C# compiler cannot just throw away the protected region markers usually placed around a finally block.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9731003</link><pubDate>Fri, 12 Jun 2009 10:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9731003</guid><dc:creator>Manoj Mehta</dc:creator><description>&lt;p&gt;@thomas&lt;/p&gt;
&lt;p&gt;I think Eric is referring to the removal of the initializer for member variable foo not local variable foo.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9731248</link><pubDate>Fri, 12 Jun 2009 10:41:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9731248</guid><dc:creator>marc.gravell</dc:creator><description>&lt;p&gt;&amp;quot;because we know that the result of the &amp;quot;new&amp;quot; operator is never null&amp;quot; - well, there is a corner-case... - see: &lt;a rel="nofollow" target="_new" href="http://stackoverflow.com/questions/194484#194671"&gt;http://stackoverflow.com/questions/194484#194671&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9732067</link><pubDate>Fri, 12 Jun 2009 12:11:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9732067</guid><dc:creator>Joren</dc:creator><description>&lt;p&gt;Re: null corner case&lt;/p&gt;
&lt;p&gt;That's certainly an unexpected (but logical) case, but Eric started with &amp;quot;suppose you have a non-virtual instance method M on a reference type C&amp;quot;. The optimisation should still be valid, because Nullable&amp;lt;T&amp;gt; is not a reference type. :)&lt;/p&gt;
&lt;p&gt;How I wish there were support for non-nullable reference types in C#. I've tried Spec# out for a while, which does support it (with the obvious syntax of T! for a non-nullable T), and it works brilliantly. Everywhere I normally insert an &amp;quot;if (foo == null) throw new ArgumentNullException(&amp;quot;foo&amp;quot;);&amp;quot; I could specify that foo is a non-nullable parameter instead, and never have to worry about it again.&lt;/p&gt;
&lt;p&gt;On a related note, why are structs required to have a public parameterless constructor instead of requiring that the programmer initializes the structs himself? If there wasn't a way for everyone to create a struct with all its fields set to default values, I could sort of implement non-nullable types by having a light-weight struct that encapsulates a single reference, with a constructor that rejects null values for the field.&lt;/p&gt;
</description></item><item><title>Interesting Finds: June 12, 2009</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9734832</link><pubDate>Fri, 12 Jun 2009 17:12:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9734832</guid><dc:creator>Jason Haley</dc:creator><description>&lt;p&gt;Interesting Finds: June 12, 2009&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9735030</link><pubDate>Fri, 12 Jun 2009 17:38:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9735030</guid><dc:creator>br1</dc:creator><description>&lt;P&gt;"The non-virtual call instruction does not do a null check; we'd have to generate extra code to check whether GetC returns null."&lt;/P&gt;
&lt;P&gt;What happens if the null check is omitted? The IL is then invalid?&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;I encourage you to read the specification when you have questions like that. But because I am in a charitable mood, I'll reproduce the relevant line of the CIL spec, Partition III, here: &lt;/P&gt;
&lt;P&gt;"For a typical use of the call instruction, verification checks that (a) method refers to a valid methodref or methoddef token; (b) the types of the objects on the stack are consistent with the types expected by the method call, and (c) the method is accessible from the callsite, and (d) the method is not abstract"&lt;/P&gt;
&lt;P&gt;So, no, this would not be a verification issue. Why do you imagine that this would be a verification issue? There's no way that type safety can be compromised by passing a null "this", is there? callvirt needs to crash if "this" is null because then there is no virtual function table from which to determine the correct call, but if a non-virtual method call doesn't dereference "this" then it is perfectly verifiable to call it with a null "this". -- Eric &lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9735133</link><pubDate>Fri, 12 Jun 2009 17:48:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9735133</guid><dc:creator>Pop Catalin</dc:creator><description>&lt;p&gt;@marc.gravell &lt;/p&gt;
&lt;p&gt;It's not the new operator that returns null, new can never return null, it's the box operator that returns null, boxing a Nullable&amp;lt;T&amp;gt; value, has special semantics in the run time that will box a all structures that have the .HasValue field set to false, to a null reference.&lt;/p&gt;
&lt;p&gt;@Joren&lt;/p&gt;
&lt;p&gt;Unless non nullable references are implemented by .Net team and throughout the framework, that non nullable struct you try to implement would only be a clever trick for null checking. You haven't solved or reduced the possibility of a null reference exception in any way, you just moved the throwing of NullReferenceException to another place, maybe more convenient. &lt;/p&gt;
&lt;p&gt;Also a question, you want structures not to have a public parameterlss constructor, well then what happens when such a structure is a used as a field of an object, how can it be initialized? &lt;/p&gt;
&lt;p&gt;Because the default value is not a valid value for the structure, as you stated.&lt;/p&gt;
&lt;p&gt;You immediately need more (more complicated) semantics in the CLR itself to support such structures, the semantics of having structures that are in a valid or invalid state (initialized or not initialized with a valid value), and having those semantics means the non null reference issue is solved already, as it's a special case for valid values of a structure :) (take structure not as struct, but in the general sense, value reference etc).&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9735655</link><pubDate>Fri, 12 Jun 2009 19:02:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9735655</guid><dc:creator>ShuggyCoUk</dc:creator><description>&lt;p&gt;&amp;quot;why are structs required to have a public parameterless constructor instead of requiring that the programmer initializes the structs himself&amp;quot;&lt;/p&gt;
&lt;p&gt;structs are *not* allowed to have parameterless constructors, this is a good thing since structs can be created in their 'blank' state by the creation of either default(T) or new T[]. This will not call a constructor so it avoid confusion.&lt;/p&gt;
&lt;p&gt;If you really need a constructor that does something similar consider having a private one that takes object and pass null to it.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9736089</link><pubDate>Fri, 12 Jun 2009 20:00:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9736089</guid><dc:creator>Dmitry Zaslavsky</dc:creator><description>&lt;p&gt;Eric are you planning to mention also emitting Debuggable attribute with different values.&lt;/p&gt;
&lt;p&gt;This, of course, effects JIT a lot. Or you just planning a new post?&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9736118</link><pubDate>Fri, 12 Jun 2009 20:03:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9736118</guid><dc:creator>Joren</dc:creator><description>&lt;p&gt;@Pop Catalin&lt;/p&gt;
&lt;p&gt;You're absolutely right about the CLR needing support for it if it were to be this way. Obviously this is not a simple change to make now, and I don't think anyone should bother (if people would be making changes to the CLR type system, I'd rather have true non-nullable reference types instead of subtly different structs) but I'm wondering why it wasn't designed like this in the first place. &lt;/p&gt;
&lt;p&gt;It is obvious that it is necessary for the CLR to be allowed to construct a struct with its fields initialized to defaults, if you want to be able to declare a variable of any struct type without initializing the variable.&lt;/p&gt;
&lt;p&gt;But I don't see why that is necessary. What is the point of allowing you to declare but not initialize a struct variable? Why would you allow the use of unassigned variables at all?&lt;/p&gt;
&lt;p&gt;@ShuggyCoUk&lt;/p&gt;
&lt;p&gt;&amp;gt;structs are *not* allowed to have parameterless constructors&lt;/p&gt;
&lt;p&gt;That's not true. You are not allowed to define parameterless constructors, but all structs have them implicitly.&lt;/p&gt;
&lt;p&gt;&amp;gt;If you really need a constructor that does something similar consider having a private one that takes object and pass null to it.&lt;/p&gt;
&lt;p&gt;How would that help me prevent the creation of a struct with its fields initialized to default values?&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9736322</link><pubDate>Fri, 12 Jun 2009 20:22:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9736322</guid><dc:creator>Mark Knell</dc:creator><description>&lt;p&gt;@ Joren's &amp;quot;why are structs required to have a public parameterless constructor&amp;quot;&lt;/p&gt;
&lt;p&gt;Allow me to restate the core point already suggested by Pop Catalin can ShuggyCoUk. This goes back to Eric's recent posts about the essential difference between value and reference types, doesnt' it? Structs are value types, so even having a reference to one requires an initialized instance, hence the requirement of a parameterless constructor. &amp;nbsp;Null reference semantics is a luxury reserved for reference types, by the nature of the difference between value and reference. &amp;nbsp;This is why Nullable&amp;lt;&amp;gt; must exist, i.e., why null reference semantics can't be given to value types inherently.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9736582</link><pubDate>Fri, 12 Jun 2009 20:47:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9736582</guid><dc:creator>Mark Knell</dc:creator><description>&lt;p&gt;@Joren &amp;quot;What is the point of allowing you to declare but not initialize a struct variable?&amp;quot;&lt;/p&gt;
&lt;p&gt;I think you wanted the subjunctive, i.e., what *would&amp;quot; be the point of that, since it's hypothetical. &amp;nbsp;In fact, by the nature of value types, declaration of a struct variable entails initialization. &amp;nbsp;Cf Eric's recent posts:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx"&gt;http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx"&gt;http://blogs.msdn.com/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You're asking to &amp;quot;prevent the creation of a struct with its fields initialized to default values&amp;quot;. &amp;nbsp;This would be a breaking change to fundamental idioms. &amp;nbsp;To take a concrete example, consider an array of value types vs an array of reference types:&lt;/p&gt;
&lt;p&gt;var a = new MyValueType[1];&lt;/p&gt;
&lt;p&gt;var b = new MyReferenceType[1];&lt;/p&gt;
&lt;p&gt;Each array's item is initializes to the default value appropriate to its type. For b[0] that's simply null, but a[0] holds an instance of MyValueType, right from the get-go. &amp;nbsp; Where is that instance going to come from, if not a default constructor? &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9736631</link><pubDate>Fri, 12 Jun 2009 20:53:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9736631</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;&amp;gt; To generate non-static methods we need to generate them on some class. What class? We only have expression trees and statement trees; what we need are declaration trees.&lt;/p&gt;
&lt;p&gt;Eric, I was specifically talking about Expression.CompileToMethod(MethodBuilder) - &lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/dd728224"&gt;http://msdn.microsoft.com/en-us/library/dd728224&lt;/a&gt;(VS.100).aspx - which already gets the MethodBuilder for a particular TypeBuilder. So I can produce the &amp;quot;declaration tree&amp;quot; using TypeBuilder/MethodBuilder as needed, and then &amp;quot;fill in&amp;quot; the methods with Expression.CompileToMethod.&lt;/p&gt;
&lt;p&gt;The problem is that:&lt;/p&gt;
&lt;p&gt;1) It explicitly checks that MethodBuilder is for a static method, and throws otherwise (not sure why, since I don't see why the generated code would be any different - it would just ignore the implicit receiver).&lt;/p&gt;
&lt;p&gt;2) Expression API not provide any way to reference &amp;quot;this&amp;quot; in the tree.&lt;/p&gt;
&lt;p&gt;Admittedly, I might well be trying to use this for a purpose it wasn't even remotely intended for, and the limitations I'm running into aren't really limitations for the real scenario. But I'm genuinely curious who this overload is intended for, then. Is it there just so that we can compile expression trees to AppDomains other than the current one?&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9736673</link><pubDate>Fri, 12 Jun 2009 20:58:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9736673</guid><dc:creator>Tanveer Badar</dc:creator><description>&lt;P&gt;[quote]The existence of that second pass implies that we do a simple arithmetic optimizations on expressions which are only partially constant. For example, if you have a method M that &lt;STRONG&gt;returns an integer&lt;/STRONG&gt;, then code like &lt;/P&gt;
&lt;P&gt;if (M() * 0 == 0) N();&lt;/P&gt;
&lt;P&gt;can be generated as though you’d written just:&lt;/P&gt;
&lt;P&gt;M();&lt;/P&gt;
&lt;P&gt;N(); [/quote]&lt;/P&gt;
&lt;P&gt;Enter Mr. Evil Programmer (disregard any syntax errors, I haven't done operator overloading for a long long time.):&lt;/P&gt;
&lt;P&gt;public static int operator * ( Foo foo , int multiplier ) { return 1; }&lt;/P&gt;
&lt;P&gt;in a certain class returned by M( ).&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;But M returns an integer, by assumption. If M() doesn't return an integer then we do not do this optimization.&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe you made a typo saying that, otherwise, its a serious bug. I am going to try anyway. :)&lt;/P&gt;</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9736799</link><pubDate>Fri, 12 Jun 2009 21:10:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9736799</guid><dc:creator>br1</dc:creator><description>&lt;p&gt;Thank you, Eric. &amp;nbsp;Sorry for not doing my homework. I thought the answer would be more complicated and was not sure where to look. To complement what you said, the C# 3.0 Specification says in 7.4.4 Function member invocation:&lt;/p&gt;
&lt;p&gt;The value of E is checked to be valid. If the value of E is null, a System.NullReferenceException is thrown and no further steps are executed.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9737345</link><pubDate>Fri, 12 Jun 2009 21:49:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9737345</guid><dc:creator>Joren</dc:creator><description>&lt;p&gt;@Mark Knell&lt;/p&gt;
&lt;p&gt;&amp;gt;I think you wanted the subjunctive, i.e., what *would&amp;quot; be the point of that, since it's hypothetical. &amp;nbsp;In fact, by the nature of value types, declaration of a struct variable entails initialization. &lt;/p&gt;
&lt;p&gt;You're right, but I meant &amp;quot;What is the point of allowing you to declare but not *personally* initialize a struct variable?&amp;quot;.&lt;/p&gt;
&lt;p&gt;Your array example illustrates the point sufficiently though. I hadn't thought of that.&lt;/p&gt;
&lt;p&gt;&amp;gt;This is why Nullable&amp;lt;&amp;gt; must exist, i.e., why null reference semantics can't be given to value types inherently.&lt;/p&gt;
&lt;p&gt;I never wanted null for structs. I was talking about not allowing you to specifiy storage of a struct type without personally guaranteeing the storage is initialized sometime, similar to how that happens for local variables.&lt;/p&gt;
&lt;p&gt;You've shown that arrays make this inconvenient, so there is really no point in thinking about it further.&lt;/p&gt;
&lt;p&gt;@Tanveer Badar&lt;/p&gt;
&lt;p&gt;But Eric required M to return an integer, not just any Foo. You can't overload the int * int operator, so the optimization works.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9737971</link><pubDate>Fri, 12 Jun 2009 22:32:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9737971</guid><dc:creator>Gabe</dc:creator><description>&lt;P&gt;What about things like common subexpression elimination and loop invariant hoisting? Those seem like things that the JITter could do, but would be applicable to all environments so the compiler could do it.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;Very few "optimizations" are guaranteed to always be improvements. Both of your examples trade increased use of temporary slots for decreased time: the classic space vs time tradeoff. Some CPUs have huge numbers of available registers, so generating more temporaries is usually a win. &lt;/P&gt;
&lt;P&gt;But on CPUs with a small number of available registers (a large number of our users use computers with x86 architecture!) sometimes generating more temporaries means that you need to move things that would have otherwise always been in registers onto the stack, and then back into registers again later. If the total cost of those data moves is larger than the cost of doing the calculation multiple times, then this optimization just made the codegen worse.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;We do not know what the register architecture is going to be when we generate the IL; therefore we leave decisions like that up to the jitter. -- Eric &lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>What does the optimize switch do? - Eric Lippert</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9741776</link><pubDate>Sat, 13 Jun 2009 07:02:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9741776</guid><dc:creator>DotNetShoutout</dc:creator><description>&lt;p&gt;Thank you for submitting this cool story - Trackback from DotNetShoutout&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9742104</link><pubDate>Sat, 13 Jun 2009 08:32:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9742104</guid><dc:creator>David</dc:creator><description>&lt;p&gt;&amp;quot;We let the jitter team worry about optimizing the heck out of the code...&amp;quot; That seems like another small lie as the C/C++ compiler appears to apply much more aggressive optimizations than the jitter. I'm not talking C/C++ specific stuff either. It's a problem the jitter teams seems to face... they are tasked with loading the assembly as fast as possible AND produce as optimal code as possible. That's a Win/Lose proposition.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9742786</link><pubDate>Sat, 13 Jun 2009 11:20:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9742786</guid><dc:creator>Patrick Smacchia</dc:creator><description>&lt;p&gt;&amp;gt;The non-virtual call instruction does not do a null check; we'd have to generate extra code to check whether GetC returns null.&lt;/p&gt;
&lt;p&gt;If only the Common Type System had some sort of non-nullable reference type there would be no need for such trick. Every .NET developers are struggling everyday with the NullReferenceException while it could have been solved statically at compile time :o/&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://codebetter.com/blogs/patricksmacchia/archive/2007/07/25/i-want-non-nullable-types-in-c-4.aspx"&gt;http://codebetter.com/blogs/patricksmacchia/archive/2007/07/25/i-want-non-nullable-types-in-c-4.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9745591</link><pubDate>Sat, 13 Jun 2009 18:33:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9745591</guid><dc:creator>Jay Bazuzi</dc:creator><description>&lt;p&gt;Is there ever a situation where '/debug-' is desirable?&lt;/p&gt;
&lt;p&gt;Why does the '/debug' switch exist? &lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9753066</link><pubDate>Mon, 15 Jun 2009 15:43:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9753066</guid><dc:creator>Marc Gravelll</dc:creator><description>&lt;p&gt;@Joren - I missed the reference-type note, thanks.&lt;/p&gt;
&lt;p&gt;@Pop Catalin - indeed, I'm very much aware of this; but the *perception* of this behaviour (especially with the GetType() boxing) can easily be deceptive.&lt;/p&gt;
&lt;p&gt;Re the whole constructor/not thing on value-types, isn't that one of those odd areas where the CLI and C# specs have different view, but just agree to disagree? Or has my memory gone to putty...?&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9759170</link><pubDate>Tue, 16 Jun 2009 09:04:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9759170</guid><dc:creator>Rudy</dc:creator><description>&lt;p&gt;@Joren, re no default constructor for structs.&lt;/p&gt;
&lt;p&gt;Another variant on the above answers are that structs are value types, and all value types can be zero. The byproduct of which means the CLR zero's them for you when you create an array of them.&lt;/p&gt;
&lt;p&gt;You can also think of the zero-struct as the same thing as a null pointer if it makes you happy. i.e. string[] arr = new string[100]; creates an array of string references. It doesn't call 100 string constructors either. You have to then set all the instances yourself. It's not really the case, but it gets the point across.&lt;/p&gt;
&lt;p&gt;A side benefit to the CLR writers is that it's really easy to zero out a chunk of the (underlying) memory, and the CLR guarantees you don't get uninitialized data (does it, or is it the compilers contract, i don't know which, whatever).&lt;/p&gt;
&lt;p&gt;While these may not be the published reasons, they're still consistent with the way the world is.&lt;/p&gt;
&lt;p&gt;enum Silly&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; One = 1,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; Two = 2&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;It's like enum's. Despite being first-class citizens, the underlying storage is an int (value type) and the default is 0. So if you have an unitinitalized enum as a field, the CLR will, by default, initialize your enum field to zero like it says it will. However like the Silly enum above, you won't be able to test to your enums in a switch because you have no value which corresponds to 0.&lt;/p&gt;
&lt;p&gt;You win some, you lose some. You end up with the best-practice rule of 'always have a 0 value for an enum as a sensible default or suffer'.&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9772207</link><pubDate>Thu, 18 Jun 2009 06:43:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9772207</guid><dc:creator>sukru</dc:creator><description>&lt;p&gt;JITs have the capability to fast load, and excessively optimize, at the same time.&lt;/p&gt;
&lt;p&gt;Well, not actually at the same time, however JITs, theoretically, can load a rough version, and optimize the code in a background task. I heard that CLI already does that, but I'm not exactly .sure&lt;/p&gt;
</description></item><item><title>Do PDB Files Affect Performance?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9779559</link><pubDate>Fri, 19 Jun 2009 08:27:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9779559</guid><dc:creator>John Robbins' Blog</dc:creator><description>&lt;p&gt;After a detour into Historical Debugging , it’s time to come back to return to answering questions about&lt;/p&gt;
</description></item><item><title>re: What does the optimize switch do?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx#9866352</link><pubDate>Wed, 12 Aug 2009 16:11:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9866352</guid><dc:creator>Andrew</dc:creator><description>&lt;p&gt;I realise the blog post is old, I hope someone gets it :) &lt;/p&gt;
&lt;p&gt;I was wondering why you would not use both /debug and /optimize at the same time to get the best of both worlds?&lt;/p&gt;
&lt;p&gt;Does using /debug with /optimize produce less than optimal code if /debug were omitted?&lt;/p&gt;
&lt;p&gt;Why wouldn't you want to use /optimize in the first place?&lt;/p&gt;
&lt;p&gt;Cheers&lt;/p&gt;
</description></item></channel></rss>