<?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>Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx</link><description>My colleague Vance Morrison wrote an internal paper on code quality issues in our current system. I thought there were some excellent items discussed in his paper so with his kind permission I've edited/summarized it for a general audience. Thank you</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#378477</link><pubDate>Wed, 23 Feb 2005 01:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:378477</guid><dc:creator>Adam</dc:creator><description>Exception Handling: They're doing this because you (microsoft as a whole) told them initially this was the new way and that it was practically free.&lt;br&gt;&lt;br&gt;In reality, its expensive and this advice was poor.</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#378482</link><pubDate>Wed, 23 Feb 2005 01:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:378482</guid><dc:creator>Rico Mariani</dc:creator><description>See &lt;a target="_new" href="http://weblogs.asp.net/ricom/archive/2003/12/19/44697.aspx"&gt;http://weblogs.asp.net/ricom/archive/2003/12/19/44697.aspx&lt;/a&gt; for more details on exception handling guidelines.&lt;br&gt;</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#378512</link><pubDate>Wed, 23 Feb 2005 01:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:378512</guid><dc:creator>Matthew W. Jackson</dc:creator><description>I personally believe that the lack of inlining for Value Types is the biggest issue in current JIT performance.  I think that Value Types in general need much more aggressive inlining than reference types.  If performance were not an issue, nobody would be using Value Types in the first place.&lt;br&gt;&lt;br&gt;Special-casing of generics for Value Types is great (at the cost of a bigger working set), but the lack of inling makes it difficult/unworthwhile to create efficient lightweight wrappers for other primitive types that add additional features (like say a wrapper for Int32 that restricted its values, or implemented a generic interface such as IArithmetic&amp;lt;T&amp;gt; for doing math in generics).  It also adds penalties when creating new primitive-like types (such as a Complex number).&lt;br&gt;&lt;br&gt;Personally, I think that there should be really aggressive inlining on overloaded operators, property accessors, and constructors defined for Value Types, even if it increases the initial cost of JITing a Value Type a bit.&lt;br&gt;&lt;br&gt;Most programmers do not &amp;quot;see&amp;quot; the added cost of calling an overloaded operator, and the runtime (or maybe even the C# compiler) should work as hard as possible to inline them.&lt;br&gt;&lt;br&gt;Even System.Decimal would benefit from such changes to the JIT, and it is very frequently used in business applications.</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#378756</link><pubDate>Wed, 23 Feb 2005 11:08:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:378756</guid><dc:creator>Stu Smith</dc:creator><description>With regards interop with unmanaged code, I wonder if you'd consider writing a post on suggestions for performance regarding crossing the managed/unmanaged boundary? We have a slight performance issue with the following, and I'm sure it must be a common scenario (even if not exactly the same).&lt;br&gt;&lt;br&gt;We started our application (in C#) pretty much as soon as .NET 1.0 appeared. Front-end and business objects are written entirely in C#. We reused an in-house object/relational system that sits on top of the database, which we upgraded from straight C++. In the middle is an auto-generated data-access layer, with a pair of object/collection classes per table, and a pair of get/set methods per column. Business objects are stateless and only contain a reference to the data-acess object. These d/a methods have the form:&lt;br&gt;&lt;br&gt;// MC++&lt;br&gt;Type Table::GetField()&lt;br&gt;{&lt;br&gt;	// Type is 'simple': one of int, bool, System::String *, DateTime, etc.&lt;br&gt;&lt;br&gt;	Type value = m_pRow-&amp;gt;GetValue( &amp;lt;fieldindex&amp;gt; ); // m_pRow is unmanaged.&lt;br&gt;&lt;br&gt;	GC::KeepAlive( this );&lt;br&gt;&lt;br&gt;	return value;&lt;br&gt;}&lt;br&gt;&lt;br&gt;So obviously the interface is too 'chatty' but that's hindsight and can't be easily changed. How can we know if we're making the cheapest possible call here (you mentioned argument conversion and security)?&lt;br&gt;</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#378793</link><pubDate>Wed, 23 Feb 2005 12:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:378793</guid><dc:creator>Hallvard Vassbotn</dc:creator><description>&amp;gt; The current just in time (JIT) compiler is more limited than a typical commercial quality unmanaged compiler, partly because it needs to be smaller and faster and partly because it just isn’t as mature. Some of the larger issues include.&lt;br&gt;&lt;br&gt;I recently read about the improved NGEN in 2.0:&lt;br&gt;&lt;a target="_new" href="http://msdn.microsoft.com/msdnmag/issues/05/04/NGen/default.aspx"&gt;http://msdn.microsoft.com/msdnmag/issues/05/04/NGen/default.aspx&lt;/a&gt;&lt;br&gt;&lt;br&gt;The article indicates that NGEN works by invoking the same underlying JIT compiler. But at install time, there isn't the time constraint. Why can't NGEN use a more agressive optimizer than the JIT'er?&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#378813</link><pubDate>Wed, 23 Feb 2005 13:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:378813</guid><dc:creator>Goran Pušić</dc:creator><description>On exceptions (first comment):&lt;br&gt;&lt;br&gt;&amp;quot;In reality, its expensive and this advice was poor&amp;quot;&lt;br&gt;&lt;br&gt;Not true. Exceptions have a cost, granted. But, the thing is, if they are used to actually handle errors (1/1000 rule from Rico's other post), the cost is not important. Why? Well, because if you have an error, the amount of processing to do is normally MUCH less than what's needed for normal operation (we write code to do stuff and not to handle errors, don't we? BTW, that's also why exceptions are good: they help us to have to write less code for error handling; that leaves us more time to write code that does stuff)&lt;br&gt;&lt;br&gt;The important overhead, then, is the &amp;quot;static&amp;quot; one for exception handling init/cleanup. But, there, do not forget that you compare the compiler-generated code that you don't see, with code for error handling that you write. So, it's not that you have hidden overhead in case of exception-enabled environment as opposed to NOTHING in exception-free (C code, anyone?) environments.&lt;br&gt;</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#378898</link><pubDate>Wed, 23 Feb 2005 15:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:378898</guid><dc:creator>Jeff Atwood</dc:creator><description>&amp;gt; You (microsoft as a whole) told them initially this was the new way and that it was practically free. &lt;br&gt;&lt;br&gt;Where have you seen this? Everything I've read, and I do mean everything, told me that exceptions are expensive and should be avoided-- eg, used for only &amp;quot;Exceptional&amp;quot; cases.&lt;br&gt;&lt;br&gt;In practice, exceptions are brutally slow-- literally slower than a database query! But I'm fine with that, because I only use them in exceptional conditions as designed.&lt;br&gt;&lt;br&gt;If you have read this somewhere (microsoft docs saying exceptions are free), can you provide a link to it?</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#379113</link><pubDate>Wed, 23 Feb 2005 20:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379113</guid><dc:creator>Dmitriy Zaslavskiy</dc:creator><description>Why can't CLR use thing like GetWriteWatch instead of memorybarrier?</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#379391</link><pubDate>Thu, 24 Feb 2005 04:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379391</guid><dc:creator>Jeffrey Sax</dc:creator><description>My vote for most pressing performance issue also goes to inlining value types. This should have been a priority from the outset, not an afterthought.&lt;br&gt;&lt;br&gt;By their nature, value types tend to be used for small, fundamental types like complex numbers, intervals, quantities (size + unit), etc. Method bodies are usually very small. These value types are used in calculations, very often inside loops.&lt;br&gt;&lt;br&gt;A second issue concerns mutable reference types with value semantics. These are sometimes necessary, but seem to run against the NET philosophy and their use is discouraged at every opportunity. One example: C# doesn't allow overloading of compound assignment operators. It just assumes your objects are immutable and therefore &amp;quot;operator synthesis&amp;quot; (x@=y -&amp;gt; x=x@y) is all you need.&lt;br&gt;&lt;br&gt;The String/Builder pattern can't be used for objects like vectors and matrices. Like value types, these objects are used in calculations, implying that their component values change often. Recycling is a must. Bounds checking can often be eliminated if certain class-level invariants can be verified.&lt;br&gt;</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#379410</link><pubDate>Thu, 24 Feb 2005 05:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379410</guid><dc:creator>Rico Mariani</dc:creator><description>Jeffery Sax's thoughts are closely aligned with mine.  Of the JIT issues, the inliner is the thing I would most like to see improved and handling of value types in the inliner is doubly important.  I think value types are largely under-used and perhaps they might be used more often if you could cash in, in practice, on the gains that they offer in theory.&lt;br&gt;&lt;br&gt;But of course all the areas identified as weaknesses in the Jit above are obviously on our minds.</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#379503</link><pubDate>Thu, 24 Feb 2005 09:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379503</guid><dc:creator>Goran Pušić</dc:creator><description>Jeff:&lt;br&gt;&lt;br&gt;&amp;quot;In practice, exceptions are brutally slow-- literally slower than a database query!&amp;quot;&lt;br&gt;&lt;br&gt;I call bullshit! Care to show a sample program that times an exception and a DB query that proves this? I'll show you one where DB query is slower!</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#379612</link><pubDate>Thu, 24 Feb 2005 14:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379612</guid><dc:creator>Frank Hileman</dc:creator><description>Vote for inlining on value types:&lt;br&gt;&lt;a target="_new" href="http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=fb7b3c93-a9e9-418b-85b3-b67a195c7e1a"&gt;http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=fb7b3c93-a9e9-418b-85b3-b67a195c7e1a&lt;/a&gt;&lt;br&gt;&lt;br&gt;This will make VG.net even faster.</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#379614</link><pubDate>Thu, 24 Feb 2005 14:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379614</guid><dc:creator>Ryan Lamansky (Kardax)</dc:creator><description>It's good to know that value-type inlining is on your (Microsoft's) minds.  Not having it is driving me insane!&lt;br&gt;&lt;br&gt;If I had the choice, I would give up every single one of .NET 2.0's new features in exchange for value-type inlining.  Seriously.</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#379693</link><pubDate>Thu, 24 Feb 2005 16:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379693</guid><dc:creator>Rico Mariani</dc:creator><description>There was a question about GetWriteWatch() up there.  It's an interesting strategy to use OS based information instead of write barriers but I don't think you'd net any performance doing it.  &lt;br&gt;&lt;br&gt;I see these problems:&lt;br&gt;&lt;br&gt;-using GetWriteWatch will manipulate the page tables, that causes more TLB misses and loading CR0 ain't cheap anyway, we'd have to do this on every garbage collection&lt;br&gt;-once armed, you'll pretty much have to take the equivalent of soft page faults to clear the bit (the OS does this for you), those aren't cheap neither&lt;br&gt;-the granularity would be at the page level, so we'd have to assume a lot more pointers are dirty&lt;br&gt;-if we use that feature then others (like debuggers) can't, and it seems more useful to them than to us&lt;br&gt;&lt;br&gt;At the end of the day Write Barriers aren't killing us.</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#379695</link><pubDate>Thu, 24 Feb 2005 17:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379695</guid><dc:creator>Rico Mariani</dc:creator><description>In order to get an exception to compete in slowness with a database query, you first need a pretty simple query.  e.g. &amp;quot;select 1&amp;quot;&lt;br&gt;&lt;br&gt;Obviously a highly complex query could take arbitrarily long.&lt;br&gt;&lt;br&gt;Secondly, perhaps the biggest cost of the exception is gathering the callstack information.  So you'd want to do the throw from a very deeply nested function.&lt;br&gt;&lt;br&gt;So I could imagine a case where all that gathering is actually slower than a network round trip to a database but I don't think it would be the norm.&lt;br&gt;&lt;br&gt;I guess the other factor is there could be an arbitrary number of finally clauses that did an arbitrary amount of work.  So if you timed from the throw to the catch you might be waiting a while.&lt;br&gt;&lt;br&gt;That's the thing about throwing -- you just don't know what is going to happen exactly -- it's both the strength and the weakness of the mechanisms generality.&lt;br&gt;</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#379840</link><pubDate>Thu, 24 Feb 2005 20:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379840</guid><dc:creator>Dmitriy Zaslavskiy</dc:creator><description>Back to using OS info.&lt;br&gt;Like you said measure first. Do you guys have a prototype?&lt;br&gt;I can argue that in app that takes does little GC compare to updating it's structures this will be a significant win. &lt;br&gt;Extra code/data for wb will cause more data to be touched trashing TLB etc. &lt;br&gt;On large memory systems you would probably wont to have granularity bigger than even a page (sans large pages)&lt;br&gt;I am sure you would agree that this is nowhere near a clear cut desision.&lt;br&gt;&lt;br&gt;And I didn't understand your last point about debuggers</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#379981</link><pubDate>Thu, 24 Feb 2005 23:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:379981</guid><dc:creator>murphee</dc:creator><description>On Write Barriers:&lt;br&gt;If you're curious about the impact of &lt;br&gt;Write Barriers refer to this paper:&lt;br&gt;&lt;a target="_new" href="http://cs.anu.edu.au/~Steve.Blackburn/pubs/papers/wb-ismm-2004.pdf"&gt;http://cs.anu.edu.au/~Steve.Blackburn/pubs/papers/wb-ismm-2004.pdf&lt;/a&gt;&lt;br&gt; (The Title &amp;quot;Write Barriers - Friend or Foe?&amp;quot; should tell you what it's about).</description></item><item><title>re: Qualitative Code Differences in Managed Code</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#380432</link><pubDate>Fri, 25 Feb 2005 17:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:380432</guid><dc:creator>Jeffrey Sax</dc:creator><description>Not all exceptions are thrown by code deep down in the call stack. Exceptions are often raised by the CLR because of an issue with code inside a method. For example: OverflowException on a checked block.&lt;br&gt;&lt;br&gt;A form of 'light-weight exception handling' for these situations, where the JIT bypasses the full exception handling mechanism, would be very welcome. I.e. if a CLR exception is thrown by code inside a try block, and the exception is caught in a corresponding catch block, the overhead of building a full-featured exception object could be eliminated.&lt;br&gt;&lt;br&gt;This is especially important since the CLI spec states that certain tests throw an exception if the test fails rather than branch if it succeeds. Example: the ckfinite instruction. In other words, without this type of optimization, the CLI *imposes* severe performance degradation.&lt;br&gt;&lt;br&gt;I'm aware that exception handling code can be extremely complicated, but that does not mean that the simple cases cannot be optimized.</description></item><item><title>Blog link of the week 08</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#381338</link><pubDate>Mon, 28 Feb 2005 02:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:381338</guid><dc:creator>Daniel Moth</dc:creator><description>Blog link of the week 08</description></item><item><title>My Life as a Managed Code Junkie - Part 4</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#546888</link><pubDate>Thu, 09 Mar 2006 08:45:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:546888</guid><dc:creator>James Kovacs' Weblog</dc:creator><description /></item><item><title>Gamefest 2007: The Costs of Managed Code: The Avoidable and the Unavoidable </title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#4824759</link><pubDate>Sat, 08 Sep 2007 10:04:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4824759</guid><dc:creator>Rico Mariani's Performance Tidbits</dc:creator><description>&lt;p&gt;A few weeks ago I spoke at Gamefest 2007 where I delivered this talk: The Costs of Managed Code: The&lt;/p&gt;
</description></item><item><title>MSDN Blog Postings  &amp;raquo; Gamefest 2007: The Costs of Managed Code: The Avoidable and the Unavoidable</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#4826288</link><pubDate>Sat, 08 Sep 2007 12:36:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4826288</guid><dc:creator>MSDN Blog Postings  » Gamefest 2007: The Costs of Managed Code: The Avoidable and the Unavoidable</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://msdnrss.thecoderblogs.com/2007/09/08/gamefest-2007-the-costs-of-managed-code-the-avoidable-and-the-unavoidable/"&gt;http://msdnrss.thecoderblogs.com/2007/09/08/gamefest-2007-the-costs-of-managed-code-the-avoidable-and-the-unavoidable/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Some notes on .NET performance &amp;laquo; Handwaving</title><link>http://blogs.msdn.com/ricom/archive/2005/02/22/378335.aspx#6922412</link><pubDate>Mon, 31 Dec 2007 21:35:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6922412</guid><dc:creator>Some notes on .NET performance « Handwaving</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://surana.wordpress.com/2007/12/31/some-notes-on-net-performance/"&gt;http://surana.wordpress.com/2007/12/31/some-notes-on-net-performance/&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>