<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.iis.net/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/" xmlns:cs="http://blogs.iis.net/"><channel><title>Tobin Titus Blog : CIL</title><link>http://blogs.iis.net/tobintitus/archive/tags/CIL/default.aspx</link><description>Tags: CIL</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Improving code performance</title><link>http://blogs.iis.net/tobintitus/archive/2006/06/06/improving-code-performance.aspx</link><pubDate>Tue, 06 Jun 2006 20:40:00 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:1307054</guid><dc:creator>TobinTitus</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.iis.net/tobintitus/rsscomments.aspx?PostID=1307054</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.iis.net/tobintitus/commentapi.aspx?PostID=1307054</wfw:comment><comments>http://blogs.iis.net/tobintitus/archive/2006/06/06/improving-code-performance.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Arial size=2&gt;Recently, an old co-worker contacted me to ask me a question about code performance. Specifically, he was emitting IL from his code and had some questions about some of the opcode usage he witnessed when viewing the IL of some compiled assemblies.&amp;nbsp; The question was based on a simple application he wrote in C#, compiled, and disassembled.&amp;nbsp; He did this&amp;nbsp;to see how the C# compiler produced IL and give him clues in how he should emit IL.&amp;nbsp; The function in question was as follows:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT color=blue&gt;public object&lt;/FONT&gt; GetProp(&lt;FONT color=blue&gt;string&lt;/FONT&gt; name) &lt;BR&gt;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=blue&gt;if&lt;/FONT&gt; (name == &lt;FONT color=maroon&gt;"X"&lt;/FONT&gt;)&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=blue&gt;return this&lt;/FONT&gt;.X;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=blue&gt;return null;&lt;BR&gt;} &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Now, the code obviously isn't meant to do anything other than lend some insight into the IL.&amp;nbsp; Compiling to 'debug' the following IL was produced.&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face=Fixedsys size=2&gt;&lt;BR&gt;.method public hidebysig instance object &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetProp(string name) cil managed&lt;BR&gt;{&lt;BR&gt;// Code size&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 35 (0x23)&lt;BR&gt;.maxstack&amp;nbsp; 2&lt;BR&gt;.locals init ([0] object CS$1$0000,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [1] bool CS$4$0001)&lt;BR&gt;IL_0000:&amp;nbsp; nop&lt;BR&gt;IL_0001:&amp;nbsp; ldarg.1&lt;BR&gt;IL_0002:&amp;nbsp; ldstr&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "X"&lt;BR&gt;IL_0007:&amp;nbsp; call&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool [mscorlib]System.String::op_Equality(string, string)&lt;BR&gt;IL_000c:&amp;nbsp; ldc.i4.0&lt;BR&gt;IL_000d:&amp;nbsp; ceq&lt;BR&gt;IL_000f:&amp;nbsp; stloc.1&lt;BR&gt;IL_0010:&amp;nbsp; ldloc.1&lt;BR&gt;IL_0011:&amp;nbsp; brtrue.s&amp;nbsp;&amp;nbsp; IL_001d&lt;BR&gt;IL_0013:&amp;nbsp; nop&lt;BR&gt;IL_0014:&amp;nbsp; ldarg.0&lt;BR&gt;IL_0015:&amp;nbsp; call&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; instance string class TestApp.TClass`1::get_X()&lt;BR&gt;IL_001a:&amp;nbsp; stloc.0&lt;BR&gt;IL_001b:&amp;nbsp; br.s&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IL_0021&lt;BR&gt;IL_001d:&amp;nbsp; ldnull&lt;BR&gt;IL_001e:&amp;nbsp; stloc.0&lt;BR&gt;IL_001f:&amp;nbsp; br.s&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IL_0021&lt;BR&gt;IL_0021:&amp;nbsp; ldloc.0&lt;BR&gt;IL_0022:&amp;nbsp; ret&lt;BR&gt;} // end of method TClass`1::GetProp&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;The question was, why was the stloc.1 and ldloc.1 needed after the ceq instruction at IL_000d (there are actually other issues in this small snippet, but I'll focus on this particular one)&amp;nbsp;. I, too, tried to resolve the issue and batted a few guesses around.&amp;nbsp; I proffered two ideas, and then ultimately suggested that the JIT compiler would likely be modifying this code anyway (particularly once it was recompiled in 'release' with optimization).&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Still curious as to why the compiler produced the stloc and ldloc opcodes, I asked around internally until Vance set me straight with this blog post.&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT:0px;"&gt;
&lt;P&gt;&lt;FONT color=blue&gt;&lt;B&gt;Introduction: What does ‘foreach’ actually do?&lt;/B&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;A title=http://blogs.msdn.com/vancem/archive/2006/02/20/535807.aspx href="http://blogs.msdn.com/vancem/archive/2006/02/20/535807.aspx"&gt;&lt;FONT color=#800080&gt;http://blogs.msdn.com/vancem/archive/2006/02/20/535807.aspx&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Essentially, he states what I initially felt -- that the JIT transformations on the IL are so dramatic, that you cannot judge an application's performance based on the IL.&amp;nbsp; He also gives some great information on how to view your JITed code -- with release optimizations and everything.&amp;nbsp; The other side to this is, that after further review, the inefficiencies of the IL were fixed in the optimized IL anyway once the code was set to 'release'.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial size=2&gt;Sometimes, it's really easy to get side-tracked by these discussions in your quest for software glory.&amp;nbsp; I'm glad to know we have people like Vance around to set me straight when I do.&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=1307054" width="1" height="1"&gt;</description><category domain="http://blogs.iis.net/tobintitus/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.iis.net/tobintitus/archive/tags/CIL/default.aspx">CIL</category><category domain="http://blogs.iis.net/tobintitus/archive/tags/.NET/default.aspx">.NET</category></item></channel></rss>