<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-1501586010252802469</atom:id><lastBuildDate>Fri, 17 Feb 2012 00:47:09 +0000</lastBuildDate><category>GC</category><category>Debugging</category><category>Memory</category><category>Development</category><category>Performance</category><category>Multithreading</category><category>CLR Hosting</category><category>.Net Internals</category><category>Networking</category><title>Liran Chen's Blog</title><description>.Net Internals, Debugging, Multithreading - and More!</description><link>http://blog.liranchen.com/</link><managingEditor>noreply@blogger.com (Liran Chen)</managingEditor><generator>Blogger</generator><openSearch:totalResults>17</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/LiranChen" /><feedburner:info uri="liranchen" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-6602582827722806714</guid><pubDate>Fri, 12 Aug 2011 16:13:00 +0000</pubDate><atom:updated>2011-12-18T23:22:59.703+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">GC</category><category domain="http://www.blogger.com/atom/ns#">Memory</category><category domain="http://www.blogger.com/atom/ns#">Performance</category><title>Writing a Manual Memory Manager in C#</title><description>Garbage collection. Aye? or Nay?&lt;br /&gt;
As usual, it depends. That is, on which developer you might ask. Some like to have as much control as possible over the way their code executes, while others simply love the fact that they don't have to deal with the "mundane" job of keeping track on their memory allocations.&lt;br /&gt;
Since there aren't really any "absolute truths" in anything related to programming, in reality you'd sometimes want to have complete control over your memory management, while at other times you wouldn't really care about it "as long as it gets done".&lt;br /&gt;
Since we're mostly discussing .Net and here, we could say that we've got the "as long as it gets done" part covered quite well, by the CLR's garbage collection mechanism. So it's time to see how we could approach implementing a manual memory manager in C#.&lt;br /&gt;
&lt;br /&gt;
What we've eventually would like to have, is an API that would enable us to allocate and deallocate typed memory on demand. Of course C# doesn't natively support the &lt;i&gt;new&lt;/i&gt; and &lt;i&gt;delete&lt;/i&gt; keywords we so kindly remember from C++, so we'll have come up with our own utility functions to do the job.&lt;br /&gt;
Eventually, our code should look something similar to this:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="border: #000080 1px solid; color: black; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;"&gt;
&lt;div style="background-color: white; max-height: 400px; overflow: auto; padding: 2px 5px; white-space: nowrap;"&gt;
&lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Main()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ITypeAllocator&lt;/span&gt; mgr = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ManalocManager&lt;/span&gt;();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;IFooData&lt;/span&gt; obj = &lt;span style="background-color: white;"&gt;mgr.New&amp;lt;&lt;/span&gt;&lt;span style="background-color: white; color: #2b91af;"&gt;IFooData&lt;/span&gt;&lt;span style="background-color: white;"&gt;&amp;gt;();&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;obj.Bar = 1;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;obj.Car = 2;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;obj.Jar = 3;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;obj.Tar = 4;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; m&lt;span style="background-color: white;"&gt;gr.Delete(obj);&lt;/span&gt;&lt;br /&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
Disabling the garbage collector completely is an unreasonable thing to do in a platform such as .Net. Doing so would probably miss the platform's purpose. Anyone who truly wants to have &lt;i&gt;complete&lt;/i&gt; control over the execution of its program wouldn't bother using C# anyway (or any other managed language for that matter).&lt;br /&gt;
However, while using C#, there might be some times that we'll want to manage our own memory, instead of having the garbage collector doing it for us. And even if not, it's still a subject interesting enough to explore and mostly play with.&lt;br /&gt;
&lt;br /&gt;
In order to demonstrate how we could do achieve manual memory management in C#, lets have a look at the following interface:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="border: #000080 1px solid; color: black; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;"&gt;
&lt;div style="background-color: white; max-height: 400px; overflow: auto; padding: 2px 5px; white-space: nowrap;"&gt;
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IFooData&lt;/span&gt;&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; Bar { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;long&lt;/span&gt; Jar { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt; Car { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;byte&lt;/span&gt; Tar { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;br /&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
The classic method to implement this interface would be to create a class with four members that will match the coresponding properties. However, doing so will result in a 21 bytes structure that will reside in the GC heap (not counting padding and the preceding &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163791.aspx"&gt;object header&lt;/a&gt;).&lt;br /&gt;
Instead, we could allocate the required memory block in the native heap (using&lt;i&gt; &lt;/i&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.allochglobal.aspx"&gt;AllocHGlobal&lt;/a&gt;) and modify out propertie's to access the native memory at the required offsets (e.g. 0 for Bar, 4 for Jar and 12 for Car). Using a &lt;i&gt;Delete&lt;/i&gt; method, we could free the native memory block on demand, when we please.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="border: #000080 1px solid; color: black; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;"&gt;
&lt;div style="background-color: white; max-height: 400px; overflow: auto; padding: 2px 5px; white-space: nowrap;"&gt;
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;FooData&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;IFooData&lt;/span&gt;&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;unsafe&lt;/span&gt; &lt;span style="color: blue;"&gt;byte&lt;/span&gt;* _native;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt; FooData()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;unsafe&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_native = (&lt;span style="color: blue;"&gt;byte&lt;/span&gt;*)(&lt;span style="color: #2b91af;"&gt;Marshal&lt;/span&gt;.AllocHGlobal(21).ToPointer());&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; Bar&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;unsafe&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; *(&lt;span style="color: blue;"&gt;int&lt;/span&gt;*)&amp;amp;_native[0]; } }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt; { &lt;span style="color: blue;"&gt;unsafe&lt;/span&gt; { *(&lt;span style="color: blue;"&gt;int&lt;/span&gt;*)&amp;amp;_native[0] = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; } }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;long&lt;/span&gt; Jar&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;unsafe&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; *(&lt;span style="color: blue;"&gt;long&lt;/span&gt;*)&amp;amp;_native[4]; } }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt; { &lt;span style="color: blue;"&gt;unsafe&lt;/span&gt; { *(&lt;span style="color: blue;"&gt;long&lt;/span&gt;*)&amp;amp;_native[4] = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; } }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;double&lt;/span&gt; Car&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;unsafe&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; *(&lt;span style="color: blue;"&gt;double&lt;/span&gt;*)&amp;amp;_native[12]; } }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt; { &lt;span style="color: blue;"&gt;unsafe&lt;/span&gt; { *(&lt;span style="color: blue;"&gt;double&lt;/span&gt;*)&amp;amp;_native[12] = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; } }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;byte&lt;/span&gt; Tar&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;unsafe&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; *(&lt;span style="color: blue;"&gt;byte&lt;/span&gt;*)&amp;amp;_native[20]; } }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt; { &lt;span style="color: blue;"&gt;unsafe&lt;/span&gt; { *(&lt;span style="color: blue;"&gt;byte&lt;/span&gt;*)&amp;amp;_native[20] = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; } }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;unsafe&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Marshal&lt;/span&gt;.FreeHGlobal(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IntPtr&lt;/span&gt;(((&lt;span style="color: blue;"&gt;void&lt;/span&gt;*)(_native))));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_native = (&lt;span style="color: blue;"&gt;byte&lt;/span&gt;*)(&lt;span style="color: #2b91af;"&gt;IntPtr&lt;/span&gt;.Zero);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
The problem with such implementation is that it could be very tedious to code and implement. Even for simple structures like &lt;i&gt;IFooData&lt;/i&gt;, the resulting implementation could be quite taunting.&lt;br /&gt;
Fortunately enough, we can automate the implementation process by adding a code generator that will implemenet our interfaces on the fly, during runtime.&lt;br /&gt;
The following interface should loosely describe the capabilities our manual memory manager should support:&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;div style="border: #000080 1px solid; color: black; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;"&gt;
&lt;div style="background-color: white; max-height: 400px; overflow: auto; padding: 2px 5px; white-space: nowrap;"&gt;
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ITypeAllocator&lt;/span&gt;&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T New&lt;t&gt;();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete&lt;t&gt;(T instance);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; PreGenerate(&lt;span style="color: blue;"&gt;params&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt;[] types);&lt;br /&gt;
}&lt;/t&gt;&lt;/t&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
The generic parameter &lt;i&gt;T&lt;/i&gt; accepts user-defined data representing interfaces such as &lt;i&gt;IFooData&lt;/i&gt;.&lt;br /&gt;
Once the &lt;i&gt;New&lt;/i&gt; method is called, our manager should generate code, compile it and instancate it during runtime. The resulting instance is then returned to the caller for it to be used. Once it finishes using it, and wants to release its memory, it calls the &lt;i&gt;Delete&lt;/i&gt; method.&lt;br /&gt;
The &lt;i&gt;PreGenerate&lt;/i&gt; method's purpose is the optimize the code's generation/compilation process. Once the user pre-generates a type, it won't have to wait on the first call to the &lt;i&gt;New&lt;/i&gt; method (much like the process of &lt;a href="http://blog.liranchen.com/2010/08/forcing-jit-compilation-during-runtime.html"&gt;forcing the JIT compiler&lt;/a&gt; to execute on your assemblies).&lt;br /&gt;
&lt;br /&gt;
When it comes to code generation, there are basically two ways to choose from: CodeDOM and Templates. Each one of them has its pros and cons, personaly I tend to prefer the CodeDOM way of doing things. While using it could result in quite verbose code, I believe that its easier to maintain in larger projects than templates.&lt;br /&gt;
Unforutantly, .Net's CodeDOM model doesn't support &lt;i&gt;unsafe code&lt;/i&gt;, so I had to resort to using some workarounds to represent all of the unsafe code blocks.&lt;br /&gt;
This should be a good time to mention the &lt;a href="http://www.codeproject.com/KB/cs/refly.aspx"&gt;Refly&lt;/a&gt; library which wraps around .Net CodeDOM API, making it much simpler and innutative to use.&lt;br /&gt;
The demonstrated implementation is very naive and limited regarding the kind of types it is able to generate, though it should illustrate the discussed concept.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="border: #000080 1px solid; color: black; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;"&gt;
&lt;div style="background-color: white; max-height: 400px; overflow: auto; padding: 2px 5px; white-space: nowrap;"&gt;
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ManalocManager&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;ITypeAllocator&lt;/span&gt;&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;// key: userType, value: generatedType&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt;&amp;gt; m_generatedTypesCache;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt; ManalocManager()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m_generatedTypesCache = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt;&amp;gt;();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete&lt;t&gt;(T instance)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt; (!(instance &lt;span style="color: blue;"&gt;is&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IManalocGeneratedType&lt;/span&gt;))&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;throw&lt;/span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ArgumentException&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"Attempted to delete an unexpected type"&lt;/span&gt;);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;IManalocGeneratedType&lt;/span&gt; generatedType = (&lt;span style="color: #2b91af;"&gt;IManalocGeneratedType&lt;/span&gt;)instance;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;generatedType.Delete();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; PreGenerate(&lt;span style="color: blue;"&gt;params&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt;[] types)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; curUserType &lt;span style="color: blue;"&gt;in&lt;/span&gt; types)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;generateAndAddToCache(curUserType);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt; T New&lt;t&gt;()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; userType = &lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(T);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; generatedType;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;bool&lt;/span&gt; alreadyGenerated = m_generatedTypesCache.TryGetValue(userType, &lt;span style="color: blue;"&gt;out&lt;/span&gt; generatedType);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt; (!alreadyGenerated)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;generatedType = generateAndAddToCache(userType);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;object&lt;/span&gt; result = &lt;span style="color: #2b91af;"&gt;Activator&lt;/span&gt;.CreateInstance(generatedType);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt; (T)result;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; generateAndAddToCache(&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; userType)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; generatedType = generateProxy(userType);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m_generatedTypesCache.Add(userType, generatedType);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt; generatedType;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; generateProxy(&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; userType)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;NamespaceDeclaration&lt;/span&gt; ns;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt; typeName = createType(userType, &lt;span style="color: blue;"&gt;out&lt;/span&gt; ns);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt; sourceFile = generateCode(ns);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Assembly&lt;/span&gt; compiledAssembly = compile(userType, sourceFile);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; compiledType = compiledAssembly.GetType(typeName);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt; compiledType;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; createType(&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; userType, &lt;span style="color: blue;"&gt;out&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;NamespaceDeclaration&lt;/span&gt; namespaceDec)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;PropertyInfo&lt;/span&gt;[] userProperties = userType.GetProperties();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;namespaceDec = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;NamespaceDeclaration&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"Manaloc.AutoGenerated"&lt;/span&gt;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;namespaceDec.Imports.Add(userType.Namespace);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ClassDeclaration&lt;/span&gt; classDec = namespaceDec.AddClass(userType.Name + &lt;span style="color: #a31515;"&gt;"_Manaloc_AutoGenerated"&lt;/span&gt;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;classDec.Interfaces.Add(userType);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;classDec.Interfaces.Add(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;IManalocGeneratedType&lt;/span&gt;));&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;FieldDeclaration&lt;/span&gt; nativeMember = classDec.AddField(&lt;span style="color: #a31515;"&gt;"unsafe byte*"&lt;/span&gt;, &lt;span style="color: #a31515;"&gt;"native"&lt;/span&gt;);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;addConstructor(userProperties, classDec, nativeMember);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;addDeleteMethod(classDec, nativeMember);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;addProperties(userProperties, classDec, nativeMember);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt; typeName = namespaceDec.Name + &lt;span style="color: #a31515;"&gt;"."&lt;/span&gt; + classDec.Name;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt; typeName;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; addConstructor(&lt;span style="color: #2b91af;"&gt;PropertyInfo&lt;/span&gt;[] userProperties, &lt;span style="color: #2b91af;"&gt;ClassDeclaration&lt;/span&gt; classDec, &lt;span style="color: #2b91af;"&gt;FieldDeclaration&lt;/span&gt; nativeMember)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; totalSize = sumSize(userProperties);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ConstructorDeclaration&lt;/span&gt; ctor = classDec.AddConstructor();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ctor.Body.Add(&lt;span style="color: #2b91af;"&gt;Stm&lt;/span&gt;.Snippet(&lt;span style="color: #a31515;"&gt;"unsafe{"&lt;/span&gt;));&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ctor.Body.AddAssign(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.This.Field(nativeMember),&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.Cast(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue;"&gt;byte&lt;/span&gt;*), &lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.Type(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;Marshal&lt;/span&gt;)).Method(&lt;span style="color: #a31515;"&gt;"AllocHGlobal"&lt;/span&gt;).Invoke(&lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.Prim(totalSize)).&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Method(&lt;span style="color: #a31515;"&gt;"ToPointer"&lt;/span&gt;).Invoke()));&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ctor.Body.Add(&lt;span style="color: #2b91af;"&gt;Stm&lt;/span&gt;.Snippet(&lt;span style="color: #a31515;"&gt;"}"&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; addDeleteMethod(&lt;span style="color: #2b91af;"&gt;ClassDeclaration&lt;/span&gt; classDec, &lt;span style="color: #2b91af;"&gt;FieldDeclaration&lt;/span&gt; nativeMember)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;MethodDeclaration&lt;/span&gt; disposeMethod = classDec.AddMethod(&lt;span style="color: #a31515;"&gt;"Delete"&lt;/span&gt;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disposeMethod.Attributes = &lt;span style="color: #2b91af;"&gt;MemberAttributes&lt;/span&gt;.Final | &lt;span style="color: #2b91af;"&gt;MemberAttributes&lt;/span&gt;.Public;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disposeMethod.Body.Add(&lt;span style="color: #2b91af;"&gt;Stm&lt;/span&gt;.Snippet(&lt;span style="color: #a31515;"&gt;"unsafe{"&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disposeMethod.Body.Add(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.Type(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;Marshal&lt;/span&gt;)).Method(&lt;span style="color: #a31515;"&gt;"FreeHGlobal"&lt;/span&gt;).Invoke(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.New(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;IntPtr&lt;/span&gt;), &lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.Cast(&lt;span style="color: #a31515;"&gt;"void*"&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.This.Field(nativeMember)))));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disposeMethod.Body.AddAssign(&lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.This.Field(nativeMember),&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.Cast(&lt;span style="color: #a31515;"&gt;"byte*"&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.Type(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;IntPtr&lt;/span&gt;)).Field(&lt;span style="color: #a31515;"&gt;"Zero"&lt;/span&gt;)));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disposeMethod.Body.Add(&lt;span style="color: #2b91af;"&gt;Stm&lt;/span&gt;.Snippet(&lt;span style="color: #a31515;"&gt;"}"&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; addProperties(&lt;span style="color: #2b91af;"&gt;PropertyInfo&lt;/span&gt;[] userProperties, &lt;span style="color: #2b91af;"&gt;ClassDeclaration&lt;/span&gt; classDec, &lt;span style="color: #2b91af;"&gt;FieldDeclaration&lt;/span&gt; nativeMember)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; offset = 0;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;PropertyInfo&lt;/span&gt; curProperty &lt;span style="color: blue;"&gt;in&lt;/span&gt; userProperties)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; propType = curProperty.PropertyType;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; propSize = &lt;span style="color: #2b91af;"&gt;Marshal&lt;/span&gt;.SizeOf(propType);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;PropertyDeclaration&lt;/span&gt; propDec = classDec.AddProperty(propType, curProperty.Name);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propDec.Attributes = &lt;span style="color: #2b91af;"&gt;MemberAttributes&lt;/span&gt;.Final | &lt;span style="color: #2b91af;"&gt;MemberAttributes&lt;/span&gt;.Public;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt; (curProperty.CanRead)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;addGetter(nativeMember, offset, propType, propDec);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt; (curProperty.CanWrite)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;addSetter(nativeMember, offset, propType, propDec);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;offset += propSize;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; addSetter(&lt;span style="color: #2b91af;"&gt;FieldDeclaration&lt;/span&gt; nativeMember, &lt;span style="color: blue;"&gt;int&lt;/span&gt; offset, &lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; propType, &lt;span style="color: #2b91af;"&gt;PropertyDeclaration&lt;/span&gt; propDec)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propDec.Set.Add(&lt;span style="color: #2b91af;"&gt;Stm&lt;/span&gt;.Snippet(&lt;span style="color: #a31515;"&gt;"unsafe{"&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propDec.Set.Add(&lt;span style="color: #2b91af;"&gt;Stm&lt;/span&gt;.Snippet(&lt;span style="color: #a31515;"&gt;"*("&lt;/span&gt; + propType.Name + &lt;span style="color: #a31515;"&gt;"*)&amp;amp;"&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propDec.Set.AddAssign(&lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.This.Field(nativeMember).Item(offset), &lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.Value);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propDec.Set.Add(&lt;span style="color: #2b91af;"&gt;Stm&lt;/span&gt;.Snippet(&lt;span style="color: #a31515;"&gt;"}"&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; addGetter(&lt;span style="color: #2b91af;"&gt;FieldDeclaration&lt;/span&gt; nativeMember, &lt;span style="color: blue;"&gt;int&lt;/span&gt; offset, &lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; propType, &lt;span style="color: #2b91af;"&gt;PropertyDeclaration&lt;/span&gt; propDec)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propDec.Get.Add(&lt;span style="color: #2b91af;"&gt;Stm&lt;/span&gt;.Snippet(&lt;span style="color: #a31515;"&gt;"unsafe{"&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propDec.Get.Add(&lt;span style="color: #2b91af;"&gt;Stm&lt;/span&gt;.Snippet(&lt;span style="color: #a31515;"&gt;"return *("&lt;/span&gt; + propType.Name + &lt;span style="color: #a31515;"&gt;"*)&amp;amp;"&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propDec.Get.Add(&lt;span style="color: #2b91af;"&gt;Expr&lt;/span&gt;.This.Field(nativeMember).Item(offset));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propDec.Get.Add(&lt;span style="color: #2b91af;"&gt;Stm&lt;/span&gt;.Snippet(&lt;span style="color: #a31515;"&gt;"}"&lt;/span&gt;));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; generateCode(&lt;span style="color: #2b91af;"&gt;NamespaceDeclaration&lt;/span&gt; ns)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt; sourceFile = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; outDir = &lt;span style="color: #a31515;"&gt;"ManalocAutoGenerated"&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt; (!&lt;span style="color: #2b91af;"&gt;Directory&lt;/span&gt;.Exists(outDir))&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Directory&lt;/span&gt;.CreateDirectory(outDir);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Refly.CodeDom.&lt;span style="color: #2b91af;"&gt;CodeGenerator&lt;/span&gt; generator = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Refly.CodeDom.&lt;span style="color: #2b91af;"&gt;CodeGenerator&lt;/span&gt;();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;generator.CreateFolders = &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;generator.FileCreated += (&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;StringEventArgs&lt;/span&gt; args) =&amp;gt; { sourceFile = args.Value; };&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;generator.GenerateCode(outDir, ns);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt; (sourceFile == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;throw&lt;/span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"Faliled to generate source file"&lt;/span&gt;);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt; sourceFile;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Assembly&lt;/span&gt; compile(&lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; userType, &lt;span style="color: blue;"&gt;string&lt;/span&gt; sourceFile)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CompilerParameters&lt;/span&gt; compilerParams = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;CompilerParameters&lt;/span&gt;();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;compilerParams.CompilerOptions = &lt;span style="color: #a31515;"&gt;"/unsafe /optimize"&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;compilerParams.ReferencedAssemblies.Add(userType.Assembly.Location);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CompilerResults&lt;/span&gt; result =&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Refly.CodeDom.&lt;span style="color: #2b91af;"&gt;CodeGenerator&lt;/span&gt;.CsProvider.CompileAssemblyFromFile(compilerParams, &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;[] { sourceFile });&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Assembly&lt;/span&gt; compiledAssembly = result.CompiledAssembly;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt; compiledAssembly;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; sumSize(&lt;span style="color: #2b91af;"&gt;PropertyInfo&lt;/span&gt;[] userProperties)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; size = 0;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;PropertyInfo&lt;/span&gt; curProperty &lt;span style="color: blue;"&gt;in&lt;/span&gt; userProperties)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;size += &lt;span style="color: #2b91af;"&gt;Marshal&lt;/span&gt;.SizeOf(curProperty.PropertyType);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt; size;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IManalocGeneratedType&lt;/span&gt;&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete();&lt;br /&gt;
}&lt;/t&gt;&lt;/t&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-6602582827722806714?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=giucJBQXwPc:59Dp0ekrqIM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=giucJBQXwPc:59Dp0ekrqIM:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=giucJBQXwPc:59Dp0ekrqIM:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/giucJBQXwPc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/giucJBQXwPc/writing-manual-memory-manager-in-c.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.liranchen.com/2011/08/writing-manual-memory-manager-in-c.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-7389850016471957427</guid><pubDate>Mon, 25 Oct 2010 19:50:00 +0000</pubDate><atom:updated>2010-10-25T23:14:25.515+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Debugging</category><title>The Case of NUnit Hanging During Startup</title><description>Recently, a coworker of mine encountered a strange behavior in &lt;a href="http://www.nunit.org/"&gt;NUnit&lt;/a&gt;. Every time he'd open NUnit's graphical interface, it would freeze and stop responding. Even though a test haven't even started to execute, the application simply froze, leaving the user only to wait patiently and stare helplessly at the screen.&lt;br /&gt;
&lt;br /&gt;
In order to investigate the issue, I've fired up &lt;a href="http://en.wikipedia.org/wiki/WinDbg"&gt;Windbg&lt;/a&gt; and attached it to the hanged process.&lt;br /&gt;
After loading SOS, the &lt;i&gt;!clrstack&lt;/i&gt; command was issued so I could get a better understanding to what was keeping the application busy. Since NUnit's graphical interface stopped from responding, one could already assume that the main thread stopped handling messages from the message pump for some reason.&lt;br /&gt;
Reviewing the command's output confirmed that assumption. (The outputs in the post were edited for brevity).&lt;br /&gt;
&lt;pre&gt;&lt;b&gt;0:007&amp;gt; ~*e!clrstack&lt;/b&gt;
PDB symbol for mscorwks.dll not loaded
&lt;span style="color: blue;"&gt;OS Thread Id: 0xe4 (0)&lt;/span&gt;
ESP       EIP     
0012ee44 7c90e4f4 &lt;span style="background-color: #fff2cc; color: red;"&gt;Win32Native.GetFileAttributesEx&lt;/span&gt;&lt;span style="background-color: #fff2cc;"&gt;&lt;span style="background-color: #fff2cc;"&gt;(...)&lt;/span&gt;&lt;/span&gt;
0012ee58 792e03f6 System.IO.File.FillAttributeInfo(...)
0012eeec 7927ff71 System.IO.File.InternalExists(System.String)
0012ef20 792e96a6 System.IO.File.Exists(System.String)
0012ef4c 03a214c3 NUnit.Util.RecentFileEntry.get_Exists()
0012ef54 00e0fb7a NUnit.Gui.NUnitForm.NUnitForm_Load(Object, EventArgs)
0012ef8c 7b1d4665 System.Windows.Forms.Form.OnLoad(EventArgs)
0012efc0 7b1d4105 System.Windows.Forms.Form.OnCreateControl()
0012efcc 7b1c6d11 System.Windows.Forms.Control.CreateControl(Boolean)
0012f008 7b1c6b14 System.Windows.Forms.Control.CreateControl()
0012f020 7b1d2fc8 System.Windows.Forms.Control.WmShowWindow(Forms.Message)
0012f05c 7b1c8906 System.Windows.Forms.Control.WndProc(Forms.Message)
0012f060 7b1d1d6a [InlinedCallFrame: 0012f060] 
...
0012f418 7b195911 System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
0012f42c 00e00643 NUnit.Gui.AppEntry.Main(System.String[])
0012f464 00e00076 NUnit.Gui.Class1.Main(System.String[])
0012f688 79e71b4c [GCFrame: 0012f688] 
&lt;span style="color: blue;"&gt;OS Thread Id: 0x1464 (1)&lt;/span&gt;
Unable to walk the managed stack. The current thread is likely not a 
managed thread. You can run !threads to get a list of managed threads in
the process
&lt;span style="color: blue;"&gt;OS Thread Id: 0xdac (2)&lt;/span&gt;
Failed to start stack walk: 80004005&amp;nbsp;&lt;/pre&gt;Evidently, NUnit is performing a synchronous IO operation on its main thread, which for some reason seems to refuse to complete. It happens to be a good example to why it's considered to be a bad practice to perform "heavy-weight lifting" on the main UI thread, or synchronous IO in general.&lt;br /&gt;
&lt;br /&gt;
After obtaining this piece of information, it was required to understand why the operation doesn't complete. Perhaps the path of the requested file could shed some light on the observed behavior.&lt;br /&gt;
Since the path is passed across several managed methods before entering the Win32 API, I've attempted to dump the values of the parameters in the managed call stack using the &lt;i&gt;!clrstack -p&lt;/i&gt; command.&lt;br /&gt;
&lt;pre&gt;&lt;b&gt;0:000&amp;gt; !clrstack -p&lt;/b&gt;
OS Thread Id: 0xe4 (0)
ESP       EIP     
0012ee44 7c90e4f4 Win32Native.GetFileAttributesEx(...)
0012ee58 792e03f6 File.FillAttributeInfo(System.String, ...)
    PARAMETERS:
        path = no data
        data = 0x0012eeec
        tryagain = no data
        returnErrorOnNotFound = 0x00000001

0012eeec 7927ff71 System.IO.File.InternalExists(System.String)
    PARAMETERS:
        path = no data

0012ef20 792e96a6 System.IO.File.Exists(System.String)
    PARAMETERS:
        path = no data

0012ef4c 03a214c3 NUnit.Util.RecentFileEntry.get_Exists()
    PARAMETERS:
        this = no data

0012ef54 00e0fb7a NUnit.Gui.NUnitForm.NUnitForm_Load(Object, EventArgs)
    PARAMETERS:
        this = 0x013349ec
        sender = no data
        e = no data
&lt;/pre&gt;Unfortunately, SOS wasn't able to retrieve the address of the managed string (thus, stating &lt;i&gt;no data&lt;/i&gt;). It's likely that either the variable was stored in a register that was already overwritten by some later executing code, or perhaps we're just witnessing some of SOS's good old "&lt;a href="http://blog.liranchen.com/2010/09/clrstack-p-isnt-always-reliable.html"&gt;buggy nature&lt;/a&gt;".&lt;br /&gt;
&lt;br /&gt;
At this point, I take a small step into the world of native debugging and issue the &lt;i&gt;kb&lt;/i&gt; command, to display the native call stack, including the first 3 parameters passed to each method.&lt;br /&gt;
&lt;pre&gt;&lt;b&gt;0:000&amp;gt; kb&lt;/b&gt;
ChildEBP RetAddr  Args to Child              
0012ed84 7c90d79c 7c8111ff 0012eddc 0012eda4 ntdll!KiFastSystemCallRet
0012ed88 7c8111ff 0012eddc 0012eda4 00151bb8 ntdll!ZwQueryFullAttributesFile+0xc
&lt;span style="background-color: #fff2cc;"&gt;0012ee08 0097a310 &lt;span style="color: red;"&gt;0139272c&lt;/span&gt; 00000000 0012eeec &lt;span style="color: red;"&gt;KERNEL32!GetFileAttributesExW&lt;/span&gt;+0x84&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;WARNING: Frame IP not in any known module. Following frames may be wrong.
0012ee2c 792e03f6 0012ee5c 00000000 01392764 &lt;unloaded_ion.dll&gt;+0x97a30f
0012eedc 7927ff71 00000001 00000000 00000000 mscorlib_ni+0x2203f6
0012ef18 792e96a6 00000000 00000000 00000000 mscorlib_ni+0x1bff71
0012ef80 7b1d4665 01336c40 0134f25c 00000000 mscorlib_ni+0x2296a6
0012efb8 7b1d4105 00000004 0012f000 7b1c6d11 System_Windows_Forms_ni+0x204665
0012efc4 7b1c6d11 0133797c 013349ec 00000001 System_Windows_Forms_ni+0x204105
0012f000 7b1c6b14 00000000 00000000 013349ec System_Windows_Forms_ni+0x1f6d11
0012f018 7b1d2fc8 013349ec 00000000 00000000 System_Windows_Forms_ni+0x1f6b14
0012f054 7b1c8906 3cc1af52 79e7a6b8 0012f2d4 System_Windows_Forms_ni+0x202fc8
0012f0ac 7b1d1d6a 013349ec 0012f0c0 7b1d1d20 System_Windows_Forms_ni+0x1f8906
0012f0b8 7b1d1d20 0012f0d0 7b1d2f11 0012f10c System_Windows_Forms_ni+0x201d6a
0012f0c0 7b1d2f11 0012f10c 013349ec 0012f0e4 System_Windows_Forms_ni+0x201d20
0012f0d0 7b1d1af4 00e111ec 0012f10c 01335448 System_Windows_Forms_ni+0x202f11
0012f0e4 7b1c8640 0012f100 7b1c85c1 00000000 System_Windows_Forms_ni+0x201af4
0012f0ec 7b1c85c1 00000000 01335448 0012f130 System_Windows_Forms_ni+0x1f8640
0012f100 7b1c849a 01335448 00291106 00000018 System_Windows_Forms_ni+0x1f85c1
0012f164 7e418734 00291106 00000018 00000001 System_Windows_Forms_ni+0x1f849a&lt;/unloaded_ion.dll&gt;&lt;/pre&gt;As you can see, the thread eventually made its way into the kernel, calling &lt;a href="http://msdn.microsoft.com/en-us/library/aa364946%28VS.85%29.aspx"&gt;GetFileAttributesExW&lt;/a&gt;.&lt;br /&gt;
Knowing that the first parameter that gets passed to the method (&lt;i&gt;lpFileName&lt;/i&gt;) represent the name of the file or directory in hand, we are only left to dump its value using the &lt;i&gt;du&lt;/i&gt; command.&lt;br /&gt;
&lt;pre&gt;&lt;b&gt;0:000&amp;gt; du 0139272c&lt;/b&gt;
0139272c  "\\123.123.123.123\foo.dll"&lt;/pre&gt;With that, the culprit is found. Apparently, some time ago a UT assembly was loaded into NUnit from a certain IP address that is no longer available in the network, causing the thread to stall indefinitely.&lt;br /&gt;
&lt;br /&gt;
In order to fix the issue, the problematic path was needed to be removed from the &lt;i&gt;RecentProjects&lt;/i&gt; list in NUnit's settings file (located at &lt;i&gt;%AppData%\NUnit\NUnitSettings.xml&lt;/i&gt;).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-7389850016471957427?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=PWtgPi18c4U:ByeXRV_zdhw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=PWtgPi18c4U:ByeXRV_zdhw:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=PWtgPi18c4U:ByeXRV_zdhw:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/PWtgPi18c4U" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/PWtgPi18c4U/case-nunit-hanging-during-startup.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>3</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/10/case-nunit-hanging-during-startup.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-801630672111449274</guid><pubDate>Mon, 11 Oct 2010 18:44:00 +0000</pubDate><atom:updated>2010-10-11T20:44:43.626+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Performance</category><category domain="http://www.blogger.com/atom/ns#">Multithreading</category><title>Hot/Cold Data Separation Considered Harmful in Multithreaded Environments</title><description>When optimizing your code for performance, a quite important step in the way is to check that your application correctly utilizes its processor's cache. Of course, the magnitude of importance here is solely depended on your own personal scenario, but in general, it's always a good idea to keep your cache usage patterns in mind when attempting to resolve a performance bottleneck or perhaps for when you're just looking for possible ways to enhance you're application's performance.&lt;br /&gt;
&lt;br /&gt;
Due to the relative high cost of accessing the main memory, modern processors make use of a data and an instruction cache in an attempt to lower the latency involved in accessing the main memory.&lt;br /&gt;
A processor may choose to keep in its cache the values of frequently accessed memory addresses, or perhaps prefetch the values of adjacent memory addresses for possible future usage.&lt;br /&gt;
When the processor attempts to access a memory address, it first check to see whether it's already exist in one of its caches (L1, L2, L3 etc.). If the address isn't found, then we have a "cache miss" and the processor we'll have to perform a round-trip to the main memory in order to obtain the required data. Wasting valuable CPU cycles on the way.&lt;br /&gt;
&lt;br /&gt;
Not too long ago, one of the most popular guidelines in designing cache-aware data structures was to split them into "hot" and "cold" parts. This kind of separation makes sense since it could lead to a more efficient utilization of the cache. Frequently used fields (the hot part) are grouped together in the same cache lines, instead of being mixed with infrequently used fields (the cold part). This way, the cache is able to contain more hot data, and cache lines aren't wasted to hold cold data.&lt;br /&gt;
For those of you who are interested, the subject is covered in greater detail in the article &lt;a href="http://www.ics.uci.edu/%7Efranz/Site/pubs-pdf/ICS-TR-98-34.pdf"&gt;Splitting Data Objects to Increase Cache Utilization&lt;/a&gt; by Michael Franz and Thomas Kistler.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://i47.tinypic.com/2njatr4.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://i47.tinypic.com/2njatr4.gif" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: right;"&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&amp;nbsp;&lt;span style="font-size: xx-small;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;b&gt;Red&lt;/b&gt;: Hot data, &lt;b&gt;Green&lt;/b&gt;: Cold data&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
While following this guideline could lead to a more efficient cache utilization, it won't necessarily lead to a performance improvement in a multithreaded environment. In fact, it's more likely that you'll face a performance degradation than gaining any performance improvement at all.&lt;br /&gt;
The problem in hand is that highly utilized caches could easily send your valuable CPU cycles &lt;a href="http://www.drdobbs.com/go-parallel/article/showArticle.jhtml;jsessionid=LB4CFRLWCMZV5QE1GHPCKH4ATMY32JVN?articleID=217500206"&gt;down the drain&lt;/a&gt;, due to the effects of &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc872851.aspx"&gt;false sharing&lt;/a&gt;. Once a single cache line holds several frequently written-to fields, the chance it will get invalidated by another processor gets greater. Sharing a single cache line with multiple frequently modified fields could easily have a negative effect on your cache's locality.&lt;br /&gt;
In multithreaded environments, one could benefit from sparsely allocating frequently used fields in the cache. There's an obvious trade-off between cache utilization (memory usage) and locality (performance). While the cache will contain less hot data (which could result in more round-trips to the main memory), it would benefit from better locality, hence better scalability across multiple processors that might attempt to modify the same object simultaneously.&lt;br /&gt;
&lt;br /&gt;
When designing a cache-aware data structure, you don't necessarily have to order your fields in such way that a hot field will always get followed by a cold field. Instead, "artificial" padding could be used to fill the excessive space left in the cache line which holds the structure. In .Net, decorating the type with an &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.aspx"&gt;StructLayout(LayoutKind.Explicit)&lt;/a&gt; attribute and assigning each field with its appropriate &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.fieldoffsetattribute.aspx"&gt;offset&lt;/a&gt; would do the job.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-801630672111449274?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=ArbIqsY4kHE:uxpqU_lolMQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=ArbIqsY4kHE:uxpqU_lolMQ:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=ArbIqsY4kHE:uxpqU_lolMQ:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/ArbIqsY4kHE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/ArbIqsY4kHE/hotcold-data-separation-considered.html</link><author>noreply@blogger.com (Liran Chen)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://i47.tinypic.com/2njatr4_th.gif" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/10/hotcold-data-separation-considered.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-3043007779553423337</guid><pubDate>Fri, 24 Sep 2010 14:22:00 +0000</pubDate><atom:updated>2010-09-25T20:26:51.435+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Performance</category><category domain="http://www.blogger.com/atom/ns#">Multithreading</category><title>Writing a Semi-Local Object Pool</title><description>Using object pooling in managed environments can usually benefit us in two ways:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Reducing the amount of time required to create "heavyweight" objects (that might involve executing some time consuming tasks).&lt;/li&gt;
&lt;li&gt;Reducing the amount and rate of dynamic memory allocations. Thus, reducing the GC latency in future collections.&lt;/li&gt;
&lt;/ul&gt;Nevertheless, it's important to remember that under certain scenarios, using object pools might actually have a negative impact on your application's performance. Since in managed environments (e.g, CLR, JVM), dynamic memory allocations is considerably fast, using object pools in favor of "lightwight" objects could cause somewhat of an unnecessary overhead during the object's allocation process. &lt;a href="http://www.ibm.com/developerworks/java/library/j-jtp09275.html?ca=dgr-jw22JavaUrbanLegends"&gt;Brian Goetz&lt;/a&gt; summarized this issue: &lt;br /&gt;
&lt;blockquote style="background-color: #f3f3f3; font-family: inherit;"&gt;&lt;i&gt;Allocation in JVMs was  not always so fast -- early JVMs indeed had poor allocation and garbage collection performance, which is almost certainly where this myth got started.  In the very early days, we saw a lot of "allocation is slow" advice -- because it was, along with everything else in early JVMs -- and performance gurus advocated various tricks to avoid allocation, such as object pooling.  (Public service announcement: &lt;b&gt;Object pooling is now a serious performance  loss for all but the most heavyweight of objects, and even then it is tricky to get right without introducing concurrency bottlenecks.&lt;/b&gt;)&lt;/i&gt;&lt;/blockquote&gt;A common, simple pattern for implementing an object pool is to create a single pool instance that is shared across all of the application. To achieve thread-safety, you would usually find a single, global lock around the &lt;i&gt;Allocate&lt;/i&gt; and &lt;i&gt;Free&lt;/i&gt; methods.&lt;br /&gt;
It's obvious that this type of design could introduce major concurrency bottlenecks. The more objects we'll attempt to pool, the greater the chance that we'll have threads attempting to acquire the pool's lock. And since we only maintain a single, global pool, contentions around that lock are bound to appear. Effectively ruining our application's scalability.&lt;br /&gt;
To demonstrate the issue, I've written a small benchmark that uses all of the available processors to allocate and free a constant number of objects (each thread gets an equal amount of objects to pool). Logically speaking, the more processors we use, the faster we should be able to finish allocating and freeing the constant number of objects. However, as the results show, we're actually experiencing a &lt;a href="http://en.wikipedia.org/wiki/Parallel_slowdown"&gt;slowdown&lt;/a&gt; that gets worse as soon as we add more and more processors.&lt;br /&gt;
The results aren't surprising since they can be easily explained due to the massive amount of contentions we're experiencing around our single lock. &lt;span style="font-size: xx-small;"&gt;(The time axis in the chart is expressed in milliseconds)&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://i47.tinypic.com/nx5h7s.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://i47.tinypic.com/nx5h7s.png" /&gt;&lt;/a&gt;&lt;/div&gt;The first implementation of the pool that was being used in the test:&lt;br /&gt;
&lt;span style="font-size: xx-small;"&gt;(Mind you that the code samples in this post are purely meant to demonstrate the conceptual differences between the pools).&lt;/span&gt;&lt;br /&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // holds a dictionary that makes a pool-per-type corelation&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SimpleMainPool
    {
        &lt;span class="kwrd"&gt;private&lt;/span&gt; Dictionary&amp;lt;Type, ISubPool&amp;gt; m_main;

        &lt;span class="rem"&gt;// to make things simpler, the dictionary isn't modified&lt;/span&gt;
        &lt;span class="rem"&gt;// after the first initialization&lt;/span&gt;
        &lt;span class="kwrd"&gt;public&lt;/span&gt; SimpleMainPool(Type[] pooledTypes)
        {
            m_main = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;Type, ISubPool&amp;gt;();

            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Type curType &lt;span class="kwrd"&gt;in&lt;/span&gt; pooledTypes)
                m_main.Add(curType, &lt;span class="kwrd"&gt;new&lt;/span&gt; SemiLocalPool(curType));
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; Allocate(Type type)
        {
            ISubPool sub = m_main[type];

            &lt;span class="kwrd"&gt;object&lt;/span&gt; pooledObj = sub.Allocate();
            &lt;span class="kwrd"&gt;return&lt;/span&gt; pooledObj;
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Free(&lt;span class="kwrd"&gt;object&lt;/span&gt; obj)
        {
            ISubPool sub = m_main[obj.GetType()];
            sub.Free(obj);
        }
    }

    &lt;span class="rem"&gt;// our simple thread-safe pool&lt;/span&gt;
    &lt;span class="kwrd"&gt;class&lt;/span&gt; SimplePool : ISubPool
    {
        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; PRIME = 50;

        &lt;span class="kwrd"&gt;private&lt;/span&gt; Type m_type;

        &lt;span class="kwrd"&gt;private&lt;/span&gt; Stack&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; m_sharedPool;

        &lt;span class="kwrd"&gt;public&lt;/span&gt; SimplePool(Type type)
        {
            m_sharedPool = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stack&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;(PRIME);
            m_type = type;

            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; PRIME; i++)
            {
                &lt;span class="kwrd"&gt;object&lt;/span&gt; sharedObj = Activator.CreateInstance(m_type);
                m_sharedPool.Push(sharedObj);
            }
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; Allocate()
        {
            &lt;span class="kwrd"&gt;lock&lt;/span&gt; (m_sharedPool)
            {
                &lt;span class="kwrd"&gt;if&lt;/span&gt; (m_sharedPool.Count == 0)
                {
                    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; PRIME; i++)
                    {
                        &lt;span class="kwrd"&gt;object&lt;/span&gt; newAlloc = Activator.CreateInstance(m_type);
                        m_sharedPool.Push(newAlloc);
                    }
                }

                &lt;span class="kwrd"&gt;object&lt;/span&gt; fromLocal = m_sharedPool.Pop();
                &lt;span class="kwrd"&gt;return&lt;/span&gt; fromLocal;
            }
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Free(&lt;span class="kwrd"&gt;object&lt;/span&gt; obj)
        {
            &lt;span class="kwrd"&gt;lock&lt;/span&gt; (m_sharedPool)
            {
                m_sharedPool.Push(obj);
            }
        }
    }

    &lt;span class="kwrd"&gt;interface&lt;/span&gt; ISubPool
    {
        &lt;span class="kwrd"&gt;object&lt;/span&gt; Allocate();
        &lt;span class="kwrd"&gt;void&lt;/span&gt; Free(&lt;span class="kwrd"&gt;object&lt;/span&gt; obj);
    }&amp;nbsp; &lt;/pre&gt;As in all things related to concurrency, if you don't have &lt;i&gt;locality&lt;/i&gt;, then you've got &lt;i&gt;sharing&lt;/i&gt;, and once you have&lt;i&gt; sharing&lt;/i&gt;, you will probably end up with contentions that are bound to harm your application's performance, wasting valuable CPU cycles.&lt;br /&gt;
So if we'd like to improve our scalability, then our goal is clear: reducing the amount of shared data. For example, if pools wouldn't be shared across different threads, then we wouldn't had to worry about synchronizing them and we could avoid the involved contentions altogether. A simple way to achieve this, is to use the &lt;a href="http://en.wikipedia.org/wiki/Thread-local_storage"&gt;TLS&lt;/a&gt; to allocate an independent pool for every thread. This way, on the one hand we'll achieve &lt;b&gt;perfect scalability&lt;/b&gt; due to the avoidance of state sharing, but on the other hand, this kind of implementation could lead to an &lt;b&gt;excessive memory usage&lt;/b&gt;. For instance, if a single instance of our pool (including all of its pre-allocated objects) weights about 10Mb, then on a machine with 16 processors, we could find ourselves dedicating no less then 160Mb in favor of our thread-local pools, even though its not likely that every single thread needs to use all the types of objects that we're allocated in its local pool.&lt;br /&gt;
For example, if we're parallelizing some algorithm using 3 threads, where &lt;i&gt;thread 1&lt;/i&gt; needs to use objects of type &lt;i&gt;A&lt;/i&gt; and &lt;i&gt;thread 2&lt;/i&gt; needs to use objects of type &lt;i&gt;B&lt;/i&gt; and &lt;i&gt;thread 3&lt;/i&gt; needs to use objects of type &lt;i&gt;C&lt;/i&gt;, then it makes no sense that every one of those threads will hold a pool that will contain objects of all three types.&lt;br /&gt;
&lt;br /&gt;
A possible solution for this problem is to use a &lt;i&gt;pool hierarchy&lt;/i&gt;, where every time a thread attempts to create an object, it will direct itself to its "closest" pool instance. If that pool doesn't contain available instances of the requested object, then it will continue to navigate up the hierarchy until it reaches a pool that holds available instances of the object. Once the thread finishes using the object, it will return it to a pool that is located "closer" to that thread, this way we are able to maintain a level of locality between a thread and its used objects.&lt;br /&gt;
Instead of getting confused with unclear and too complex hierarchies, I'll demonstrate the concept using a flat hierarchy that offers a single "global" pool that is shared across all threads, and another local pool for every thread.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://i50.tinypic.com/4ktls1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://i50.tinypic.com/4ktls1.png" /&gt;&lt;/a&gt;&lt;/div&gt;Basically, the idea is that the only place synchronization is involved is in the shared pool. So in the optimal scenario, each local pool will eventually hold only the amount of objects required  to keep the thread from accessing the shared pool.&lt;br /&gt;
Every time a thread needs to create an object, it will first check its local pool. Since this pool only serves the requesting thread, we won't have to deal with synchronization here. Only in case where we've ran out of objects, we'll move on to the shared pool and transfer &lt;i&gt;N&lt;/i&gt; more instances of the requested object to the local pool. It could be wise to transfer more objects than the thread initially asked for in order to avoid future accesses to the shared pool. Also, in order to cap the amount of memory we'd like to dedicate for each thread, we could decide that each local pool can hold a maximum of &lt;i&gt;X&lt;/i&gt; objects. Once we've exceeded that number, every time a thread will want to free an object, it will return it to the shared pool instead of its local pool (of course, this could cause some contentions, depending on the implementation detail [e.g. the pool may buffer object returns]. But its entirely up to the developer to perform this kind of fine-tuning [memory usage vs. scalability]).&lt;br /&gt;
&lt;br /&gt;
To demonstrate to concept, I've came up with this simplistic pool implementation:&lt;br /&gt;
&lt;span id="ctl00_ContentPlaceHolder1_output"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span id="ctl00_ContentPlaceHolder1_output"&gt;&lt;span class="kwrd"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; class&lt;/span&gt; SemiLocalPool : ISubPool
    {
        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; SHARED_PRIME = 50;

        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; LOCAL_PRIME = 20;
        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; LOCAL_MAX = 1000;

        [ThreadStatic]
        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Stack&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; t_localPool;

        &lt;span class="kwrd"&gt;private&lt;/span&gt; Type m_type;
        &lt;span class="kwrd"&gt;private&lt;/span&gt; Stack&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; m_sharedPool;

        &lt;span class="kwrd"&gt;public&lt;/span&gt; SemiLocalPool(Type type)
        {
            m_sharedPool = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stack&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;(SHARED_PRIME);

            m_type = type;

            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; SHARED_PRIME; i++)
            {
                &lt;span class="kwrd"&gt;object&lt;/span&gt; sharedObj = Activator.CreateInstance(m_type);
                m_sharedPool.Push(sharedObj);
            }
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Init()
        {
            t_localPool = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stack&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;(LOCAL_PRIME);
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; Allocate()
        {
            &lt;span class="rem"&gt;// first, try to allocate from the local pool&lt;/span&gt;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (t_localPool.Count &amp;gt; 0)
            {
                &lt;span class="kwrd"&gt;object&lt;/span&gt; localObj = t_localPool.Pop();
                &lt;span class="kwrd"&gt;return&lt;/span&gt; localObj;
            }

            &lt;span class="kwrd"&gt;int&lt;/span&gt; allocated = 0;

            &lt;span class="kwrd"&gt;lock&lt;/span&gt; (m_sharedPool)
            {
                &lt;span class="rem"&gt;// pass objects from shared to local pool&lt;/span&gt;
                &lt;span class="kwrd"&gt;for&lt;/span&gt; (; m_sharedPool.Count &amp;gt; 0 &amp;amp;&amp;amp; allocated &amp;lt; LOCAL_PRIME - 1; allocated++)
                {
                    &lt;span class="kwrd"&gt;object&lt;/span&gt; sharedObj = m_sharedPool.Pop();
                    t_localPool.Push(sharedObj);
                }

                &lt;span class="rem"&gt;// prime share pool&lt;/span&gt;
                &lt;span class="kwrd"&gt;if&lt;/span&gt; (m_sharedPool.Count == 0)
                {
                    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; SHARED_PRIME; i++)
                    {
                        &lt;span class="rem"&gt;// bad practice: holding the lock while executing external code&lt;/span&gt;
                        &lt;span class="kwrd"&gt;object&lt;/span&gt; sharedObj = Activator.CreateInstance(m_type);

                        m_sharedPool.Push(sharedObj);
                    }
                }
            }

            &lt;span class="rem"&gt;// if the shared pool didn't contain enough elements, prime the remaining items&lt;/span&gt;
            &lt;span class="kwrd"&gt;for&lt;/span&gt; (; allocated &amp;lt; LOCAL_PRIME - 1; allocated++)
            {
                &lt;span class="kwrd"&gt;object&lt;/span&gt; newAlloc = Activator.CreateInstance(m_type);
                t_localPool.Push(newAlloc);
            }

            &lt;span class="kwrd"&gt;object&lt;/span&gt; fromLocal = Activator.CreateInstance(m_type);
            &lt;span class="kwrd"&gt;return&lt;/span&gt; fromLocal;
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Free(&lt;span class="kwrd"&gt;object&lt;/span&gt; obj)
        {
            &lt;span class="rem"&gt;// first return to local pool&lt;/span&gt;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (t_localPool.Count &amp;lt; LOCAL_MAX)
            {
                t_localPool.Push(obj);
                &lt;span class="kwrd"&gt;return&lt;/span&gt;;
            }

            &lt;span class="rem"&gt;// only after reaching LOCAL_MAX push back to the shared pool&lt;/span&gt;
            &lt;span class="kwrd"&gt;lock&lt;/span&gt; (m_sharedPool)
            {
                m_sharedPool.Push(obj);
            }
        }
    }
&lt;/span&gt;&lt;/pre&gt;&lt;span id="ctl00_ContentPlaceHolder1_output"&gt;&lt;/span&gt;&lt;br /&gt;
The scalability difference between the two implementations is closely related to the thread's pool usage pattern and to the values given to &lt;i&gt;LOCAL_MAX, LOCAL_PRIME &lt;/i&gt;etc. If we reach a situation where there's always enough objects in the local pool, then we'll should enjoy perfect scalability.&lt;br /&gt;
For the purpose of the demonstration, here are the results of the previous benchmark, now using the new pool implementation (beside exceeding the predefined values at the beginning of the benchmark, the benchmark's behavior exhibits optimal usage pattern [accessing only the local pool after a while]).&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://i46.tinypic.com/s2vfc9.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://i46.tinypic.com/s2vfc9.png" /&gt;&lt;/a&gt;&lt;/div&gt;One problematic characteristic of this type of design is its reliance on &lt;i&gt;thread affinity&lt;/i&gt;. While in some scenarios it could actually benefit us, in others it could make the Semi-Local Pool irrelevant.&lt;br /&gt;
If every thread in our application is affinitized to certain section of the code (that allocates a constant set of objects), then using this design could be optimal since we dedicate each local pool to a managed thread. We actually assume that the thread will always attempt to allocate objects from a specific, constant set of objects.&lt;br /&gt;
However, if the threads doesn't comply with this assumption, then its only a matter of time until each local pool will hold the entire set of pooled objects in the applications (which will of course lead to high memory usage).&lt;br /&gt;
In order to improve our way of handling with such scenarios, we could decide to add a kind of additional hierarchy level, that will separate the&lt;i&gt; &lt;/i&gt;shared pools according to different sections in the code. Meaning, threads that are &lt;i&gt;currently&lt;/i&gt; executing code from a network module for example will access &lt;i&gt;Pool X&lt;/i&gt;, while threads that are currently executing some algorithm will access &lt;i&gt;Pool Y&lt;/i&gt;. This way we could achieve object locality not by relaying on thread affinity, but on "category affinity" (each section of the code uses a certain set of objects, relevant to it). When a thread will want to allocate an object, it will tell the pool which area in the code its currently executing, so it would receive the appropriate "category pool. It's likely that this pool already contains the same type of objects that will be requested by the current thread since they we're already allocated by other threads that previously executed the same code section.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://i47.tinypic.com/etfn2w.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://i47.tinypic.com/etfn2w.png" /&gt;&lt;/a&gt;&lt;/div&gt;And some code to illustrate the concept:&lt;br /&gt;
&lt;span id="ctl00_ContentPlaceHolder1_output"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span id="ctl00_ContentPlaceHolder1_output"&gt;&lt;span class="kwrd"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CategorizedMainPool
    {
        &lt;span class="kwrd"&gt;private&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, SimpleMainPool&amp;gt; m_main;

        &lt;span class="kwrd"&gt;public&lt;/span&gt; CategorizedMainPool(Tuple&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, Type[]&amp;gt;[] pooledCategories)
        {
            m_main = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, SimpleMainPool&amp;gt;();

            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Tuple&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, Type[]&amp;gt; curCategory &lt;span class="kwrd"&gt;in&lt;/span&gt; pooledCategories)
            {
                SimpleMainPool curSub = &lt;span class="kwrd"&gt;new&lt;/span&gt; SimpleMainPool(curCategory.Item2);
                m_main.Add(curCategory.Item1, curSub);
            }
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; Allocate(&lt;span class="kwrd"&gt;string&lt;/span&gt; category, Type type)
        {
            SimpleMainPool sub = m_main[category];

            &lt;span class="kwrd"&gt;object&lt;/span&gt; pooledObj = sub.Allocate(type);
            &lt;span class="kwrd"&gt;return&lt;/span&gt; pooledObj;
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Free(&lt;span class="kwrd"&gt;string&lt;/span&gt; category, &lt;span class="kwrd"&gt;object&lt;/span&gt; obj)
        {
            SimpleMainPool sub = m_main[catagory];
            sub.Free(obj);
        }
    }&lt;/span&gt;&lt;/pre&gt;&lt;span id="ctl00_ContentPlaceHolder1_output"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-3043007779553423337?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=_YhJ1IQeJR4:MF_R2Vi2TZE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=_YhJ1IQeJR4:MF_R2Vi2TZE:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=_YhJ1IQeJR4:MF_R2Vi2TZE:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/_YhJ1IQeJR4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/_YhJ1IQeJR4/writing-semi-local-object-pool.html</link><author>noreply@blogger.com (Liran Chen)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://i47.tinypic.com/nx5h7s_th.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/09/writing-semi-local-object-pool.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-5906177901421756154</guid><pubDate>Fri, 10 Sep 2010 14:35:00 +0000</pubDate><atom:updated>2010-10-06T20:24:53.772+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Debugging</category><category domain="http://www.blogger.com/atom/ns#">.Net Internals</category><title>"!CLRStack -p" Isn't Always Reliable</title><description>One of the most commonly used commands in &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc164138.aspx"&gt;SOS&lt;/a&gt; is &lt;i&gt;!CLRStack&lt;/i&gt;. When combined with the &lt;i&gt;-p&lt;/i&gt; switch, SOS will &lt;i&gt;attempt &lt;/i&gt;to display the values of the parameters that were passed to the functions in our managed call-stack. It's important to emphasis that SOS will only &lt;i&gt;attempt&lt;/i&gt; to display the correct values, since sometimes its just going to get it all wrong.&lt;br /&gt;
&lt;br /&gt;
In the case where SOS comes to the conclusion that it cannot retrieve the value of a parameter, the string &lt;i&gt;&lt;no data=""&gt;&lt;/no&gt;&lt;/i&gt; will be displayed. This happens at situations where SOS honestly can't track the value of the parameter by just looking at a specific &lt;a href="http://en.wikipedia.org/wiki/Call_stack#Structure"&gt;stack frame&lt;/a&gt;. For example, if we're using a &lt;a href="http://en.wikipedia.org/wiki/Calling_convention"&gt;calling convention&lt;/a&gt; such as &lt;a href="http://msdn.microsoft.com/en-us/library/6xa169sk.aspx"&gt;fast call&lt;/a&gt;, the first two parameters (starting from the left) that can be contained in a register, will be passed in the ECX and EDX registers instead of being pushed onto the stack. For member functions, the value of the &lt;i&gt;this&lt;/i&gt; pointer is usually passed in the ECX register.&lt;br /&gt;
This kind of behavior can lead into situations where the values of some of the function parameters may be missing since the registers we're already overridden by other functions that we're called down the stack.&lt;br /&gt;
As opposed to situations where SOS is able to come to the conclusion that it isn't sure what is the correct value of a parameter, every once in a while it's just going to get things wrong, and display incorrect values. This obviously could terribly mislead the person who attempts to debug the application.&lt;br /&gt;
The thing that might be the most concerning about this phenomena, is that it's not very hard to reproduce. Let's take the following application as an example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="font-family: consolas;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;Program
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] args)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Foo a = &lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;Foo();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a.Work(1, 2, 3, 4, 5);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;Foo
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [MethodImpl(MethodImplOptions.NoInlining)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; Work(&lt;span style="color: blue;"&gt;int&lt;/span&gt; x, &lt;span style="color: blue;"&gt;int&lt;/span&gt; y, &lt;span style="color: blue;"&gt;int&lt;/span&gt; z, &lt;span style="color: blue;"&gt;int&lt;/span&gt; k, &lt;span style="color: blue;"&gt;int&lt;/span&gt; p)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// break with debugger here&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/pre&gt;Now we'll run WinDbg, load SOS and see what the &lt;i&gt;!CLRStack&lt;/i&gt; command has to say about the parameters that we're passed to the &lt;i&gt;Work&lt;/i&gt; method.&lt;br /&gt;
&lt;i&gt;(Note: The output might slightly vary, depending on your version of SOS. The following debugging session was perform on version 4.0.30319.1).&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;b&gt;0:000&amp;gt; !clrstack -p&lt;/b&gt; &lt;br /&gt;
OS Thread Id:  0xfbc (0) &lt;br /&gt;
Child SP IP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Call Site &lt;br /&gt;
0012f3fc 030300b3  ConsoleApplication1.Foo.Work(Int32, Int32, Int32, Int32, Int32) [Program.cs  @ 24] &lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PARAMETERS: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this (&lt;clr reg=""&gt;) = &lt;span style="color: blue;"&gt;0x00b3c358&lt;/span&gt; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x (&lt;clr reg=""&gt;) = &lt;span style="color: blue;"&gt;0x00000001&lt;/span&gt; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y (0x0012f40c) = &lt;span style="color: red;"&gt;0x00000003&lt;/span&gt; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z (0x0012f408) = &lt;span style="color: red;"&gt;0x00000004&lt;/span&gt; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; k (0x0012f404) = &lt;span style="color: red;"&gt;0x00000005&lt;/span&gt; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p (0x0012f400) = &lt;span style="color: red;"&gt;0x03030092&lt;/span&gt; &lt;/clr&gt;&lt;/clr&gt;&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;0012f414 03030092  ConsoleApplication1.Program.Main(System.String[]) [Program.cs @ 16] &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;  PARAMETERS: &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; args = &lt;no data=""&gt; &lt;/no&gt;&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;0012f648 791421db [GCFrame: 0012f648]&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;b&gt;0:000&amp;gt; r ecx&lt;/b&gt;&amp;nbsp; &lt;span style="color: green;"&gt;//  holds "this" pointer&lt;/span&gt; &lt;br /&gt;
ecx=&lt;span style="color: blue;"&gt;00b3c358&lt;/span&gt;  &lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;b&gt;0:000&amp;gt; r edx&lt;/b&gt;&amp;nbsp; &lt;span style="color: green;"&gt;// holds "x" parameter&lt;/span&gt;&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;edx=&lt;span style="color: blue;"&gt;00000001&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div align="left" dir="ltr"&gt;If so, it isn't difficult to see that besides the &lt;i&gt;this&lt;/i&gt; pointer and the &lt;i&gt;x&lt;/i&gt; parameter (that we're passed as registers), SOS got all of the other parameters wrong. In fact, one may notice in a "shift" that was performed on the displayed values (&lt;i&gt;y&lt;/i&gt; got the value of &lt;i&gt;z&lt;/i&gt;, while &lt;i&gt;z &lt;/i&gt;got the value of &lt;i&gt;k&lt;/i&gt;, and so on...).&lt;/div&gt;&lt;div align="left" dir="ltr"&gt;In order to better understand what went on here, we'll print the memory between the relevant &lt;a href="http://en.wikipedia.org/wiki/Stack_%28data_structure%29#Hardware_stacks"&gt;stack pointers&lt;/a&gt; (in previous versions, this value was represented by the &lt;i&gt;ESP &lt;/i&gt;column instead of the &lt;i&gt;Child SP&lt;/i&gt; column).&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;b&gt;0:000&amp;gt; dp /c 1 0012f3fc&amp;nbsp; 0012f414&lt;/b&gt;&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="color: green;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;b&gt;&lt;/b&gt;0012f3fc&amp;nbsp; 0012f414 &lt;span style="color: green;"&gt;// EBP&lt;/span&gt;&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="color: green;"&gt;&lt;/span&gt;0012f400&amp;nbsp; 03030092 &lt;span style="color: green;"&gt;// Return Address&lt;/span&gt;&lt;br /&gt;
0012f404&amp;nbsp; &lt;span style="color: blue;"&gt;00000005&lt;/span&gt; &lt;span style="color: green;"&gt;// p&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="color: green;"&gt;&lt;/span&gt;0012f408&amp;nbsp; &lt;span style="color: blue;"&gt;00000004&lt;/span&gt; &lt;span style="color: green;"&gt;//  k&lt;/span&gt; &lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;0012f40c&amp;nbsp; &lt;span style="color: blue;"&gt;00000003&lt;/span&gt; &lt;span style="color: green;"&gt;// z&lt;/span&gt; &lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;0012f410&amp;nbsp; &lt;span style="color: blue;"&gt;00000002&lt;/span&gt; &lt;span style="color: green;"&gt;// y&lt;/span&gt; &lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;0012f414&amp;nbsp; 0012f424 &lt;span style="color: green;"&gt;//  EBP&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div align="left" dir="ltr" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;/div&gt;&lt;br /&gt;
Now, when we compare the stack-addresses of the parameters that SOS gave us, against the real addresses that we see here, we can confirm that a small address shift was performed to the memory addresses of the parameters that we're passed on the stack. So every time SOS attempts to display the value of a parameter, it actually displays the value of the parameter that was passed next to it.&lt;br /&gt;
This scenario is a classic example to the bit "buggy" nature of SOS. It doesn't mean that we have to immediatly stop using the &lt;i&gt;!CLRStack&lt;/i&gt; command, but it should to remind us not to take SOS's output as the "absolute truth" when debugging, and just keep ourselves alert for all kind of "weird" behaviors such as this one.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-5906177901421756154?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=bQwOxcYsMGI:skmLMZ7vFPc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=bQwOxcYsMGI:skmLMZ7vFPc:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=bQwOxcYsMGI:skmLMZ7vFPc:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/bQwOxcYsMGI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/bQwOxcYsMGI/clrstack-p-isnt-always-reliable.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/09/clrstack-p-isnt-always-reliable.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-8604647704226410012</guid><pubDate>Thu, 02 Sep 2010 07:12:00 +0000</pubDate><atom:updated>2010-09-02T10:44:46.085+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Debugging</category><category domain="http://www.blogger.com/atom/ns#">.Net Internals</category><category domain="http://www.blogger.com/atom/ns#">Performance</category><title>DateTime.Now in v4.0 Causes Dynamic Memory Allocations</title><description>A while back I've mentioned in a &lt;a href="http://blog.liranchen.com/2010/07/datetimenow-causes-boxing.html"&gt;post&lt;/a&gt; that calling &lt;i&gt;DateTime.Now&lt;/i&gt; causes boxing. Following the posting, a feedback item was also posted on Microsoft Connect, reporting about the issue.&lt;br /&gt;
Yesterday, the feedback's status turned to Resolved after Greg cleverly remarked that this issue was fixed in v4.0 (the boxing occurred until v3.5).&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;However&lt;/b&gt;, after reading the response I took the same benchmark code from the previous post an ran it again, this time using .Net Framework 4.0. Surprisingly, &lt;i&gt;perfmon&lt;/i&gt; kept reporting on high allocation rates in the &lt;i&gt;Allocated Bytes/sec&lt;/i&gt; counter. So I've opened up &lt;i&gt;Reflector&lt;/i&gt; and took a look at &lt;i&gt;DateTime.Now's&lt;/i&gt; new implementation detail. And indeed, the boxing issue was resolved since the new implementation uses the &lt;i&gt;TimeZoneInfo&lt;/i&gt; type instead of &lt;i&gt;TimeZone&lt;/i&gt;. Unable to find the source of the allocations from just looking at the code, it was time to launch &lt;i&gt;WinDbg&lt;/i&gt;.&lt;br /&gt;
After letting the application to run for a while, I've attached WinDbg to the process and executed the &lt;i&gt;!DumpHeap -stat&lt;/i&gt; command a couple of times so I could take a general look at the kind and amount of objects that currently live on the managed heap. The output was as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;0:003&amp;gt; !dumpheap -stat&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;total 0 objects&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Statistics:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MT&amp;nbsp;&amp;nbsp;&amp;nbsp; Count&amp;nbsp;&amp;nbsp;&amp;nbsp; TotalSize Class Name&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;/span&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;... &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;79ba1d88&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1532 System.Char[]&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;79ba2f20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3164 Dictionary[[String, mscorlib],[String, mscorlib]][]&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;79b9f9ac&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 449&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12220 System.String&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;001663d8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 53&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13212&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Free&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;79b56c28&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 17996 System.Object[]&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #fff2cc; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;79b8e7b0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;span style="color: red;"&gt;9119&lt;/span&gt;&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 291808 System.Globalization.DaylightTime&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Total 9771 objects&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;0:003&amp;gt; !dumpheap -stat&lt;/span&gt;&lt;/b&gt;&amp;nbsp; &lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;total 0 objects&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Statistics:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MT&amp;nbsp;&amp;nbsp;&amp;nbsp; Count&amp;nbsp;&amp;nbsp;&amp;nbsp; TotalSize Class Name&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;/span&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;... &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;79ba1d88&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1532 System.Char[]&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;79ba2f20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3164 &lt;/span&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Dictionary[[String, mscorlib],[String, mscorlib]][]&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;79b9f9ac&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 449&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12220 System.String&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;001663d8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 53&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13212&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Free&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;79b56c28&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 17996 System.Object[]&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #fff2cc; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;79b8e7b0&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b style="color: red;"&gt;20654&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 660928 System.Globalization.DaylightTime&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Total 21306 objects&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This output reveals the identity of our memory-consuming bandit: &lt;a href="http://msdn.microsoft.com/en-us/library/system.globalization.daylighttime.aspx"&gt;&lt;i&gt;DaylightTime&lt;/i&gt;&lt;/a&gt;.&lt;br /&gt;
Now, all is left is to spot where this type is being instantated and used. For this purpose, we could use the &lt;i&gt;!BPMD&lt;/i&gt; &lt;i&gt;-md&lt;/i&gt; command in order to place breakpoints on specific managed methods that &lt;i&gt;DaylightTime&lt;/i&gt; exposes (you could dump the type's methods using the following command: &lt;i&gt;!DumpMT -md &lt;methodtable address=""&gt;&lt;/methodtable&gt;&lt;/i&gt;).&lt;br /&gt;
After setting the breakpoint, the application continues its execution and immediately breaks. Looking at the managed callstack using &lt;i&gt;!CLRStack&lt;/i&gt; reveals the source method of the allocation: &lt;i&gt;TimeZoneInfo&lt;/i&gt;.&lt;i&gt;GetIsDaylightSavingsFromUtc&lt;/i&gt;. This method creates an instance of &lt;i&gt;DaylightTime&lt;/i&gt;, and since &lt;i&gt;DaylightTime&lt;/i&gt; is a class (hence, a reference type), a dynamic memory allocation is being made.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="font-family: consolas;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//&amp;nbsp;a&amp;nbsp;snippet&amp;nbsp;from&amp;nbsp;the&amp;nbsp;implementation&lt;/span&gt;:
&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;TimeSpan&lt;/span&gt;&amp;nbsp;span&amp;nbsp;=&amp;nbsp;utc;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;DaylightTime&lt;/span&gt;&amp;nbsp;daylightTime&amp;nbsp;=&amp;nbsp;&lt;span style="background-color: #fff2cc;"&gt;GetDaylightTime&lt;/span&gt;(Year,&amp;nbsp;rule);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;DateTime&lt;/span&gt;&amp;nbsp;startTime&amp;nbsp;=&amp;nbsp;daylightTime.Start&amp;nbsp;-&amp;nbsp;span;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;DateTime&lt;/span&gt;&amp;nbsp;endTime&amp;nbsp;=&amp;nbsp;(daylightTime.End&amp;nbsp;-&amp;nbsp;span)&amp;nbsp;-&amp;nbsp;rule.DaylightDelta;&lt;/pre&gt;&lt;br /&gt;
In conclusion, &lt;i&gt;DateTime.Now's&lt;/i&gt; implementation was updated in BCL v4.0 and the boxing of Int32s was removed since the implementation uses&lt;i&gt; &lt;/i&gt;the &lt;i&gt;TimeZoneInfo&lt;/i&gt; type instead of &lt;i&gt;TimeZone&lt;/i&gt;. However, using &lt;i&gt;TimeZoneInfo&lt;/i&gt; results in a &lt;b&gt;new&lt;/b&gt; source for dynamic allocations, but this time instead of redundant boxings, the allocations are caused just because a reference type is beind used under the hood. And since each instance of &lt;i&gt;DaylightTime&lt;/i&gt; is sized up at 32 bytes (including the &lt;i&gt;Object Header&lt;/i&gt;), the update in the BCL could be considered as a turn for the worst regarding memory usage since &lt;i&gt;DaylightTime&lt;/i&gt; instances are more memory consuming than boxed instances of &lt;i&gt;Int32&lt;/i&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-8604647704226410012?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=7lGf0JzwrLU:k_7kOMX6sBQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=7lGf0JzwrLU:k_7kOMX6sBQ:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=7lGf0JzwrLU:k_7kOMX6sBQ:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/7lGf0JzwrLU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/7lGf0JzwrLU/datetimenow-in-v40-causes-dynamic.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>4</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/09/datetimenow-in-v40-causes-dynamic.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-5105866759605229105</guid><pubDate>Sun, 29 Aug 2010 17:43:00 +0000</pubDate><atom:updated>2010-08-29T20:43:41.718+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.Net Internals</category><title>Brain Teasing With Strings</title><description>And in today's post... a riddle.&lt;br /&gt;
Actually, it comes down to a short C# program that deals with string comparisons, and demonstrates a series of "unexpected" behaviors. How "unexpected" are the results? Well, it depends on the reader's degree of experience at string comparisons.&lt;br /&gt;
Lately this sort of "brain-teasers" became quite popular on different forums/newsgroups, but it seems like no matter where those teasers are posted, it never comes with a full in-depth explanation for all of the "dark-magic" going on behind the scenes, that could potentially explain the demonstrated "weird" and "unexpected" behaviors. So basically, I'll attempt to make things a little bit clearer than it is now.&lt;br /&gt;
So without anymore introductions, please tell me, what would be the output for the following program?&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue;"&gt;&lt;/span&gt;)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;char&lt;/span&gt;[] a = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;char&lt;/span&gt;[0];&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; b = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;(a);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; c = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;char&lt;/span&gt;[0]);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; d = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;char&lt;/span&gt;[0]);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: blue;"&gt;object&lt;/span&gt;.ReferenceEquals(a, b));&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: blue;"&gt;object&lt;/span&gt;.ReferenceEquals(b, c));&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: blue;"&gt;object&lt;/span&gt;.ReferenceEquals(c, d));&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;When this question is presented to a group of experienced developers, it's most likely to result in a variety of different answers, where each of the answers is backed up with some explanation that usually "makes sense". The problem is that they usually just "makes sense", and doesn't refer to some hard proofs that can uncover the mystery completely.&lt;br /&gt;
So, are you ready with your answers? Let's check the output:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;i&gt;False&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;True&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;True&lt;/i&gt;&lt;/blockquote&gt;"Hmm... that was interesting".&lt;br /&gt;
Let's see what do we got here. On the one hand, &lt;i&gt;a&lt;/i&gt; isn't equal to &lt;i&gt;b&lt;/i&gt;. This makes sense since after all, they are difference instances of different types. But on the other hand, we can also see that &lt;i&gt;b&lt;/i&gt; is equal to &lt;i&gt;c&lt;/i&gt; and &lt;i&gt;d&lt;/i&gt; as well. This could appear to be a little confusing since as we know, strings are supposed to be immutable, so according to the code, we are "supposed" to create three different string instances (mind you that we aren't dealing with user-hardcoded, interned strings here).&lt;br /&gt;
It appears that even though we've create three &lt;b&gt;different&lt;/b&gt; arrays, we've only allocated a single string (that one that &lt;i&gt;b, c&lt;/i&gt; and &lt;i&gt;d&lt;/i&gt; references).&lt;br /&gt;
&lt;br /&gt;
Usually, when a &lt;i&gt;char[]&lt;/i&gt; (or, some other kind of string-representing structure) is passed to a string constructor, a new string is allocated to represent the array's characters. However, as our brief program demonstrates, this isn't always the case.&lt;br /&gt;
To understand why this happens, we'll have to dig into the implementation of the &lt;i&gt;string&lt;/i&gt; class, and more specifically, to it's internal implementation in &lt;i&gt;SString&lt;/i&gt; (the following code samples are taken from &lt;a href="http://en.wikipedia.org/wiki/Shared_Source_Common_Language_Infrastructure"&gt;SSCLI&lt;/a&gt;'s implementation).&lt;br /&gt;
Looking at &lt;i&gt;SString&lt;/i&gt;'s internal constructor, we could notice that the &lt;i&gt;char[]&lt;/i&gt; argument that was received is checked whether it's set to NULL or it's length is equal to zero. If it does, the implementation completely "forgets" about that argument, and instead uses the new &lt;i&gt;SString&lt;/i&gt; instance to reference an interned version of an empty string (so basically, the redundant allocation of the new, empty string, is optimized away).&lt;br /&gt;
We could see this behavior in &lt;i&gt;SString's Set&lt;/i&gt; and &lt;i&gt;Clear&lt;/i&gt; functions (the &lt;i&gt;Set &lt;/i&gt;function is invoked from the constructor).&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt; SString::Set(&lt;span style="color: blue;"&gt;const&lt;/span&gt; WCHAR *string)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{ &lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (string == NULL || *string == 0)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;Clear();&lt;/b&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Resize((COUNT_T) wcslen(string), REPRESENTATION_UNICODE);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; wcscpy_s(GetRawUnicode(), GetBufferSizeInCharIncludeNullChar(), string);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; RETURN;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;/div&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt; SString::Clear()&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetRepresentation(REPRESENTATION_EMPTY);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (IsImmutable())&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt; &lt;span style="color: green;"&gt;// Use shared empty string rather than allocating a new buffer&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SBuffer::SetImmutable(s_EmptyBuffer, &lt;span style="color: blue;"&gt;sizeof&lt;/span&gt;(s_EmptyBuffer));&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SBuffer::TweakSize(&lt;span style="color: blue;"&gt;sizeof&lt;/span&gt;(WCHAR));&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; GetRawUnicode()[0] = 0;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; RETURN;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-5105866759605229105?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=4LFd-TLFI1c:OHYhVdnFrog:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=4LFd-TLFI1c:OHYhVdnFrog:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=4LFd-TLFI1c:OHYhVdnFrog:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/4LFd-TLFI1c" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/4LFd-TLFI1c/brain-teasing-with-strings.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>4</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/08/brain-teasing-with-strings.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-6286216209604546584</guid><pubDate>Tue, 24 Aug 2010 19:08:00 +0000</pubDate><atom:updated>2010-10-05T21:35:45.138+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Performance</category><category domain="http://www.blogger.com/atom/ns#">Multithreading</category><title>Reducing AutoResetEvent's Synchronization Overhead</title><description>One of the most common design patterns in multithreaded programming is the producer-consumer. In the usual scenario, a consumer thread is spawned into an infinite loop, where it waits on some handle until new data arrives from the producer threads. Then it wakes up, and process the new data.&lt;br /&gt;
The overall idea can be expressed via this code:&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;AutoResetEvent m_event = &lt;span style="color: blue;"&gt;new&lt;/span&gt; AutoResetEvent(&lt;span style="color: blue;"&gt;false&lt;/span&gt;);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; WorkCycles()&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;while&lt;/span&gt;(&lt;span style="color: blue;"&gt;true&lt;/span&gt;)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// wait for a signal&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; m_event.WaitOne();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// do work..&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;Using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx"&gt;&lt;i&gt;AutoResetEvent&lt;/i&gt;&lt;/a&gt; class seems very suitable for this kind of scenario since it supplies us the exact behavior that we're looking for: causing a thread to to wait on a handle until a signal arrives. However, calls to &lt;i&gt;WaitOne/Set&lt;/i&gt; come with a price, and it ain't cheap. The &lt;i&gt;AutoResetEvent&lt;/i&gt; class is simply a managed-code wrapper for Win32's &lt;i&gt;WaitForSingleObject&lt;/i&gt;, and it doesn't keep track of the internal event's state (whether it's currently signaled state or not), thus, every call to &lt;i&gt;AutoResetEvent&lt;/i&gt; will result in diving all the way into kernel-mode, even though "we don't have to" (if we would have tracked the event's internal state).&lt;br /&gt;
In some scenarios, especially in those where producer threads pass data for the consumer in high frequencies, most of the calls to &lt;i&gt;Set()&lt;/i&gt; are redundant. If for example every 100ms a producer passes new data (which results in a calling to &lt;i&gt;Set)&lt;/i&gt;, but it takes the consumer an entire second to process that data, then it means we've spent time calling &lt;i&gt;Set&lt;/i&gt; 9 times more than we had to. Additionally, after the consumer finished its processing, it will have to call &lt;i&gt;WaitOne&lt;/i&gt; again, only to realize the event was already set by a producer.&lt;br /&gt;
&lt;br /&gt;
So, how expensive are those redundant calls to &lt;i&gt;AutoResetEvent&lt;/i&gt;? The following benchmark should demonstrate:&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;while&lt;/span&gt; (&lt;span style="color: blue;"&gt;true&lt;/span&gt;)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AutoResetEvent eventObj = &lt;span style="color: blue;"&gt;new&lt;/span&gt; AutoResetEvent(&lt;span style="color: blue;"&gt;false&lt;/span&gt;);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Stopwatch sw = Stopwatch.StartNew();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;for&lt;/span&gt; (&lt;span style="color: blue;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 1000000; i++)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; eventObj.Set();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; eventObj.WaitOne();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(sw.ElapsedMilliseconds);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
On my machine, the average result was &lt;b&gt;1035ms&lt;/b&gt;.&lt;br /&gt;
A whole second, this is the ridiculously large amount of time it took us the complete those few lines of code. We are wasting here an entire second just for using a synchronization primitive that in our real application, could be considered to be quite insignificant at first look. Seemingly, we shouldn't waste more than a few milliseconds running that code, but as you can see, things are a little different in practice.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;EconomicAutoResetEvent&lt;/b&gt;&lt;br /&gt;
The solution for this problem is quite straightforward, since our goal is to reduce the number of redundant calls to &lt;i&gt;Set/WaitOne&lt;/i&gt; to a minimum. How could you achieve that? One option is to use an alternative synchronization primitive, that for the sake of this post, will be named &lt;i&gt;EconomicAutoResetEvent&lt;/i&gt;. The function of this new primitive, is to improve &lt;i&gt;AutoResetEvent&lt;/i&gt;'s performance by keeping track of the event's state, thus, avoiding transitions into the kernel.&lt;br /&gt;
I'll demonstrate a possible implementation for such primitive, and explain its behavior in more detail afterwords:&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; EconomicResetEvent&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;volatile int&lt;/span&gt; m_eventState;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; AutoResetEvent m_event;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; Thread m_worker;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; EVENT_SET&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = 1;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; EVENT_NOT_SET = 2;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; EVENT_ON_WAIT = 3;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; EconomicResetEvent(&lt;span style="color: blue;"&gt;bool&lt;/span&gt; initialState, Thread workerThread)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; m_event = &lt;span style="color: blue;"&gt;new&lt;/span&gt; AutoResetEvent(initialState);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; m_eventState = initialState ? EVENT_SET : EVENT_NOT_SET;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; m_worker = workerThread;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; WaitForWork()&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; verifyCaller();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (m_eventState == EVENT_SET &amp;amp;&amp;amp; Interlocked.CompareExchange(&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;ref&lt;/span&gt; m_eventState, EVENT_NOT_SET, EVENT_SET) == EVENT_SET)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt;;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (m_eventState == EVENT_NOT_SET &amp;amp;&amp;amp; Interlocked.CompareExchange(&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;ref&lt;/span&gt; m_eventState, EVENT_ON_WAIT, EVENT_NOT_SET) == EVENT_NOT_SET)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; m_event.WaitOne();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; SignalWork()&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (m_eventState == EVENT_NOT_SET &amp;amp;&amp;amp; Interlocked.CompareExchange(&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;ref&lt;/span&gt; m_eventState, EVENT_SET, EVENT_NOT_SET) == EVENT_NOT_SET)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt;;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (m_eventState == EVENT_ON_WAIT &amp;amp;&amp;amp; Interlocked.CompareExchange(&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;ref&lt;/span&gt; m_eventState, EVENT_NOT_SET, EVENT_ON_WAIT) == EVENT_ON_WAIT)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; m_event.Set();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// [Conditional("DEBUG")]&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; verifyCaller()&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (m_worker != Thread.CurrentThread)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; errMsg = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format("Only the pre-defined Worker thread may &lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; call WaitOne (Current: {0}, Worker: {1})", Thread.CurrentThread, m_worker);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;throw&lt;/span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; SynchronizationLockException(errMsg);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&amp;nbsp; &lt;br /&gt;
I assume that the question most of you are probably thinking about right now is "How fast it is?". Well, after running the same benchmark as before, the average result I get on my machine is only &lt;b&gt;9ms&lt;/b&gt;. An outstanding 11500% of improvement over using &lt;i&gt;AutoResetEvent&lt;/i&gt; directly. And to think that all we just did is to avoid some calls to the internal kernel object.&lt;br /&gt;
The most important part in the implementation is the tracking of the state of the internal kernel object (signaled/not-signaled). The state itself is kept in &lt;i&gt;m_eventState&lt;/i&gt;, this how we could know which calls to &lt;i&gt;WaitForWork/SignalWork&lt;/i&gt; could avoid accessing &lt;i&gt;m_event &lt;/i&gt;(&lt;i&gt;AutoResetEvent&lt;/i&gt;).&lt;br /&gt;
Additionally, in order to make the store/load operations on &lt;i&gt;m_eventState&lt;/i&gt; thread-safe, I've used a couple of &lt;a href="http://en.wikipedia.org/wiki/Compare-and-swap"&gt;CAS&lt;/a&gt; operations, that even though they could be cheaper then a "full blown lock", it's still considered to be quite an expensive instruction that usually it's best to avoid when they're not necessary. This is exactly why the double-tests on the &lt;i&gt;if&lt;/i&gt; statements are performed. This technique is usually referred to as &lt;a href="http://en.wikipedia.org/wiki/Test_and_Test-and-set"&gt;TATAS&lt;/a&gt; (i.e, Test-and-Test-and-Set).&lt;br /&gt;
As the comment at the class's deceleration mentions, some mild race conditions could occur when the consumer thread is entering/leaving &lt;i&gt;WaitForWork&lt;/i&gt; while a producer calls &lt;i&gt;SignalWork&lt;/i&gt; exactly at a very specific moment. This race condition won't cause any functional harm, at its worst-case, it would cause us to perform a redundant call to &lt;i&gt;Set/WaitOne. &lt;/i&gt;Resolving this "issue" would mean that we prefer to enforce fairness. Such enforcement could have some negative effects on performance.&lt;br /&gt;
&lt;br /&gt;
Since we're already discussing the subject, It would be appropriate to mention that the&lt;i&gt; &lt;/i&gt;&lt;a href="http://en.wikipedia.org/wiki/Parallel_Extensions"&gt;Parallel Extensions&lt;/a&gt; library had introduced a new synchronization primitive named &lt;i&gt;ManualResetEventSlim&lt;/i&gt;, that also supplies us with extended functionality and better performance over the standard event objects. Beyond keeping the state of the internal kernel event, it also performs some spinning around the flag before actually blocking the thread, and also uses &lt;i&gt;lazy initialization&lt;/i&gt; to create the kernel object (its created only when it's needed). The good guys at Microsoft were kind enough to publish a short but informative &lt;a href="http://services.social.microsoft.com/feeds/FeedItem?feedId=639a99a9-ff25-4062-b61d-a86ea9d66a06&amp;amp;itemId=b1ce1558-9338-4f4c-94e2-c360508d99b9&amp;amp;title=Performance+Characteristics+of++%0aNew+Synchronization+Primitives+in+the++%0a.NET+Framework+4&amp;amp;uri=http%3a%2f%2fdownload.microsoft.com%2fdownload%2fB%2fC%2fF%2fBCFD4868-1354-45E3-B71B-B851CD78733D%2fPerformanceCharacteristicsOfSyncPrimitives.pdf&amp;amp;k=AOKtgi85h3hYnBJJXymVpck1JsfsXC2f37BmAWLfL44%3d"&gt;document&lt;/a&gt; that demonstrates the performance differences between the new and old synchronization primitives under a selected group of usage scenarios.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-6286216209604546584?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=DENzREhm4jE:-g5vt3VqJtg:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=DENzREhm4jE:-g5vt3VqJtg:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=DENzREhm4jE:-g5vt3VqJtg:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/DENzREhm4jE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/DENzREhm4jE/reducing-autoresetevents.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/08/reducing-autoresetevents.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-1197277921611629760</guid><pubDate>Fri, 20 Aug 2010 06:13:00 +0000</pubDate><atom:updated>2010-09-25T20:25:12.935+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Networking</category><category domain="http://www.blogger.com/atom/ns#">Performance</category><title>The Case of Delayed ACKs and Nagle's Algorithm</title><description>Even though the negative consequences of combining Nagle's Algorithm with Delayed ACKs when using TCP is well documented in the literature, it's still considered to be a common pitfall that many people tend to forget about. Since the negative effects of the combination will appear only under certain pre-conditions, it could be a while until one correlates the application's "weird" behavior with the usage of those two algorithms. So as the performance penalty grows to be more distinct, it would be easier the identify its source.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Delayed ACKs&lt;/b&gt;&lt;br /&gt;
Since TCP guarantees that every packet that is sent on the network will arrive to its designated remote host, it needs some kind of acknowledgment from that remote host that the packet managed to arrive. So, every time a host receives a packet, it needs to send back an ACK message as an acknowledgment.&lt;br /&gt;
The problem with using dedicated ACK messages is that we could find ourselves "overflowing" the network with messages that have nothing to do with our application (and contains almost no data at all). All of this just to say "Yes, I've received your packet" (if you combine the minimal size of a TCP and IP header [even without the Ethernet's header size] you already reach 40 bytes [and for IPv6, that number increases to 60 bytes]).&lt;br /&gt;
So, in order to reduce that overhead, the usage of "Delayed ACKs" was defined. The idea is that instead of sending a dedicated ACK message for each received packet, we make an assumption that our application (the receiver) is probably about to send "some" message (not necessarily a response message) back to the remote host that sent us the packet in the first place. This way, we could "piggyback" the application's message and add to its header the ACK for the previously received packet. Hench, we could substantially reduce that amount of redundant data circulating in our network.&lt;br /&gt;
Usually its customary to use a 200ms delay for sending ACKs (the exact behavior of the timeout depends on the protocol's implementation detail. Does a 200ms timer is created when the socket opens? or perhaps only at times that the application needs to send ACKs?).&lt;br /&gt;
In Windows, the default timeout is 200ms as well. But if you want to, you can change it by modifying the value of the &lt;i&gt;&lt;a href="http://technet.microsoft.com/en-us/library/cc938206.aspx"&gt;TcpDelAckTicks&lt;/a&gt; &lt;/i&gt;in the registry, and set it somewhere between 0ms and 600ms (as an anecdote, the Host Requirements RFC forbids using ACK delays greater than 500ms).&lt;br /&gt;
Its worth noting that according to the RFC, there's no need to send back an ACK for every packet we receive, since a single ACK message could acknowledge multiple received packets. If for example machine A sends 5 different messages in 10ms differences to machine B, the minute the first message arrives to machine B, it opens a timer with a 200ms timeout so it will be alerted to send back an ACK message for the first message (assuming this is a one-way interface, so machine B won't send back any messages). Until that timer will elapse, the machine will receive the other 4 messages. So if we're already going to send an ACK for the first message, we might as well modify it so it will acknowledge the entire set of 5 messages.&lt;br /&gt;
Additionally, receiving packets aren't the only triggers for sending an ACK message. For example, if our receive window (the maximum number of bytes we can receive without sending back an ACK message), we will have to immediately send back an ACK message. Another trigger is the "Send ACK for Every Second Packet", that does exactly what its name implies. In Windows, the default value (2) can be changed, by modifying the &lt;i&gt;&lt;a href="http://support.microsoft.com/kb/328890"&gt;TcpAckFrequency&lt;/a&gt; &lt;/i&gt;registry value.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Nagle's Algorithm&lt;/b&gt;&lt;br /&gt;
Even though it isn't directly related to the usage of Delayed ACKs, this algorithm attempts to resolve a similar problem that occurs on the sender's side. From the same reason we shouldn't overflow the network with "small, ACK messages", we shouldn't send "small, application messages" ourselves, due to the overhead involved in sending the entire header, while the message's body is very small.&lt;br /&gt;
According to the algorithm, the protocol may delay small "send" operations so it could buffer them together (and thus, a single packet could be send for more than a couple of small, applicative messages).&lt;br /&gt;
Deciding when to stop buffering the data isn't arbitrary, as it depends on the rate of receiving ACKs from the remote host. The idea is that until we haven't received an ACK message for our previously sent packet, there's no point in sending an additional packet. So, while we are waiting to receive an ACK from the remote host, we are buffering all of data we are about to send (of course, under the limits of the &lt;a href="http://en.wikipedia.org/wiki/Maximum_segment_size"&gt;MSS&lt;/a&gt;). The minute we'll receive the ACK for our previous packet, all of the buffered data will be combined, and sent as a single packet.&lt;br /&gt;
&lt;br /&gt;
Seemingly, both of the mentioned algorithms justifies their existence since they are attempting to resolve real problems. However, what will happen if we'll combine them both? On one hand, both of them will try to reduce the amount of tinygrams being sent on their side (either ACK messages, or applicative messages). But on the other hand, under certain conditions, they may cause significant amount of delay when attempting to send messages on the network. The most obvious example is when you've got a one-way interface that only one side only sends messages, and the other just receive them (without "answering"). In this case, even if the application continuously sends messages under a high rate, we are expected to notice latencies up to 200ms, since the time the message was "sent" by the application, until it was received by the recipient (since it was delayed by Nagle's algorithm). In other cases, also in two-way interfaces, there might be occasions in which the client/server stops sending message for a couple of moments. This also might cause latencies up to 200ms in its receive rate. In those kind of cases, identifying the source of the latencies might be more difficult since they tend to appear randomly, with latencies that are not always constant (less then 200ms). The exact behavior is determined by the characteristics of the application.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://i50.tinypic.com/9abkap.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://i50.tinypic.com/9abkap.gif" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;br /&gt;
&lt;/div&gt;In order to illustrate this behavior via code, I've written the following program that measures the time it takes us to send two messages that seemingly should be sent instantly. One right after the other.&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt; Server()&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Socket server = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; server.Bind(ServerEndPoint);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; server.Listen(1);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Socket s = server.Accept();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;while&lt;/span&gt; (&lt;span style="color: blue;"&gt;true&lt;/span&gt;)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// measure how long it takes to receive both messages&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Stopwatch stopwatch = Stopwatch.StartNew();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; s.Receive(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[8]);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; s.Receive(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[8]);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Output: around 200ms&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(stopwatch.ElapsedMilliseconds);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt; Client()&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Socket client = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; client.Bind(ClientEndPoint);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; client.Connect(ServerEndPoint);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;while&lt;/span&gt; (&lt;span style="color: blue;"&gt;true&lt;/span&gt;)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; client.Send(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[8]); &lt;span style="color: green;"&gt;// will be sent immediately&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; client.Send(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[8]); &lt;span style="color: green;"&gt;// delayed for 200ms&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// wait for an imaginery response&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; client.Receive(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[0]);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
Due to the unpleasant effects that might cause due to the combination of the algorithms, the RFC states that implementations of the TCP protocol, that use Nagle's algorithm, must also support a way to disable it, so applicative messages won't be delayed by the protocol when they are being sent. This ability is exposed to us by using the &lt;i&gt;TCP_NODELAY&lt;/i&gt; flag, and in .Net, its usage is wrapped with the &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.nodelay.aspx"&gt;&lt;i&gt;Socket.NoDelay&lt;/i&gt;&lt;/a&gt; property.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-1197277921611629760?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=F1ufAb4IZRg:4iGHWfrc-hM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=F1ufAb4IZRg:4iGHWfrc-hM:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=F1ufAb4IZRg:4iGHWfrc-hM:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/F1ufAb4IZRg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/F1ufAb4IZRg/case-of-delayed-acks-and-nagles.html</link><author>noreply@blogger.com (Liran Chen)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://i50.tinypic.com/9abkap_th.gif" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/08/case-of-delayed-acks-and-nagles.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-8555592221574058269</guid><pubDate>Sat, 14 Aug 2010 17:31:00 +0000</pubDate><atom:updated>2011-12-19T21:25:53.735+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Performance</category><category domain="http://www.blogger.com/atom/ns#">Multithreading</category><title>Don't Rely on Environment.ProcessorCount</title><description>One of the most hidden knowledge in multithreaded programming is the question &lt;i&gt;"How many threads I should use in my application to achieve the best performance from my available hardware?"&lt;/i&gt;. The answer to this question may vary since it depends on the characteristics of the application. Whether it's CPU bound, or IO bound? How much of the work is being paralleled? and so on... At the bottom line, all of these "formulas" are based upon the number of available processors.&lt;br /&gt;
&lt;br /&gt;
Usually when an application runs it's initialization routine and gets ready to create a collection of worker threads, it will check how many processors are installed on the machine, so it could decide how many threads exactly it should create. To get the number of installed processors, you would usually call &lt;a href="http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx"&gt;&lt;i&gt;Environment.ProcessorCount&lt;/i&gt;&lt;/a&gt;. This property simply calls the Win32 &lt;i&gt;GetSystemInfo&lt;/i&gt; function, and returns the &lt;i&gt;dwNumberOfProcessors&lt;/i&gt; field to the caller. The problem with this value is that it doesn't necessarily represent the number of processors available to our process. In a certain scenario, the user that launched our application might have decided to give it a certain &lt;a href="http://msdn.microsoft.com/en-us/library/aa384228%28VS.85%29.aspx"&gt;&lt;i&gt;Processor Affinity&lt;/i&gt;&lt;/a&gt;, that will cause our application's threads to execute only on the processors set by the given affinity, instead of all the installed processors. The problem in this case is that the application will completely ignore the processor affinity, and create a larger amount of threads than it should (in an even worse case, it could assign the threads with affinities set to processors that aren't even available to the process).&lt;br /&gt;
What will eventually happen is that we'll have too many threads running on our subset of available processors. Causing us to suffer from a performance penalty caused by excessive context switching.&lt;br /&gt;
The scenario in which we set our process to run on only a subset of processors is far from being far-fetched since in situations where we have a couple of applications that are designed to take advantage of all of the available processors, and are meant to run in parallel on the same machine (without using some kind of VMware), then we would probably like to split our installed processors in half, giving each process only a set or processors to use. If the applications won't be aware to this subset, then we'll find ourselves wasting valuable CPU cycles, for no good reason.&lt;br /&gt;
&lt;br /&gt;
In .Net, you could get the processor affinity by using the property &lt;i&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.processoraffinity.aspx"&gt;Process.ProcessorAffinity&lt;/a&gt;. &lt;/i&gt;It will return a bitmask where each bit represents a logical CPU where our process can execute (in case the mask is set to 0, the scheduler will decide which processors our application will use. So basically, all of the processors are available).&lt;br /&gt;
Current versions of Windows provide support for &lt;a href="http://software.intel.com/en-us/blogs/2009/01/05/what-does-256-cores-look-like/"&gt;more than 64 logical CPUs&lt;/a&gt;. In order to address all of those CPUs, they are divided into &lt;i&gt;Groups&lt;/i&gt;, where each group can address up to 64 processors. So when looking at the processor affinity bitmask, you should be aware that it belongs only to a specific group (by default, only a single group is used).&lt;br /&gt;
So next time you're checking how many processors are available to your process, remember to count the number of lit bits in your processor affinity bitmask.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="font-family: consolas;"&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;PrintAffinitizedProcessors()
&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//&amp;nbsp;gets&amp;nbsp;the&amp;nbsp;number&amp;nbsp;of&amp;nbsp;affinitized&amp;nbsp;proccesors&amp;nbsp;in&amp;nbsp;the&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//&amp;nbsp;current&amp;nbsp;processor&amp;nbsp;group&amp;nbsp;(up&amp;nbsp;to&amp;nbsp;64&amp;nbsp;logical&amp;nbsp;processors)&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Process&lt;/span&gt;&amp;nbsp;currentProcess&amp;nbsp;=&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Process&lt;/span&gt;.GetCurrentProcess();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&amp;nbsp;affinityMask&amp;nbsp;=&amp;nbsp;(&lt;span style="color: blue;"&gt;long&lt;/span&gt;)currentProcess.ProcessorAffinity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(affinityMask&amp;nbsp;==&amp;nbsp;0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;affinityMask&amp;nbsp;=&amp;nbsp;(&lt;span style="color: blue;"&gt;long&lt;/span&gt;)&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;.Pow(&lt;span style="color: #2b91af;"&gt;Environment&lt;/span&gt;.ProcessorCount,&amp;nbsp;2)&amp;nbsp;-&amp;nbsp;1;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;const&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;BITS_IN_BYTE&amp;nbsp;=&amp;nbsp;8;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;numberOfBits&amp;nbsp;=&amp;nbsp;&lt;span style="color: #2b91af;"&gt;IntPtr&lt;/span&gt;.Size&amp;nbsp;*&amp;nbsp;BITS_IN_BYTE;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;counter&amp;nbsp;=&amp;nbsp;0;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;for&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;i&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;i&amp;nbsp;&amp;lt;&amp;nbsp;numberOfBits;&amp;nbsp;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;((affinityMask&amp;nbsp;&amp;gt;&amp;gt;&amp;nbsp;i&amp;nbsp;&amp;amp;&amp;nbsp;1)&amp;nbsp;==&amp;nbsp;1)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(i);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;counter++;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"Total:&amp;nbsp;"&lt;/span&gt;&amp;nbsp;+&amp;nbsp;counter);
&amp;nbsp;}&lt;/pre&gt;
&lt;span class="em"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-8555592221574058269?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=9OxVIfZdDmc:kkMcC3eBHwk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=9OxVIfZdDmc:kkMcC3eBHwk:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=9OxVIfZdDmc:kkMcC3eBHwk:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/9OxVIfZdDmc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/9OxVIfZdDmc/dont-rely-on-environmentprocessorcount.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/08/dont-rely-on-environmentprocessorcount.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-2712674694475545976</guid><pubDate>Sat, 07 Aug 2010 10:18:00 +0000</pubDate><atom:updated>2010-08-07T13:34:35.180+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">GC</category><category domain="http://www.blogger.com/atom/ns#">Performance</category><category domain="http://www.blogger.com/atom/ns#">CLR Hosting</category><title>Accurately Measuring GC Suspensions</title><description>When you analyze the performance of a managed application, and look for the application's main bottlenecks, one place you should always check is the amount of time your application spend in GC. In order to get that information, you can always run &lt;i&gt;&lt;a href="http://blogs.msdn.com/b/maoni/archive/2004/06/03/148029.aspx"&gt;Perfmon&lt;/a&gt; &lt;/i&gt;and get a general analysis of how the GC behaves in your application (where the most of the time, you'll be looking at the %Time in GC counter).&lt;br /&gt;
While looking at those data sets can give us a good overview about how the GC affects our application's performance, it's just not enough in order to get more in-depth insights about observed "fezzes" in the application. For instance, even if we'll see that in 90% of the time we only spend 2% in GC collections, it still doesn't mean that during some critical moment in our application, there wasn't any generation 2 collection that caused our application to freeze for about 200ms, which could cause some serious damage in some scenarios.&lt;br /&gt;
The problem is that GC collections don't just cause the application to "run more slowly", but it puts the application into a complete stop for unknown amounts of time. So solely looking at the %Time in GC graph doesn't tells us for sure what was the real damage of each collection. Also, remember that perfmon's highest sampling resolution is only 1 second. So in case we've suffered from a single generation 2 collection, we've might even won't notice it as we should. More than that, its almost impossible to make sure that perfmon is runs, gathers and records the correct data on every machine that runs your application at any given time.&lt;br /&gt;
&lt;br /&gt;
This is why we need a reliable way to monitor when the GC decides to perform a collection, and when it does, check the collection's generation, and the amount of time it takes to complete it.&lt;br /&gt;
Today, the &lt;i&gt;System.GC&lt;/i&gt; class doesn't expose us an accurate way to collect this data. However, by writing a custom &lt;i&gt;CLR Host&lt;/i&gt;, we could integrate our code with the CLR and receive from it the proper notifications that will tell us exactly when each and every GC starts and end in the process.&lt;br /&gt;
In order to do so, we will need to implement the &lt;a href="http://msdn.microsoft.com/en-us/library/ms164485.aspx"&gt;&lt;i&gt;IHostGCManager&lt;/i&gt;&lt;/a&gt; interface, and use one out of its three available callback functions. The first is &lt;a href="http://msdn.microsoft.com/en-us/library/ms164487.aspx"&gt;&lt;i&gt;SuspensionStarting&lt;/i&gt;&lt;/a&gt;, and the second is &lt;a href="http://msdn.microsoft.com/en-us/library/ms164486.aspx"&gt;&lt;i&gt;SuspensionEnding&lt;/i&gt;&lt;/a&gt;, which also passes us a single &lt;i&gt;DWORD&lt;/i&gt; parameter that represent the number of generation that ended. So as you might have already figured out, the CLR makes sure to call &lt;i&gt;SuspensionStarting&lt;/i&gt; right before it starts the collection, and afterwords, it calls &lt;i&gt;SuspensionEnding&lt;/i&gt;.&lt;br /&gt;
One thing to pay attention about &lt;i&gt;SuspensionEnding&lt;/i&gt; is that the documentation available on MSDN isn't entirely accurate, and might confuse users about what are the possible triggers to call the callback. This is what the documentation says today:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;i&gt;"...Notifies the host that the common language runtime (CLR) is resuming execution of tasks on threads that had been suspended for a garbage collection.&lt;br /&gt;
[parameter: generation] The garbage collection generation that is just finishing, from which the thread is resuming."&lt;/i&gt;&lt;/blockquote&gt;So according to the documentation, the callback will only be invoked due to freezings caused by a GC. However, this isn't the only trigger for invoking this callback. Actually, the CLR will invoke it also after it continuous execution after other kinds of "stop-the-world" operations that it might perform (e.g, loading and unloading of &lt;i&gt;AppDomains&lt;/i&gt;). If we'll look in the &lt;a href="http://en.wikipedia.org/wiki/Shared_Source_Common_Language_Infrastructure"&gt;&lt;i&gt;SSCLI&lt;/i&gt;&lt;/a&gt;'s implementation to where that callback is invoked, we could notice the following code:&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt; (pGCThreadControl)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// If we the suspension was for a GC, tell the host what generation GC.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DWORD&amp;nbsp;&amp;nbsp; Generation = (bFinishedGC&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ? GCHeap::GetGCHeap()-&amp;gt;GetCondemnedGeneration()&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : ~0U);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pGCThreadControl-&amp;gt;SuspensionEnding(Generation);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;So we can see that for every freezing that wasn't caused due to a GC, the &lt;i&gt;generation&lt;/i&gt; parameter that is passed to the callback will contain the value of &lt;i&gt;UINT_MAX&lt;/i&gt;, so when implementing your &lt;i&gt;CLR Host&lt;/i&gt;, you should remember checking for this special value.&lt;br /&gt;
&lt;br /&gt;
As for the measuring itself, we'll use the &lt;i&gt;QueryPerformanceCounter&lt;/i&gt; function (in .Net, wrapped by the &lt;i&gt;Stopwatch&lt;/i&gt; class), to achieve the highest possible time resolution for every collection.&lt;br /&gt;
Since in most of the time, the collections that we'll encounter will be very short ones (mostly ephemeral collections, that could take only a few millisecond per collection), we'll likely want to avoid spending time recording the data (so we'll avoid unnecessary IO). In such case it could be useful to use a logging framework that will filter collections according to their severity (e.g, &lt;i&gt;Debug&lt;/i&gt; for very short collections, &lt;i&gt;Info&lt;/i&gt; for more notable collections, and &lt;i&gt;Warn&lt;/i&gt; for more lengthy collections that might indicate a problem). After attaching an appender that writes all of our logs to the console window, and running an application that constantly allocates memory, we could get an output such as this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://i37.tinypic.com/160df77.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://i37.tinypic.com/160df77.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;/div&gt;&lt;br /&gt;
As a reference, I'm including a sample &lt;i&gt;CLR Host&lt;/i&gt; that monitors collections, and writes to the console their duration:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;#include&lt;/span&gt; &lt;mscoree.h&gt;&lt;/mscoree.h&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;#include&lt;/span&gt; &lt;windows.h&gt;&lt;/windows.h&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;#include&lt;/span&gt; &lt;assert.h&gt;&lt;/assert.h&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;#include&lt;/span&gt; &lt;iostream&gt;&lt;/iostream&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; &lt;span style="color: blue;"&gt;namespace&lt;/span&gt; std;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;#define&lt;/span&gt; APP_STARTUP_EXE L"TestApplication.exe"&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;#define&lt;/span&gt; APP_ENTRY_TYPE L"SomeNamespace.Program"&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;#define&lt;/span&gt; APP_ENTRY_METHOD L"Main"&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt; MyCLRHost : &lt;span style="color: blue;"&gt;public&lt;/span&gt; IHostControl, &lt;span style="color: blue;"&gt;public&lt;/span&gt; IHostGCManager&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;:&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LONG m_refCount;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LARGE_INTEGER m_lastGCStart;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LARGE_INTEGER m_frequency;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;:&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MyCLRHost() { QueryPerformanceFrequency(&amp;amp;m_frequency); }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// IHostControl&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HRESULT &lt;span style="color: blue;"&gt;__stdcall&lt;/span&gt; GetHostManager(REFIID riid, &lt;span style="color: blue;"&gt;void&lt;/span&gt;** ppObject)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt;(riid == IID_IHostGCManager)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; *ppObject = &lt;span style="color: blue;"&gt;static_cast&lt;/span&gt;&lt;ihostgcmanager*&gt;(&lt;span style="color: blue;"&gt;this&lt;/span&gt;);&lt;/ihostgcmanager*&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; S_OK;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; *ppObject = NULL;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; E_NOINTERFACE;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// IUnknown&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HRESULT &lt;span style="color: blue;"&gt;__stdcall&lt;/span&gt; QueryInterface(REFIID riid, &lt;span style="color: blue;"&gt;void&lt;/span&gt;** ppvObject)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (riid == IID_IHostGCManager)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; *ppvObject = &lt;span style="color: blue;"&gt;static_cast&lt;/span&gt;&lt;ihostgcmanager*&gt;(&lt;span style="color: blue;"&gt;this&lt;/span&gt;);&lt;/ihostgcmanager*&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; S_OK;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; *ppvObject = NULL;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; E_NOINTERFACE;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HRESULT &lt;span style="color: blue;"&gt;__stdcall&lt;/span&gt; SetAppDomainManager(DWORD appDomain, IUnknown* domainManager)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; S_OK;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ULONG &lt;span style="color: blue;"&gt;__stdcall&lt;/span&gt; AddRef() { &lt;span style="color: blue;"&gt;return&lt;/span&gt; InterlockedIncrement(&amp;amp;m_refCount); }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ULONG &lt;span style="color: blue;"&gt;__stdcall&lt;/span&gt; Release() { &lt;span style="color: blue;"&gt;return&lt;/span&gt; InterlockedDecrement(&amp;amp;m_refCount); }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// IHostGCManager&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HRESULT &lt;span style="color: blue;"&gt;__stdcall&lt;/span&gt; ThreadIsBlockingForSuspension() { &lt;span style="color: blue;"&gt;return&lt;/span&gt; S_OK; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HRESULT &lt;span style="color: blue;"&gt;__stdcall&lt;/span&gt; SuspensionStarting()&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; m_lastGCStart;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; QueryPerformanceCounter(&amp;amp;m_lastGCStart);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; S_OK;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HRESULT &lt;span style="color: blue;"&gt;__stdcall&lt;/span&gt; SuspensionEnding(DWORD gen)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; LARGE_INTEGER gcEnd;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; QueryPerformanceCounter(&amp;amp;gcEnd);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;double&lt;/span&gt; duration = ((gcEnd.QuadPart - m_lastGCStart.QuadPart)) &lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; * 1000.0 / (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)m_frequency.QuadPart;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt;(gen != UINT_MAX)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; cout&amp;lt;&amp;lt;"GC generation "&amp;lt;&lt;gen&gt;&amp;lt;&amp;lt;" ended: "&amp;lt;&lt;duration&gt;&amp;lt;&amp;lt;"ms"&amp;lt;&lt;endl;&gt;&lt;/endl;&gt;&lt;/duration&gt;&lt;/gen&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; cout&amp;lt;&amp;lt;"CLR suspension ended: "&amp;lt;&lt;duration&gt;&amp;lt;&amp;lt;" ms"&amp;lt;&lt;endl;&gt;&lt;/endl;&gt;&lt;/duration&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; S_OK;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;};&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt; _tmain(&lt;span style="color: blue;"&gt;int&lt;/span&gt; argc, _TCHAR* argv[])&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ICLRRuntimeHost* pCLR;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DWORD startupFlags = STARTUP_CONCURRENT_GC;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HRESULT hr = CorBindToRuntimeEx(L"v2.0.50727", L"wks", startupFlags,&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&amp;amp;pCLR);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; assert(SUCCEEDED(hr));&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MyCLRHost customHost;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hr = pCLR-&amp;gt;SetHostControl(&amp;amp;customHost);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; assert(SUCCEEDED(hr));&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hr = pCLR-&amp;gt;Start();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; assert(SUCCEEDED(hr));&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DWORD retcode;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hr = pCLR-&amp;gt;ExecuteInDefaultAppDomain(APP_STARTUP_EXE,&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; APP_ENTRY_TYPE, APP_ENTRY_METHOD, L"" , &amp;amp;retcode);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; assert(SUCCEEDED(hr));&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; 0;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;};&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-2712674694475545976?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=yWJjnff329E:d1plya_b7lI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=yWJjnff329E:d1plya_b7lI:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=yWJjnff329E:d1plya_b7lI:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/yWJjnff329E" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/yWJjnff329E/accurately-measuring-gc-suspensions.html</link><author>noreply@blogger.com (Liran Chen)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://i37.tinypic.com/160df77_th.png" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/08/accurately-measuring-gc-suspensions.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-3117736400671050655</guid><pubDate>Tue, 03 Aug 2010 19:31:00 +0000</pubDate><atom:updated>2010-09-11T08:54:10.968+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.Net Internals</category><category domain="http://www.blogger.com/atom/ns#">Performance</category><title>Forcing JIT Compilation During Runtime</title><description>One of the advantages/disadvantages of the .Net Framework is its usage of JIT  Compilation: The process in which machine code is generated from CIL code  during the application's run-time, at the production environment.&lt;br /&gt;
Depending on your point of view, this behavior can be considered as a helpful feature since during the JIT  compilation, the compiler  can utilize special features that the production processor supports  (such as using more efficient, unique instructions that aren't supported by other processors). This way, we could achieve better performance than if  we didn't use JIT compilation at all, since we would have to address a more basic  set of processor features (in order to support a variety of processors).&lt;br /&gt;
The most dominant disadvantage of using a JIT compiler is the time being spent  compiling CIL code on the production machine (since every time the user  triggers a "first-time" call to a managed method, the JIT compiler is  triggered, and starts to compile the method for the current and future  calls).&lt;br /&gt;
In the CLR, every &lt;i&gt;MethodDesc&lt;/i&gt; instance (a structure that exists  for every managed method. Contains some metadata about the method and is  allocated in the EE's (Execution Engine) memory), contains an &lt;i&gt;IsJitted &lt;/i&gt;boolean  field. Like the field name implies, its value tells us whether the the  JIT compiler already compiled the relevant method (this field will  assist us later on).&lt;br /&gt;
Once a method is called for the first time, the execution flow is  redirected by a special stub into the JIT, that will be responsible to  compile the method. Then, the stub that caused the redirection is overridden by a &lt;i&gt;jmp&lt;/i&gt; instruction that directs the execution flow to the JIT's generated machine code (in fact, this mechanism resembles the &lt;a href="http://www.microsoft.com/msj/1298/hood/hood1298.aspx"&gt;delay load&lt;/a&gt;  feature available in C++). So as you understand, each time a method is  called for the first time, we suffer from a certain performance penalty.&lt;br /&gt;
Its important to emphasize that usually, that the JIT's performance  penalty is quite minimal, since we only have to experience it at  the method's first invocation. However, for applications that demand  high responsiveness, this performance penalty might be intolerable.&lt;br /&gt;
&lt;br /&gt;
The classic solution for this problem is to use &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163808.aspx"&gt;NGen&lt;/a&gt;.  The idea behind NGen is that when your application is installed on the  production machine, the JIT is triggered to compile our entire set of  managed assemblies, and generate a native image for the application.  Afterwards, when the application is launched, the CLR will make sure to  load the correct native image from the disk, and so avoid unnecessary  "jitting".&lt;br /&gt;
The problem with NGen is that it's quite complicated to use, and if we  are really looking to avoid performance penalties during our  application's initialization, we will have to spend more of our valuable  time in order to register our assemblies in the GAC, correctly set our  DLL's base addresses in order to avoid &lt;a href="http://en.wikipedia.org/wiki/Rebasing"&gt;rebasing&lt;/a&gt;, etc.&lt;br /&gt;
However, &lt;b&gt;there is an alternative&lt;/b&gt; for using NGen, and it's triggering the JIT to compile your during runtime, exactly when your desire it. This solution have its own set of  disadvantages and isn't necessarily better than using NGen, but in case  you want to speed up your JIT process with minimal effort on your part,  this a solution that you will definitely want to consider.&lt;br /&gt;
&lt;br /&gt;
So heading right down to business, I'll explain how this works from the  bottom-up, so first we will understand how we are able to trigger the  JIT compiler ourselves, in an noninvasive fashion (without actually executing the method). And then I'll explain on how you can utilize  this ability in your own existing applications.&lt;br /&gt;
If so, our first step is to learn how to trigger the JIT compiler on a single, specific method. For that, we will use the &lt;a href="http://runtiehelpers.preparemethod/"&gt;&lt;i&gt;RuntimeHelpers.&lt;/i&gt;&lt;i&gt;PrepareMethod&lt;/i&gt;&lt;/a&gt; function. Normally, we would use this method when we need to call virtual functions inside a &lt;a href="http://msdn.microsoft.com/en-us/library/ms228973.aspx"&gt;CER&lt;/a&gt;  region, but in our case, what is more interesting is that this method  causes the JIT to compile the method that was passed to it (Remark: it should be  noted that calling &lt;i&gt;PrepareMethod&lt;/i&gt; may trigger the static constructor of the destination type, that is, if it has one).&lt;br /&gt;
Since in this demonstration we we'll want to JIT-compile the entire set  of methods contained in a given assembly, we will use a utility method  that iterates over the contained types in a given assembly, and invokes &lt;i&gt;PrepareMethod&lt;/i&gt; on every relevant method:&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; PreJITMethods(Assembly assembly)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Type[] types = assembly.GetTypes();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (Type curType &lt;span style="color: blue;"&gt;in&lt;/span&gt; types)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; MethodInfo[] methods = curType.GetMethods(&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; BindingFlags.DeclaredOnly |&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; BindingFlags.NonPublic |&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; BindingFlags.Public |&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; BindingFlags.Instance |&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; BindingFlags.Static);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (MethodInfo curMethod &lt;span style="color: blue;"&gt;in&lt;/span&gt; methods)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (curMethod.IsAbstract ||&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; curMethod.ContainsGenericParameters)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;continue&lt;/span&gt;;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; RuntimeHelpers.PrepareMethod(curMethod.MethodHandle);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
One important thing about this method is the placement of the &lt;i&gt;if&lt;/i&gt; statement right before the invocation of &lt;i&gt;PrepareMethod&lt;/i&gt;.  The first part of the statement is obvious, we have to ignore abstract  methods since they don't contain any implementation detail, so the JIT  compiler have nothing to do with them (attempting to pass them to &lt;i&gt;PrepareMethod&lt;/i&gt;  will result in an exception). The second part of the statement is the  ignorance of generic methods. We can't cause the JIT to compile a  generic methods since at this stage we still don't know which parameters  are passed to the method in different call sites in the application's  code. This is in fact a disadvantage versus NGen, that is aware of which "versions" of the generic method needs to be compiled.&lt;br /&gt;
&lt;br /&gt;
The next step is to integrate this utility method into our application's  initialization routine. When doing so, there are a few things to  consider. Would we rather to trigger the code generation once right  after our application finished loading? Or perhaps every time a new  assembly is loaded into the &lt;i&gt;AppDomain&lt;/i&gt;? Do we prefer executing  this code synchronously, or perhaps asynchronously? And what about  dedicating a thread with a lower priority than our other applicative  threads? This questions will have to be answered according to your own  preferences, and your application's needs.&lt;br /&gt;
Another thing to remember is that by default, the CLR delays the loading  of referenced assemblies until we reach the first time we actually  reference a type from it in our code (so if have a reference to a certain assembly, and we never reached a point where we needed to use it, it  won't be loaded into the &lt;i&gt;AppDomain&lt;/i&gt;). So if we want to trigger the  JIT compiler on all of the statically referenced assemblies, we will  have to explicitly load them. In order to do so, we could&amp;nbsp; recursively  call &lt;a href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getreferencedassemblies.aspx"&gt;&lt;i&gt;GetReferencedAssemblies&lt;/i&gt;&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.load.aspx"&gt;&lt;i&gt;Load&lt;/i&gt;&lt;/a&gt;. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;// recursively load all of assemblies referenced by the given assembly&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; ForceLoadAll(Assembly assembly)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ForceLoadAll(assembly, &lt;span style="color: blue;"&gt;new&lt;/span&gt; HashSet());&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; ForceLoadAll(Assembly assembly,&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HashSet loadedAssmblies)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; alreadyLoaded = !loadedAssmblies.Add(assembly);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (alreadyLoaded)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt;;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AssemblyName[] refrencedAssemblies =&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; assembly.GetReferencedAssemblies();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (AssemblyName curAssemblyName &lt;span style="color: blue;"&gt;in&lt;/span&gt; refrencedAssemblies)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Assembly nextAssembly = Assembly.Load(curAssemblyName);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (nextAssembly.GlobalAssemblyCache)&lt;/b&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;continue&lt;/span&gt;;&lt;/b&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ForceLoadAll(nextAssembly, loadedAssmblies);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
In this example, I've chosen to ignore assemblies that are loaded from  the GAC, so we won't compile the BCL's massive codebase (thus,  minimizing our &lt;a href="http://msdn.microsoft.com/en-us/library/ms684891%28VS.85%29.aspx"&gt;&lt;i&gt;working set&lt;/i&gt;&lt;/a&gt;  size). As you can understand, this method can be customized to fit your  specific needs regarding which assemblies to load, and which to ignore.&lt;br /&gt;
Obviously, this method will only load assemblies that are statically  referenced to our application. In order to trigger JIT compilation on  dynamically loaded assemblies (as a product of using Reflection for  instance), you may want to subscribe to the &lt;a href="http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyload.aspx"&gt;&lt;i&gt;AssemblyLoad&lt;/i&gt;&lt;/a&gt; event that will be fired during the loading of new assemblies into the &lt;i&gt;AppDomain&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
In case you are interested to know how you can positively confirm that a certain method was JIT compiled by the &lt;i&gt;PrepareMethod&lt;/i&gt; (or by some other means), then you will need to grab WinDbg and SOS.&lt;br /&gt;
All you need to do is to find the address of your method's &lt;i&gt;MethodDesc&lt;/i&gt; instance, and check what is the value of the &lt;i&gt;IsJitted&lt;/i&gt; field. Here is a typical example to how you would want to do so:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style="font-size: small;"&gt;&amp;gt; !name2ee OtherAssmebly.dll C.ClassC
Module: 00a457b8 (OtherAssmebly.dll)
Token: 0x02000002
MethodTable: &lt;b&gt;00a48bbc&lt;/b&gt;
EEClass: 00f283d4
Name: C.ClassC

&amp;gt; !dumpmt -md &lt;b&gt;00a48bbc&lt;/b&gt;
EEClass: 00f283d4
Module: 00a457b8
Name: C.ClassC
mdToken: 02000002&amp;nbsp; (C:\...\OtherAssmebly.dll)
BaseSize: 0xc
ComponentSize: 0x0
Number of IFaces in IFaceMap: 0
Slots in VTable: 6
--------------------------------------
MethodDesc Table
&amp;nbsp;&amp;nbsp; Entry MethodDesc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; JIT Name
00a4c838&amp;nbsp;&amp;nbsp; 00a48bb0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NONE C.ClassC..ctor()
&lt;b&gt;00a4c830&amp;nbsp;&amp;nbsp; 00a48ba0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;JIT&lt;/span&gt; C.ClassC.FooMethod()&lt;/b&gt;

&amp;gt; !dumpmd 00a48ba0
Method Name: C.ClassC.FooMethod()
Class: 00f283d4
MethodTable: 00a48bbc
mdToken: 06000001
Module: 00a457b8
&lt;b&gt;IsJitted: &lt;span style="color: green;"&gt;yes&lt;/span&gt;&lt;/b&gt;
CodeAddr: 00f50778&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-3117736400671050655?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=NpCyzzrhAHQ:1ZWz8GYV-m4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=NpCyzzrhAHQ:1ZWz8GYV-m4:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=NpCyzzrhAHQ:1ZWz8GYV-m4:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/NpCyzzrhAHQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/NpCyzzrhAHQ/forcing-jit-compilation-during-runtime.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>1</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/08/forcing-jit-compilation-during-runtime.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-973073404954612916</guid><pubDate>Tue, 20 Jul 2010 14:43:00 +0000</pubDate><atom:updated>2011-02-09T22:31:18.914+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.Net Internals</category><category domain="http://www.blogger.com/atom/ns#">Multithreading</category><category domain="http://www.blogger.com/atom/ns#">CLR Hosting</category><title>Monitor's Locking Primitive</title><description>Lately, a discussion in a C# user group raised the question "In which synchronization primitive does the CLR uses when I call &lt;i&gt;Monitor.Enter&lt;/i&gt;?". Does a Mutex is being created by the OS? Maybe an Event? or perhaps it's a user-mode primitive such as CriticalSection? Apparently there's some vagueness in the subject, so in this post I will demonstrate how can we find the answer to the question using the tools available to us.&lt;br /&gt;
In general, the CLR's object synchronization ability is implemented by allocating a &lt;i&gt;SyncBlock&lt;/i&gt; for every object that we attempt to lock. Looking the the object's header, the CLR can find the corresponding SyncBlock&amp;nbsp; object that belongs to that object. It's the SyncBlock's responsibility to synchronize the locking requests to that object.&lt;br /&gt;
One needs to remember that those synchronization abilities are a feature of the CLR, in the sense that they are implemented in the CLR, and not necessarily in (or using) the operating system's synchronization primitives. This matches the sense that "theoretically", a managed thread doesn't have to be based on an operating system's kernel thread. So basically, no one can guarantee that the CLR will always use one synchronization primitive or another. Today this isn't the case, and in the meanwhile it doesn't seem like things are going to change in the near or far future.&lt;br /&gt;
&lt;br /&gt;
After a quick review of the documentation available to us in the MSDN, one can impressed as if there's no real documentation about the basic primitive being used. But since we are talking about a synchronization primitive, we can remember that &lt;i&gt;IHostSyncManager&lt;/i&gt; interface that is exposed to us by the CLR's hosting ability. One of this interface's functionalities is the ability to replace the implementation of the synchronization primitive being used by the &lt;i&gt;Monitor&lt;/i&gt; class. This ability is exposed by the method &lt;i&gt;CreateMonitorEvent&lt;/i&gt;.&lt;br /&gt;
Even at this stage we may pay attention to what is being said under the remarks paragraph:&lt;br /&gt;
&lt;blockquote&gt;&lt;i&gt;CreateMonitorEvent returns an IHostAutoEvent&amp;nbsp; that the CLR uses in  its implementation of the managed System.Threading.Monitor type. This  method mirrors the Win32 CreateEvent function, with a value of false  specified for the bManualReset parameter.&lt;/i&gt;       &lt;/blockquote&gt;Even though, the keyword here is "Mirrors" so there isn't a true guarantee about what is happening in the CLR's internal implementation. In order to verify the thick hint we've just received here, we are going to have to pull out the big guns, and use WinDbg.&lt;br /&gt;
In favor of the test, I wrote up an application that results in an endless contention:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Main()      &lt;br /&gt;
{      &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Thread t1 = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Thread(() =&amp;gt; { &lt;span style="color: blue;"&gt;lock&lt;/span&gt; ("A") { &lt;span style="color: blue;"&gt;while&lt;/span&gt; (&lt;span style="color: blue;"&gt;true&lt;/span&gt;);} });      &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t1.Start();      &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;lock&lt;/span&gt; ("A") { &lt;span style="color: blue;"&gt;while&lt;/span&gt; (&lt;span style="color: blue;"&gt;true&lt;/span&gt;);}       &lt;br /&gt;
}      &lt;br /&gt;
&lt;br /&gt;
After the application already runs in the background, we could launch WinDbg and attach the appropriate process.&lt;br /&gt;
After loading SOS, our first step will be to find the thread that lost the race for acquiring the lock. To do so, we will print the managed stacks of all of our threads, and when we'll find a "suitable" stack trace, we'll move to its context:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;gt;~*e!clrstack&lt;/b&gt; &lt;span style="color: green;"&gt;// execute !clrstack on all of the threads&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: green;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;OS Thread Id: 0xf80 (0)          &lt;/span&gt;Child SP IP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Call Site         &lt;br /&gt;
0012f3ec 030300fe ConsoleApplication1.Program.Main(System.String[]) [...]         &lt;br /&gt;
0012f648 791421db [GCFrame: 0012f648]         &lt;br /&gt;
&lt;span style="color: blue;"&gt;OS Thread Id: 0xf4c (1)&lt;/span&gt;         &lt;br /&gt;
Unable to walk the managed stack. The current thread is likely not a         &lt;br /&gt;
managed thread. You can run !threads to get a list of managed threads in         &lt;br /&gt;
the process         &lt;br /&gt;
&lt;span style="color: blue;"&gt;OS Thread Id: 0x840 (2)&lt;/span&gt;         &lt;br /&gt;
Child SP IP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Call Site         &lt;br /&gt;
02ccfe68 7c90e514 [DebuggerU2MCatchHandlerFrame: 02ccfe68]         &lt;br /&gt;
&lt;span style="color: blue;"&gt;OS Thread Id: 0xbe0 (3)&lt;/span&gt;         &lt;br /&gt;
Child SP IP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Call Site         &lt;br /&gt;
0313f67c 7c90e514 [GCFrame: 0313f67c]         &lt;br /&gt;
0313f794 7c90e514 [GCFrame: 0313f794]         &lt;br /&gt;
0313f7b0 7c90e514 [HelperMethodFrame_1OBJ: 0313f7b0] System.Threading.Monitor.ReliableEnter(System.Object, Boolean ByRef)           &lt;br /&gt;
&lt;span style="color: red;"&gt;0313f808 79b2e0c4 System.Threading.Monitor. Enter(System.Object, Boolean ByRef) &lt;/span&gt;        &lt;br /&gt;
0313f818 03030163 ConsoleApplication1.Program.&amp;lt; Main&amp;gt;b__3() [...]         &lt;br /&gt;
0313f848 79b2ae5b System.Threading.ThreadHelper.ThreadStart_Context(System.Object)         &lt;br /&gt;
0313f858 79ab7ff4 System.Threading.  ExecutionContext.Run(System.Threading.ExecutionContext,  System.Threading.ContextCallback, System.Object, Boolean)         &lt;br /&gt;
0313f87c 79ab7f34 System.Threading.  ExecutionContext.Run(System.Threading.ExecutionContext,  System.Threading.ContextCallback, System.Object)         &lt;br /&gt;
0313f898 79b2ade8 System.Threading.ThreadHelper. ThreadStart()         &lt;br /&gt;
0313fabc 791421db [GCFrame: 0313fabc]         &lt;br /&gt;
0313fd80 791421db [DebuggerU2MCatchHandlerFrame: 0313fd80]         &lt;br /&gt;
&lt;span style="color: blue;"&gt;OS Thread Id: 0xe60 (4)&lt;/span&gt;         &lt;br /&gt;
Unable to walk the managed stack. The current thread is likely not a         &lt;br /&gt;
managed thread. You can run !threads to get a list of managed threads in         &lt;br /&gt;
the process         &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;gt;~3s&lt;/b&gt; &lt;span style="color: green;"&gt;// change the current thread to 3&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
After finding the correct thread, we will have to check its native stack, so we'll use the &lt;i&gt;kb&lt;/i&gt; commend that will also display the first 3 parameters that were passed to each method:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;gt;kb&lt;/b&gt;         &lt;br /&gt;
ChildEBP RetAddr&amp;nbsp; Args to Child&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
0313f3c8 7c90df4a 7c809590 00000001 0313f3f4 ntdll!KiFastSystemCallRet         &lt;br /&gt;
0313f3cc 7c809590 00000001 0313f3f4 00000001 ntdll!ZwWaitForMultipleObjects+0xc        &lt;br /&gt;
&lt;span style="color: red;"&gt;0313f468 791f516a 00000001 001820bc 00000000 KERNEL32!WaitForMultipleObjectsEx+0x12c&lt;/span&gt;         &lt;br /&gt;
0313f4cc 791f4f98 00000001 001820bc 00000000 clr!WaitForMultipleObjectsEx_SO_TOLERANT+0x56         &lt;br /&gt;
0313f4ec 791f4dd8 00000001 001820bc 00000000 clr!Thread::DoAppropriateAptStateWait+0x4d         &lt;br /&gt;
0313f580 791f4e99 00000001 001820bc 00000000 clr!Thread::DoAppropriateWaitWorker+0x17d         &lt;br /&gt;
0313f5ec 791f4f17 00000001 001820bc 00000000 clr!Thread::DoAppropriateWait+0x60         &lt;br /&gt;
0313f640 7919d409 ffffffff 00000001 00000000 clr!CLREvent::WaitEx+0x106         &lt;br /&gt;
0313f654 792e0160 ffffffff 00000001 00000000 clr!CLREvent::Wait+0x19         &lt;br /&gt;
0313f6e4 792e0256 001818a0 ffffffff 8079c412 clr!AwareLock::EnterEpilogHelper+0xa8         &lt;br /&gt;
0313f724 792e029b 001818a0 001818a0 79142c0d clr!AwareLock::EnterEpilog+0x42         &lt;br /&gt;
0313f744 792c7729 8079cb36 0313f830 00b3c368 clr!AwareLock::Enter+0x5f         &lt;br /&gt;
0313f800 79b2e0c4 79161f8e 00941f02 0313f840 clr!JIT_MonReliableEnter_Portable+0x104         &lt;br /&gt;
0313f840 79b2ae5b 00b3c3ec 01b3101c 0313f86c mscorlib_ni+0x2ae0c4         &lt;br /&gt;
0313f850 79ab7ff4 00b3e010 00000000 00b3c3b8 mscorlib_ni+0x2aae5b         &lt;br /&gt;
0313f86c 79ab7f34 00000000 00b3c3b8 00000000 mscorlib_ni+0x237ff4         &lt;br /&gt;
0313f88c 79b2ade8 00b3c3b8 00000000 001818a0 mscorlib_ni+0x237f34         &lt;br /&gt;
0313f8a4 791421db 000001a7 0313fae0 0313f930 mscorlib_ni+0x2aade8         &lt;br /&gt;
0313f8b4 79164a2a 0313f980 00000000 0313f950 clr!CallDescrWorker+0x33         &lt;br /&gt;
0313f930 79164bcc 0313f980 00000000 0313f950 clr!CallDescrWorkerWithHandler+0x8e         &lt;br /&gt;
&lt;br /&gt;
At this point we can already see that the last thing that the thread did before we've interrupted him, is to call to &lt;i&gt;WaitForMultipleObjectsEx&lt;/i&gt; where the first parameter was &lt;i&gt;1&lt;/i&gt; and the second is &lt;i&gt;0x001820BC&lt;/i&gt;. Having this information, we can understand that we are waiting on a single Handle object, since the first parameter specifies the affective size of the array that was passed as the second parameter. So all we've got left to do is to understand which object hides behind that Handle that was passed to the function.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;gt;dp 0x001820BC 0x001820BC&lt;/b&gt;         &lt;br /&gt;
001820bc&amp;nbsp; &lt;span style="color: red;"&gt;000006c8&lt;/span&gt;&lt;span style="color: green;"&gt; // our handle's value          &lt;/span&gt;        &lt;br /&gt;
&lt;b&gt;&amp;gt;!handle 000006c8 F&lt;/b&gt;&lt;span style="color: green;"&gt; // pass "F" as bitmask to display all of the relevant data&lt;/span&gt;         &lt;br /&gt;
Handle 6c8         &lt;br /&gt;
&amp;nbsp; Type&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;Event&lt;/b&gt;         &lt;br /&gt;
&amp;nbsp; Attributes&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0         &lt;br /&gt;
&amp;nbsp; GrantedAccess&amp;nbsp;&amp;nbsp;&amp;nbsp; 0x1f0003:         &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Delete,ReadControl,WriteDac,WriteOwner,Synch         &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; QueryState,ModifyState         &lt;br /&gt;
&amp;nbsp; HandleCount&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2         &lt;br /&gt;
&amp;nbsp; PointerCount&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4         &lt;br /&gt;
&amp;nbsp; Name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;none&gt;         &lt;br /&gt;
&amp;nbsp; Object Specific Information         &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Event Type &lt;b&gt;Auto Reset&lt;/b&gt;         &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Event is Waiting         &lt;/none&gt;&lt;br /&gt;
&lt;br /&gt;
And so, this was our last step. We have confirmed that &lt;i&gt;Monitor&lt;/i&gt;'s synchronization primitive is in fact an Event object of an AutoReset type.&lt;br /&gt;
Whoever still wants to view the creation and usage of the Even in code, can open up the &lt;i&gt;sync.cpp&lt;/i&gt; file under the SSCLI's implementation, and see how a call to &lt;i&gt;CLREvent::CreateMonitorEvent&lt;/i&gt; triggers a call to &lt;i&gt;UnsafeCreateEvent&lt;/i&gt; (that actually is a typedef to the familiar &lt;i&gt;CreateEvent&lt;/i&gt; function).&lt;br /&gt;
&lt;br /&gt;
Even so, one have to remember that this is only a partial answer. Since as I've mentioned at the beginning of this post, there's no guarantee that once we'll call &lt;i&gt;Monitor.Enter&lt;/i&gt; we will always find ourselves accessing some kernel object. In fact, in one of his posts, &lt;a href="http://www.bluebytesoftware.com/blog/Default.aspx"&gt;Joe Duffy&lt;/a&gt; makes sure to mention that in the CLR's implementation, when a thread encounters contention, it will attempt to spin a little before re-trying to acquire the lock, without leaving the user-mode and waiting for some kernel object. So even if the CLR doesn't give a full blown implementation of a synchronization primitive, it may still provide some optimizations over the supplied services of the operating system (much like the &lt;i&gt;CriticalSection&lt;/i&gt; type).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-973073404954612916?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=imKDbXkPNck:UZ8PFSv8CRk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=imKDbXkPNck:UZ8PFSv8CRk:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=imKDbXkPNck:UZ8PFSv8CRk:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/imKDbXkPNck" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/imKDbXkPNck/monitors-locking-primitive.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/07/monitors-locking-primitive.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-1296694455456656159</guid><pubDate>Mon, 05 Jul 2010 13:48:00 +0000</pubDate><atom:updated>2011-08-14T23:49:45.143+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.Net Internals</category><title>Behind The .locals init Flag</title><description>As we all probably know, the C# language specification demands that every declared local variable will be assigned-to before its usage.&lt;br /&gt;
Even so, whoever used ILDASM to peek into the compiler's generated IL code, must have noticed that right after the deceleration for a method, there's some peculiar line that starts with &lt;i&gt;.local init&lt;/i&gt;. Actually something like that:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;.method private hidebysig static void&amp;nbsp; Main(string[] args) cil managed
{
&amp;nbsp; .entrypoint
&amp;nbsp; // Code size&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10 (0xa)
&amp;nbsp; .maxstack&amp;nbsp; 1
&amp;nbsp;&lt;b&gt; .locals init ([0] int32 x) &amp;lt;--- &lt;i&gt;localsinit flag&lt;/i&gt;&lt;/b&gt; 
&amp;nbsp; IL_0000:&amp;nbsp; ldc.i4.4
&amp;nbsp; IL_0001:&amp;nbsp; stloc.0
&amp;nbsp; IL_0002:&amp;nbsp; ldloc.0
&amp;nbsp; IL_0003:&amp;nbsp; call&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void [mscorlib]System.Console::WriteLine(int32) 
}&lt;/pre&gt;&lt;br /&gt;
This line represents the existence of the CorILMethod_InitLocals flag in the current method's header. This flag effectively guarantees that the CLR will initialize all of the local variables declared in the method's body to they're default values. Meaning, regardless to which default value you have chosen to set your local variable to (in our case, the variable &lt;i&gt;x&lt;/i&gt; gets the value of 4), the platform will always make sure that before our code will execute, the local variable &lt;i&gt;x&lt;/i&gt; will necesserily be initialized to its legal, default value (in this case, 0).&lt;br /&gt;
&lt;br /&gt;
In Microsoft's implementation of the CLI, this flag always exists in the method's header (assuming there are local variables declared in its body). This could make us wonder why would the compiler insist on reporting an error every time a programmer forgets the initialize its local variables before using them. This constraint held by the compiler may seem redundant, but in fact, there's several reason to why it is quite necessary.&lt;br /&gt;
&lt;br /&gt;
Before diving into the meaning of the &lt;i&gt;.locals init&lt;/i&gt; flag, let's review again the issue of the so called "duplicate assignment" performed by the compiler and the CLR.&lt;br /&gt;
Looking at the IL code from before, one may think that every time we declare a local variable we have to pay some "initialization overhead" since both the compiler and the CLR insist on initializing it to some "default value". Even though this overhead is quite minimal, it still gives us some "bad vibe" since it just seems redundent.&lt;br /&gt;
But in fact, this duplicate assignment never really occurs. The reason for that lays inside the way that the &lt;i&gt;.locals init&lt;/i&gt; flag guarantees the default values assignment. All it does, is to make sure the JIT compiler will generate code that will initialize the variable before its usage. In our case, the JIT will have to generate a &lt;i&gt;mov&lt;/i&gt; instruction that will set our &lt;i&gt;x&lt;/i&gt; variable to 0.&lt;br /&gt;
And so, the assembler code we get during run-time (without using optimizations), confirms it:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Normal JIT generated code
ConsoleApplication4.Program.Main(System.String[])
Begin 00e20070, size 30
00E20070 push&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ebp
00E20071 mov&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ebp,esp
00E20073 sub&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; esp,8
00E20076 mov&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dword ptr [ebp-4],ecx
00E20079 cmp&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dword ptr ds:[00942E14h],0
00E20080 je&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 00E20087
00E20082 call&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7A0CA6C1 (JitHelp: CORINFO_HELP_DBG_IS_JUST_MY_CODE)

-------------------- &lt;i&gt;Generated code due to the LocalsInit flag&lt;/i&gt;&amp;nbsp; ----------------
&lt;b&gt;00E20087 xor&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; edx,edx&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;// zero out the EDX register&lt;/b&gt;
&lt;b&gt;00E20089 mov&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dword ptr [ebp-8],edx // assign the value of EDX to the location of 'X'&lt;/b&gt;

--------------------- &lt;i&gt;Our own application's code&lt;/i&gt; ---------------------------------
&lt;b&gt;00E2008C mov&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dword ptr [ebp-8],4&lt;/b&gt;&amp;nbsp;&amp;nbsp; &lt;b&gt;// assign the value 4 to the location of 'X'&lt;/b&gt;

00E20093 mov&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ecx,dword ptr [ebp-8]
00E20096 call&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 79793E74 (System.Console.WriteLine(Int32), mdToken: 060007c3)
00E2009B nop
00E2009C mov&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; esp,ebp
00E2009E pop&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ebp
00E2009F ret&lt;/pre&gt;&lt;br /&gt;
If so, in this code example one can defenitly see the effect of the &lt;i&gt;localsInit&lt;/i&gt; flag on the generated assembler code. And also, we can see the existence of the "duplicate-assignment" phenomena as mentioned before.&lt;br /&gt;
However, one should remember that this code was generated without any usage of JIT optimizations. Once we will allow these optimizations, we would see that the JIT is able to identify the duplicate assignment, and treats it as &lt;a href="http://en.wikipedia.org/wiki/Dead_code"&gt;dead code&lt;/a&gt; since it has no effect on the variable. As a result, the first assignment is completely removed, and only the user's value initialization appears in the generated code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Normal JIT generated code
ConsoleApplication4.Program.Main(System.String[])
Begin 00c80070, size 19
00c80070 push&amp;nbsp;&amp;nbsp;&amp;nbsp; ebp
00c80071 mov&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ebp,esp
00c80073 call&amp;nbsp;&amp;nbsp;&amp;nbsp; mscorlib_ni+0x22d2f0 (792ed2f0) (System.Console.get_Out(), mdToken: 06000772)
00c80078 mov&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ecx,eax
&lt;b&gt;00c8007a mov&amp;nbsp;&amp;nbsp; edx,4&amp;nbsp; // assign 4 to the "virtual representation" of X&lt;/b&gt; 
00c8007f mov&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; eax,dword ptr [ecx]
00c80081 call&amp;nbsp;&amp;nbsp;&amp;nbsp; dword ptr [eax+0BCh]&lt;/pre&gt;&lt;br /&gt;
The first thing we notice is that we don't have a representation of &lt;i&gt;x&lt;/i&gt; in our application's main memory. Instead, there's a CPU register that holds its "virtual" representation. But more importantly, we can now see that there's no remnant of the previous duplicate assignment we've witnessed due to the usage of the &lt;i&gt;localsInit&lt;/i&gt; flag.&lt;br /&gt;
&lt;br /&gt;
Now, we can check what in fact is the meaning behind the usage of &lt;i&gt;localsInit&lt;/i&gt; and the compilers constraint for initializing local variables before they're usage.&lt;br /&gt;
Microsoft's argument regarding using the &lt;i&gt;Definite Assignment&lt;/i&gt; mechanism is that most of the times programmers don't to initialize they're local variables are cause by logical bugs, and not by the fact that the programmer relies on its compiler to set the variable to its default value. In one of &lt;a href="http://blogs.msdn.com/ericlippert/default.aspx"&gt;Eric Lippert's&lt;/a&gt; comments on his blog, he says it himself:&lt;br /&gt;
&lt;blockquote&gt;&lt;i&gt;"The reason we require definite assignment is because failure to definitely assign a local is probably a bug. We do not want to detect and then silently ignore your bug! We tell you so that you can fix it." &lt;/i&gt;&lt;/blockquote&gt;The importance of the &lt;i&gt;localsInit&lt;/i&gt; flag, can be summarized in one word: &lt;b&gt;Verification&lt;/b&gt;.&lt;br /&gt;
Verification is the process in which the CLI makes sure that all of the CIL code that exit in the application is "safe". This includes making sure that all of that methods we use are able the receive the number of parameters we pass them, the parameter's types, that all of the local variables are properly initialized, etc.&lt;br /&gt;
In case the CLR detects a piece of code that fails the verification process, a &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.verificationexception.aspx"&gt;VerficationException&lt;/a&gt; will be thrown.&lt;br /&gt;
Nontheless, not every IL code needs to be verifiable, as mentioned in Partition III of the standard:&lt;br /&gt;
&lt;blockquote&gt;&lt;i&gt;"It is perfectly acceptable to generate correct CIL code that is not verifiable, but which is known to be memory safe by the compiler writer. Thus, correct CIL&amp;nbsp; might not be verifiable, even though the producing compiler might know that it is memory safe."&lt;/i&gt;&lt;/blockquote&gt;Having said that, every time we write some unverifiable code, we have to update the proper permissions using the &lt;i&gt;SecurityPermissionAttribute&lt;/i&gt;, and explicitly tell the CLR not the perform verification on our code using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.permissions.securitypermissionattribute.skipverification.aspx"&gt;&lt;i&gt;SkipVerfication&lt;/i&gt;&lt;/a&gt; property (the CLR won't perform definite assignment analysis on our code). One of the most common times in which we would want to this is when we write unsafe code. In such cases, we will have to explicitly mark the required check-box in the project's properties, thus, allowing to compiler to compile our unsafe code blocks, and making it to add the &lt;i&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.security.unverifiablecodeattribute.aspx"&gt;UnverifiableCodeAttribute&lt;/a&gt; &lt;/i&gt;to the generated assembly, telling the CLR that this module is unverifiable.&lt;br /&gt;
&lt;br /&gt;
The verification process requires that every local variable will be initialized. To be exact, it requires that in case no one requested to skip over the verification process, the &lt;i&gt;localsInit&lt;/i&gt; flag must be present in the generated IL. For this reason, looking at the CIL instructions reference, you may encounter remarks such as this:&lt;br /&gt;
&lt;blockquote&gt;&lt;i&gt;"Local variables are initialized to 0 before entering the method only if the &lt;b&gt;localsinit &lt;/b&gt;on the method is true (see Partition I) ... System.VerificationException is thrown if the the &lt;b&gt;localsinit &lt;/b&gt;bit for this method has not been set, and the assembly containing this method has not been granted &lt;br /&gt;
System.Security.Permissions.SecurityPermission.SkipVerification (and the CIL does not perform automatic definite-assignment analysis) "&lt;/i&gt;&lt;/blockquote&gt;Later on, the document address the overhead issue of performing the definite assignment analysis during runtime:&lt;br /&gt;
&lt;blockquote&gt;&lt;i&gt;"Performance measurements on C++ implementations (which do not require definite-assignment analysis) indicate that adding this requirement has almost no impact, even in highly optimized code. Furthermore, customers incorrectly attribute bugs to the compiler when this zeroing is not performed, since such code often fails when small, unrelated changes are made to the program."&lt;/i&gt;&lt;/blockquote&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-1296694455456656159?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=mItLJXiWQDI:vs_zJN4wFlc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=mItLJXiWQDI:vs_zJN4wFlc:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=mItLJXiWQDI:vs_zJN4wFlc:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/mItLJXiWQDI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/mItLJXiWQDI/behind-locals-init-flag.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/07/behind-locals-init-flag.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-8182489882696801167</guid><pubDate>Mon, 28 Jun 2010 10:00:00 +0000</pubDate><atom:updated>2010-07-31T15:41:55.653+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.Net Internals</category><category domain="http://www.blogger.com/atom/ns#">Performance</category><title>String.Format Isn't Suitable for Intensive Logging</title><description>One of the first rules that every .Net programmer learns to memorize is "Never use the + operator to combine strings. Only use the &lt;i&gt;StringBuilder&lt;/i&gt; class to do so". If you'll ask a random programmer why should we do so, the typical answer would be "Since strings in .Net are immutable, every time we use the + operator we actually cause a memory allocation, so in order to avoid we should use the &lt;i&gt;StringBuilder&lt;/i&gt;".&lt;br /&gt;
This answer is a common misconception. Does &lt;i&gt;StringBuilder&lt;/i&gt; is necessarily better than using the + operator, or perhaps other means for combining strings? No, not necessarily.&lt;br /&gt;
&lt;br /&gt;
First of all, let's look at the + operator's behavior. Basically, what it does is to allocate a new string, and copy the characters from the two older strings into the new one. So theoretically, there's a serious negative impact on memory usage, since when combining multiple strings, we could find ourselves allocation redundant strings that will only be used to contain "parts" of targeted final string.&lt;br /&gt;
This is only partialy true, since the C# compiler will detect combinations of 5 strings and above using the + operator, and will convert it to use &lt;i&gt;string.Concat&lt;/i&gt;.&lt;br /&gt;
For example, this code:&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt; str2 = 1.ToString() + 2.ToString() + 3.ToString() + 4.ToString() + 5.ToString();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
Will be compiled into this code:&lt;br /&gt;
&lt;br /&gt;
&lt;div align="left" dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] CS$0$0000 = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;[] { 1, 2, 3, 4, 5 }; // edit: removed ToString&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt; text1 = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Concat(CS$0$0000);&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
Regarding &lt;i&gt;string.Concat&lt;/i&gt;, you shouldn't be worried about excessive memory allocations, since the first thing the method does is to calculate the combined string's total length, creates the string using a call to &lt;i&gt;AllocateFastString&lt;/i&gt;, and then simply copies the content of the old strings to the new one using &lt;i&gt;wstrcpy&lt;/i&gt; (efficient raw bytes copying using unsafe code).&lt;br /&gt;
&lt;br /&gt;
One of the more common usages for combining strings, is in log formatting. In case your application does lots of intensive logging, then there's a good chance that it's spending some serious amounts of time in formatting and combining strings that will eventually arrive to the logging framework.&lt;br /&gt;
The situation might change a little depending on which logging framework you use, but I'll demonstrate using log4net. The case in log4net is that you may choose to use the simple logging methods without formatting (e.g, Deubg/Info/Warn..), or, you may also choose to use the ones that support formatting (e.g, DebugFormat/InfoFormat/WarnFormat..). While the first option receives a parameterless string, the other receives a formatted string, with additional paramters that should be placed inside the string, using &lt;i&gt;string.Format&lt;/i&gt;. Now this could introduce a problem.&lt;br /&gt;
The issue here is that every time we would like to insert some parameter into our log, we are most likely to use log4net's &lt;i&gt;Format&lt;/i&gt; method (that uses &lt;i&gt;string.Format&lt;/i&gt;) instead of using &lt;i&gt;string.Concat &lt;/i&gt;or perhaps &lt;i&gt;string.Join&lt;/i&gt;. Just because this API seems more convenient, and approachable.&lt;br /&gt;
The following benchmark demonstrates the differences between the three alternatives:&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;div align="left" dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;while&lt;/span&gt;(&lt;span style="color: blue;"&gt;true&lt;/span&gt;)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;{&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Stopwatch sw = Stopwatch.StartNew();&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;for&lt;/span&gt; (&lt;span style="color: blue;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 500000; i++)&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// 960ms&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; str1 = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format("{0}, {1}, {2}, {3}, {4}", 1, 2, 3, 4, 5);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// 665ms&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; str2 = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Concat(1, ", ", 2, ", ",3, ", ", 4, ", ", 5);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// 566ms&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; str3 = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Join(", ", &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;[] { 1, 2, 3, 4, 5 });&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(sw.ElapsedMilliseconds);&lt;/div&gt;&lt;div style="margin: 0px;"&gt;}&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;In this case, even though I've use a fairly simple log, the differences between the methods are definite. When comparing the performance of &lt;i&gt;string.Format&lt;/i&gt; to &lt;i&gt;string.Concat&lt;/i&gt;, we see that we achieved a 31% of improvement. And in comparison to &lt;i&gt;string.Join&lt;/i&gt;, we achieve a substantial 62% of performance gain (&lt;i&gt;string.Join&lt;/i&gt;'s implementation doesn't use &lt;i&gt;string.Concat&lt;/i&gt; under the covers, but works in a similar way by calling &lt;i&gt;FastAllocateString&lt;/i&gt; and using the &lt;i&gt;UnSafeCharBuffer&lt;/i&gt; class to construct the new string).&lt;br /&gt;
Don't think that this performance improvement comes with the cost of more intensive memory usage, since in this area, &lt;i&gt;Concat&lt;/i&gt; and &lt;i&gt;Join&lt;/i&gt; still achieve much better results than &lt;i&gt;string.Format&lt;/i&gt;.&lt;br /&gt;
These results dosn't mean that &lt;i&gt;StringBuilder&lt;/i&gt; &lt;i&gt;&lt;/i&gt;will always produce lesser performance, since in cases in which you don't always know the number of concatenations you will need to perform, or perhaps in cases in which you could optimize memory allocations by modifying the &lt;i&gt;capacity&lt;/i&gt; variable in the &lt;i&gt;StringBuilder&lt;/i&gt;'s constructor, you could actually achieve better results than these.&lt;br /&gt;
&lt;br /&gt;
Of course that if you perform your logs only "once in a while", this performance difference is almost meaningless. And the fact that using &lt;i&gt;FooFormat&lt;/i&gt; might be more comfortable to us might be just enough to justify its usage. But, in cases in which your application performs some intensice logging, these performance differences might be substantial for you.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-8182489882696801167?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=fwXEjT8RTO0:1u57Q1-5WbM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=fwXEjT8RTO0:1u57Q1-5WbM:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=fwXEjT8RTO0:1u57Q1-5WbM:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/fwXEjT8RTO0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/fwXEjT8RTO0/stringformat-isnt-suitable-for.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/07/stringformat-isnt-suitable-for.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-7093824260811525050</guid><pubDate>Sun, 13 Jun 2010 09:13:00 +0000</pubDate><atom:updated>2010-08-11T00:11:12.013+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Development</category><category domain="http://www.blogger.com/atom/ns#">.Net Internals</category><title>Headaches with Prefix and Temporary Variables</title><description>No less than 6 years ago, Luca Bolognese wrote a &lt;a href="http://blogs.msdn.com/b/lucabol/archive/2004/08/31/223580.aspx"&gt;post&lt;/a&gt; that dealt with an issue raised in the C# User Group. The question was "What should be the value of 'x' after executing the following lines of code?"&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt; x = 3;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;x += x++;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;If we're aware of the differences between Prefix and Postfix, then we should realize that the value of &lt;i&gt;x&lt;/i&gt; will be 6 since the last increment is redundant. In fact, we could simplify the expression by writing &lt;i&gt;x = x + x&lt;/i&gt;, since we'll still get the same result (It's important to pay attention and understand that this is the case specifically for C#. In C++ for example, the behavior of this expression is undefined).&lt;br /&gt;
&lt;br /&gt;
So after we've passed this starting point, allow me to take one step forward and suggest the following, highly readable, lines of code:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt; x = 10;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;x = --x + x + --x;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;This is the point in which things get a little more interesting. What would be the value of &lt;i&gt;x&lt;/i&gt; after execution? This expression can be quite confusing, so I suggest taking a couple of minutes to make sure you're not missing anything.&lt;br /&gt;
Ready? If your answer was "26", then you're correct. Why? well, there's a subtle hint in the post's header.&lt;br /&gt;
&lt;br /&gt;
What's confusing about this expression is that we always need to keep track of where our variable's value is stored. Whether its in the main memory? a CPU register? or perhaps in a totally different location? In fact, during execution, the program actually reserves 4 bytes on the thread's stack to keep the result of part of the computation.&lt;br /&gt;
In the first step, we decrement &lt;i&gt;x&lt;/i&gt;'s value by 1, and update it's value in main memory (could effectively be in the CPU cache, but this part isn't relevant to our case). At this point, x's value is set to 9. Next, we multiply x's value by 2 (x+x) and store the resulting value on a new variable located on the stack. Do not confuse, we &lt;b&gt;don't&lt;/b&gt; update the value of the original &lt;i&gt;x&lt;/i&gt; variable at this time. Afterwords, we decrement &lt;i&gt;x&lt;/i&gt;'s value again by 1 (now &lt;i&gt;x&lt;/i&gt; is set to 8), then then combine it to the value of the temporary variable. The result of this last computation goes to &lt;i&gt;x&lt;/i&gt;. So by the end of this single line of code, &lt;i&gt;x &lt;/i&gt;is set to 26.&lt;br /&gt;
&lt;br /&gt;
Looking at the generated assembler code, things might look a bit clearer:&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;div dir="ltr" style="text-align: left;"&gt;&lt;div style="-moz-background-inline-policy: continuous; background: none repeat scroll 0% 0% white; color: black; font-family: Courier New; font-size: 10pt;"&gt;&lt;div style="margin: 0px;"&gt;00000019&amp;nbsp; mov&amp;nbsp; dword ptr [ebp-4],0Ah&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// &lt;span style="color: red;"&gt;&lt;b&gt;x = 10&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;00000020&amp;nbsp; dec&amp;nbsp; dword ptr [ebp-4]&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// &lt;span style="color: red;"&gt;&lt;b&gt;x = 9&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;00000023&amp;nbsp; mov&amp;nbsp; eax, dword ptr [ebp-4]&amp;nbsp; &lt;span style="color: green;"&gt;// x = 9, &lt;span style="color: red;"&gt;&lt;b&gt;eax = 9&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;00000026&amp;nbsp; add&amp;nbsp; eax, dword ptr [ebp-4]&amp;nbsp; &lt;span style="color: green;"&gt;// x = 9, &lt;span style="color: red;"&gt;&lt;b&gt;eax = 18&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;00000029&amp;nbsp; mov&amp;nbsp; dword ptr [ebp-8],eax&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// x = 9, eax = 18, &lt;span style="color: red;"&gt;&lt;b&gt;temp = 18&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;0000002c&amp;nbsp; dec&amp;nbsp; dword ptr [ebp-4]&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// &lt;span style="color: red;"&gt;&lt;b&gt;x = 8&lt;/b&gt;&lt;/span&gt;, eax = 18, temp = 18&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;0000002f&amp;nbsp; mov&amp;nbsp; eax, dword ptr [ebp-8]&amp;nbsp; &lt;span style="color: green;"&gt;// x = 8, eax = 18, temp = 18&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;00000032&amp;nbsp; add&amp;nbsp; dword ptr [ebp-4],eax&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// &lt;span style="color: red;"&gt;&lt;b&gt;x = 26&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-7093824260811525050?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=ff_kC5meHk0:fv1sZSnXLgI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=ff_kC5meHk0:fv1sZSnXLgI:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=ff_kC5meHk0:fv1sZSnXLgI:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/ff_kC5meHk0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/ff_kC5meHk0/headaches-with-prefix-and-temporary.html</link><author>noreply@blogger.com (Liran Chen)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/07/headaches-with-prefix-and-temporary.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1501586010252802469.post-2366392736586325104</guid><pubDate>Wed, 02 Jun 2010 08:46:00 +0000</pubDate><atom:updated>2010-09-27T20:15:12.482+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.Net Internals</category><category domain="http://www.blogger.com/atom/ns#">Performance</category><title>DateTime.Now Causes Boxing</title><description>&lt;div style="background-color: #f3f3f3;"&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Update 2.9.2010&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: #f3f3f3;"&gt;The content of this post regards &lt;i&gt;DateTime.Now's&lt;/i&gt; implementation until v3.5. At the release of v4.0, its internal implementation was updated to avoid unnecessary boxing.&lt;/div&gt;&lt;div style="background-color: #f3f3f3;"&gt;&lt;b&gt;However&lt;/b&gt;, looking into the new implementation details revealead a new source for dynamic memory allocations in &lt;i&gt;DateTime.Now. &lt;/i&gt;The issue is discussed in detail in the updated post: &lt;a href="http://blog.liranchen.com/2010/09/datetimenow-in-v40-causes-dynamic.html"&gt;&lt;i&gt;DateTime.Now in v4.0 Causes Dynamic Memory Allocations&lt;/i&gt;&lt;/a&gt;.&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Perhaps you weren't aware of it, but every time you access &lt;i&gt;DateTime.Now&lt;/i&gt;, you cause to a dynamic memory allocation through the boxing of an Int32 variable.&lt;br /&gt;
The reason for this lays deep inside the implementation of the&lt;i&gt; Now&lt;/i&gt; property. If we will look a bit closer using Reflector, we could see that it actually wraps a call to &lt;i&gt;UtcNow&lt;/i&gt; and then converts it to local time using the &lt;i&gt;ToLocalTime&lt;/i&gt; method.&lt;br /&gt;
While there's nothing wrong with the call to &lt;i&gt;UtcNow&lt;/i&gt; (it just wraps a call to the Win32 &lt;a href="http://msdn.microsoft.com/en-us/library/ms724397%28VS.85%29.aspx"&gt;&lt;i&gt;GetSystemTimeAsFileTime&lt;/i&gt;&lt;/a&gt;), there is some trouble with &lt;i&gt;ToLocalTime&lt;/i&gt;, or actually, with the class &lt;i&gt;CurrentSystemTimeZone &lt;/i&gt;that is being used. During the conversion the method &lt;i&gt;GetDaylightChanges&lt;/i&gt; is called that causes the boxing. You can look for yourself:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="font-family: consolas;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;DaylightTime&amp;nbsp;GetDaylightChanges(&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;year)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&amp;nbsp;key&amp;nbsp;=&amp;nbsp;year;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(!&lt;span style="color: blue;"&gt;this&lt;/span&gt;.m_CachedDaylightChanges.Contains(key))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//&amp;nbsp;..lots&amp;nbsp;of&amp;nbsp;code&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;(DaylightTime)&lt;span style="color: blue;"&gt;this&lt;/span&gt;.m_CachedDaylightChanges[key];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/pre&gt;&lt;span style="font-size: small;"&gt;&lt;/span&gt;&lt;br /&gt;
This is a good example showing that even in latest versions of the .Net Framework, some types still uses non-generic data structures, from the days of 1.0/1.1. In this case, the data member &lt;i&gt;m_CachedDaylightChanges&lt;/i&gt; is actually a &lt;i&gt;Hashtable&lt;/i&gt;).&lt;br /&gt;
This means that you might find plenty more of cases in which unnecessary boxing occurs without your knowing. But what makes &lt;i&gt;DateTime.Now&lt;/i&gt; case especially serious is that it's a very common property to access, especially by logging frameworks for instance.&lt;br /&gt;
&lt;br /&gt;
All of those boxings comes with a price, and it's not cheap. Due to the high frequency of dynamic memory allocations, we could cause some serious stress for the GC.&lt;br /&gt;
In order to demonstrate the impact on memory usage, I'll use the following benchmark:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="font-family: consolas;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;while&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;true&lt;/span&gt;)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Stopwatch&amp;nbsp;sw&amp;nbsp;=&amp;nbsp;Stopwatch.StartNew();
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;for&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;i&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;i&amp;nbsp;&amp;lt;&amp;nbsp;1000000;&amp;nbsp;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;DateTime&lt;/span&gt;&amp;nbsp;now&amp;nbsp;=&amp;nbsp;&lt;span style="color: #2b91af;"&gt;DateTime&lt;/span&gt;.Now;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(sw.ElapsedMilliseconds);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/pre&gt;After executing this code, we can learn two things. The first, is that it takes on average 456ms to complete each iteration on the &lt;i&gt;while&lt;/i&gt; loop (I'll address this result in a moment), and the other, is that we can learn the magnitude of the affect of calling &lt;i&gt;DateTime.Now&lt;/i&gt; on our application's memory usage patterns. In this case, I've used &lt;i&gt;perfmon&lt;/i&gt; to monitor the application's memory allocation rate:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://i26.tinypic.com/bf5qxg.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="198" src="http://i26.tinypic.com/bf5qxg.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;br /&gt;
&lt;/div&gt;Even though most of these allocations will be released by the GC in a Generation 0 collection, we still can't take them as granted. Especially if you're developing an application where you need to minimize as possible any dynamic memory allocation, mostly because you want to reduce the number of collections performed by the GC.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How to Avoid The Boxing?&lt;/b&gt;&lt;br /&gt;
After we've seen why we shouldn't call &lt;i&gt;DateTime.Now&lt;/i&gt; (in certain scenarios), raises the question "How could we avoid those unnecessary memory allocations?".&lt;b&gt; &lt;/b&gt;&lt;br /&gt;
First, there's Microsoft's side, which needs to make sure to update their code to make sure they avoid boxing (e.g, using a &lt;i&gt;Dictionary&lt;/i&gt; instead of a &lt;i&gt;Hashtable&lt;/i&gt;). But until this happens, we'll need to take matters to our own hands and workaround this issue. My suggestion is to write a wrapper method that is almost identical to &lt;i&gt;UtcNow&lt;/i&gt;'s implementation (that simply wraps a call to the Win32 API). All we need to do is to call &lt;a href="http://msdn.microsoft.com/en-us/library/ms724338%28VS.85%29.aspx"&gt;&lt;i&gt;GetLocalTime&lt;/i&gt;&lt;/a&gt; that will return use the the in &lt;a href="http://msdn.microsoft.com/en-us/library/ms724950%28VS.85%29.aspx"&gt;SYSTEMTIME&lt;/a&gt; format, which we will need to convert back to a &lt;i&gt;DateTime&lt;/i&gt; type.&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;TimeUtil&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[DllImport(&lt;span style="color: #a31515;"&gt;"kernel32.dll"&lt;/span&gt;)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;extern&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;GetLocalTime(&lt;span style="color: blue;"&gt;out&lt;/span&gt;&amp;nbsp;SYSTEMTIME&amp;nbsp;time);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[StructLayout(LayoutKind.Sequential)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;struct&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;SYSTEMTIME&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;&amp;nbsp;Year;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;&amp;nbsp;Month;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;&amp;nbsp;DayOfWeek;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;&amp;nbsp;Day;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;&amp;nbsp;Hour;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;&amp;nbsp;Minute;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;&amp;nbsp;Second;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;&amp;nbsp;Milliseconds;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;DateTime&lt;/span&gt;&amp;nbsp;LocalTime
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SYSTEMTIME&amp;nbsp;nativeTime;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;GetLocalTime(&lt;span style="color: blue;"&gt;out&lt;/span&gt;&amp;nbsp;nativeTime);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;DateTime&lt;/span&gt;(nativeTime.Year,&amp;nbsp;nativeTime.Month,&amp;nbsp;nativeTime.Day,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nativeTime.Hour,&amp;nbsp;nativeTime.Minute,&amp;nbsp;nativeTime.Second,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nativeTime.Milliseconds,&amp;nbsp;&lt;span style="color: #2b91af;"&gt;DateTimeKind&lt;/span&gt;.Local);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;&lt;br /&gt;
Running the benchmark from before with this updated implementation, we could see that we completely got rid of the memory allocations, and as a bonus, every iteration takes only 370ms. That is, an 18% improvement over the performance of &lt;i&gt;DateTime.Now&lt;/i&gt;'s implementation.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1501586010252802469-2366392736586325104?l=blog.liranchen.com' alt='' /&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=uiWnphWE1PY:KU5lsmYboZA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=uiWnphWE1PY:KU5lsmYboZA:63t7Ie-LG7Y"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=63t7Ie-LG7Y" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/LiranChen?a=uiWnphWE1PY:KU5lsmYboZA:G79ilh31hkQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/LiranChen?d=G79ilh31hkQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/LiranChen/~4/uiWnphWE1PY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/LiranChen/~3/uiWnphWE1PY/datetimenow-causes-boxing.html</link><author>noreply@blogger.com (Liran Chen)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://i26.tinypic.com/bf5qxg_th.png" height="72" width="72" /><thr:total>6</thr:total><feedburner:origLink>http://blog.liranchen.com/2010/07/datetimenow-causes-boxing.html</feedburner:origLink></item></channel></rss>

