<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Virtual Method Performance Penalty</title>
	<atom:link href="http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/feed/" rel="self" type="application/rss+xml" />
	<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/</link>
	<description>Trying to walk that thin line between intelligence and ignorance</description>
	<lastBuildDate>Thu, 29 Jul 2010 21:01:23 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Alex Yakunin</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-25676</link>
		<dc:creator>Alex Yakunin</dc:creator>
		<pubDate>Thu, 21 Jan 2010 19:06:31 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-25676</guid>
		<description>Finally, there are no reasons to cache VMTable lookups - they anyway take constant (and very small) time. 

VMTable lookup caching can be practically useful only for interface calls, since method address resolution is more complex in this case.

So forget about idea #3 ;)</description>
		<content:encoded><![CDATA[<p>Finally, there are no reasons to cache VMTable lookups &#8211; they anyway take constant (and very small) time. </p>
<p>VMTable lookup caching can be practically useful only for interface calls, since method address resolution is more complex in this case.</p>
<p>So forget about idea #3 <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Yakunin</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-25675</link>
		<dc:creator>Alex Yakunin</dc:creator>
		<pubDate>Thu, 21 Jan 2010 18:57:55 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-25675</guid>
		<description>A bit better idea to avoid any possible VMCall caching:

// Objects must be different
theObject1 = theObject.Clone();
...
theObject2 = theObject.Clone();

— Loop start —
theObject1.VirtualIncreaseLong();
…
theObjectN.VirtualIncreaseLong();

// Now we&#039;re rolling all the references to ensure all of them are changed
theObjectT   = theObjectN;
theObjectN   = theObjectN-1;
theObjectN-1 = theObjectN-2;
...
theObject2 = theObject1;
theObject1 = theObjectT;
— Loop end —</description>
		<content:encoded><![CDATA[<p>A bit better idea to avoid any possible VMCall caching:</p>
<p>// Objects must be different<br />
theObject1 = theObject.Clone();<br />
&#8230;<br />
theObject2 = theObject.Clone();</p>
<p>— Loop start —<br />
theObject1.VirtualIncreaseLong();<br />
…<br />
theObjectN.VirtualIncreaseLong();</p>
<p>// Now we&#8217;re rolling all the references to ensure all of them are changed<br />
theObjectT   = theObjectN;<br />
theObjectN   = theObjectN-1;<br />
theObjectN-1 = theObjectN-2;<br />
&#8230;<br />
theObject2 = theObject1;<br />
theObject1 = theObjectT;<br />
— Loop end —</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Yakunin</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-25674</link>
		<dc:creator>Alex Yakunin</dc:creator>
		<pubDate>Thu, 21 Jan 2010 18:52:14 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-25674</guid>
		<description>&gt; i tried it with new instances for each call, the performance measurements of calling the methods were pretty much identical

Surely: allocation is ~ 4-5 times more costly than vmcall.</description>
		<content:encoded><![CDATA[<p>&gt; i tried it with new instances for each call, the performance measurements of calling the methods were pretty much identical</p>
<p>Surely: allocation is ~ 4-5 times more costly than vmcall.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Yakunin</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-25673</link>
		<dc:creator>Alex Yakunin</dc:creator>
		<pubDate>Thu, 21 Jan 2010 18:50:07 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-25673</guid>
		<description>&gt; Since the c# compiler is emitting “callvirt” for both virtual and non-virtual method calls, you don’t see any difference in performance between the virtual and non-virtual call.

That&#039;s wrong. JITter emits different code for callvirt that actually invoking non-virtual method.

The main reason to always emit callvirt by C# is that actually referenced method can be replaced by virtual in future versions of assembly it is declared in, so using callvirt is safe from the point of compatibility.</description>
		<content:encoded><![CDATA[<p>&gt; Since the c# compiler is emitting “callvirt” for both virtual and non-virtual method calls, you don’t see any difference in performance between the virtual and non-virtual call.</p>
<p>That&#8217;s wrong. JITter emits different code for callvirt that actually invoking non-virtual method.</p>
<p>The main reason to always emit callvirt by C# is that actually referenced method can be replaced by virtual in future versions of assembly it is declared in, so using callvirt is safe from the point of compatibility.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Yakunin</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-25671</link>
		<dc:creator>Alex Yakunin</dc:creator>
		<pubDate>Thu, 21 Jan 2010 18:46:04 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-25671</guid>
		<description>Two remarks on your tests:

1) I&#039;d recommend to enroll the main loop 10 times at least. Increment + comparison cost is comparable to VTable lookup.

2) EnsureThatEverythingHasBeenJitted actually doesn&#039;t work: TestXxxMethod isn&#039;t jitted after this call, and consequently, Stopwatch code isn&#039;t jitted as well. Not really important in your case (2...5 sec. is pretty large time in comparison to JITter costs), but I&#039;d anyway call TestXxxMethods from EnsureThatEverythingHasBeenJitted, but with small numberOfTimes value.

3) If 1 &amp; 2 won&#039;t affect on result, I&#039;d try to use a bit different code to avoid possible VTable lookup caching:
theObject1 = (MyObject) (object) theObject;
...
theObjectN = (MyObject) (object) theObject;
--- Loop start ---
theObject1.VirtualIncreaseLong();
...
theObjectN.VirtualIncreaseLong();
--- Loop end ---

Not fully sure if this will work, since a lot depends on compiler here. But it&#039;s clear that original code is simply ideal for VTable lookup caching.</description>
		<content:encoded><![CDATA[<p>Two remarks on your tests:</p>
<p>1) I&#8217;d recommend to enroll the main loop 10 times at least. Increment + comparison cost is comparable to VTable lookup.</p>
<p>2) EnsureThatEverythingHasBeenJitted actually doesn&#8217;t work: TestXxxMethod isn&#8217;t jitted after this call, and consequently, Stopwatch code isn&#8217;t jitted as well. Not really important in your case (2&#8230;5 sec. is pretty large time in comparison to JITter costs), but I&#8217;d anyway call TestXxxMethods from EnsureThatEverythingHasBeenJitted, but with small numberOfTimes value.</p>
<p>3) If 1 &amp; 2 won&#8217;t affect on result, I&#8217;d try to use a bit different code to avoid possible VTable lookup caching:<br />
theObject1 = (MyObject) (object) theObject;<br />
&#8230;<br />
theObjectN = (MyObject) (object) theObject;<br />
&#8212; Loop start &#8212;<br />
theObject1.VirtualIncreaseLong();<br />
&#8230;<br />
theObjectN.VirtualIncreaseLong();<br />
&#8212; Loop end &#8212;</p>
<p>Not fully sure if this will work, since a lot depends on compiler here. But it&#8217;s clear that original code is simply ideal for VTable lookup caching.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Interesting Finds: 2010 01.10 ~ 01.18 - gOODiDEA.NET</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-25411</link>
		<dc:creator>Interesting Finds: 2010 01.10 ~ 01.18 - gOODiDEA.NET</dc:creator>
		<pubDate>Tue, 19 Jan 2010 02:26:03 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-25411</guid>
		<description>[...] Virtual Method Performance Penalty [...]</description>
		<content:encoded><![CDATA[<p>[...] Virtual Method Performance Penalty [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24521</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Mon, 11 Jan 2010 16:03:04 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24521</guid>
		<description>@Philip

there is still a difference between using callvirt for a non-virtual method and for a virtual method AFAIK.  When using callvirt with a virtual function, the CLR still does a null reference check (and throws a NullReferenceException if the instance reference is null) while it does the null check and an aditional lookup of the correct virtual method to call in case of a virtual method.  I had always been told that that was supposedly the reason why there should be a performance difference.  AFAIK, the call instruction is only used for static methods.

If there is indeed no performance cost at all, then certain Microsoft people really need to stop using that as an argument ;). And it should, in fact be removed as a reason why not to use virtual methods from the Framework Design Guidelines book that they publish.  And they might want to update the CLR Via C# book regarding this as well ;)</description>
		<content:encoded><![CDATA[<p>@Philip</p>
<p>there is still a difference between using callvirt for a non-virtual method and for a virtual method AFAIK.  When using callvirt with a virtual function, the CLR still does a null reference check (and throws a NullReferenceException if the instance reference is null) while it does the null check and an aditional lookup of the correct virtual method to call in case of a virtual method.  I had always been told that that was supposedly the reason why there should be a performance difference.  AFAIK, the call instruction is only used for static methods.</p>
<p>If there is indeed no performance cost at all, then certain Microsoft people really need to stop using that as an argument <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . And it should, in fact be removed as a reason why not to use virtual methods from the Framework Design Guidelines book that they publish.  And they might want to update the CLR Via C# book regarding this as well <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Philip Rieck</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24519</link>
		<dc:creator>Philip Rieck</dc:creator>
		<pubDate>Mon, 11 Jan 2010 14:53:49 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24519</guid>
		<description>You can see some of the reasons for what you&#039;re seeing here&lt;/a&gt; and &lt;a href=&quot;http://blogs.msdn.com/ericgu/archive/2008/07/07/why-does-c-always-use-callvirt-followup.aspx&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt; (both go to Eric Gunnerson&#039;s blog).

Basically, there are two IL instructions to call a method,  &quot;call&quot; and &quot;callvirt&quot;.  Since the c# compiler is emitting &quot;callvirt&quot; for both virtual and non-virtual method calls, you don&#039;t see any difference in performance between the virtual and non-virtual call.</description>
		<content:encoded><![CDATA[<p>You can see some of the reasons for what you&#8217;re seeing here and <a href="http://blogs.msdn.com/ericgu/archive/2008/07/07/why-does-c-always-use-callvirt-followup.aspx" rel="nofollow">here</a> (both go to Eric Gunnerson&#8217;s blog).</p>
<p>Basically, there are two IL instructions to call a method,  &#8220;call&#8221; and &#8220;callvirt&#8221;.  Since the c# compiler is emitting &#8220;callvirt&#8221; for both virtual and non-virtual method calls, you don&#8217;t see any difference in performance between the virtual and non-virtual call.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: den Ben</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24518</link>
		<dc:creator>den Ben</dc:creator>
		<pubDate>Mon, 11 Jan 2010 14:44:55 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24518</guid>
		<description>I meant using multiple classes.. as in types, not instances.

Once a vtable is loaded for a certain type, I imagine the performance cost to be pretty low.</description>
		<content:encoded><![CDATA[<p>I meant using multiple classes.. as in types, not instances.</p>
<p>Once a vtable is loaded for a certain type, I imagine the performance cost to be pretty low.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24504</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Mon, 11 Jan 2010 13:23:54 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24504</guid>
		<description>and yes, i allocated the new instances _before_ i started timing obviously, so i don&#039;t think it&#039;s just a caching thing</description>
		<content:encoded><![CDATA[<p>and yes, i allocated the new instances _before_ i started timing obviously, so i don&#8217;t think it&#8217;s just a caching thing</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24503</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Mon, 11 Jan 2010 13:23:15 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24503</guid>
		<description>@Ben

i tried it with new instances for each call, the performance measurements of calling the methods were pretty much identical</description>
		<content:encoded><![CDATA[<p>@Ben</p>
<p>i tried it with new instances for each call, the performance measurements of calling the methods were pretty much identical</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: den Ben</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24500</link>
		<dc:creator>den Ben</dc:creator>
		<pubDate>Mon, 11 Jan 2010 12:49:16 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24500</guid>
		<description>My guess is that subsequent virtual method calls are cached (not the result offcourse, the call itself).

To be able to measure the performance correctly you would need multiple classes... maybe quicknet it? ;-)</description>
		<content:encoded><![CDATA[<p>My guess is that subsequent virtual method calls are cached (not the result offcourse, the call itself).</p>
<p>To be able to measure the performance correctly you would need multiple classes&#8230; maybe quicknet it? <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24498</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Mon, 11 Jan 2010 11:37:26 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24498</guid>
		<description>@Dmitri

i don&#039;t see anything in that post that explains the behavior i&#039;m seeing here</description>
		<content:encoded><![CDATA[<p>@Dmitri</p>
<p>i don&#8217;t see anything in that post that explains the behavior i&#8217;m seeing here</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: The Morning Brew - Chris Alcock &#187; The Morning Brew #514</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24494</link>
		<dc:creator>The Morning Brew - Chris Alcock &#187; The Morning Brew #514</dc:creator>
		<pubDate>Mon, 11 Jan 2010 08:28:06 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24494</guid>
		<description>[...] Virtual Method Performance Penalty - Davy Brion talks about the alleged penalty in performance when working with Virtual methods in .NET, and tries some simple benchmarks which appear to show that there is little or no actual penalty in hi use case. [...]</description>
		<content:encoded><![CDATA[<p>[...] Virtual Method Performance Penalty &#8211; Davy Brion talks about the alleged penalty in performance when working with Virtual methods in .NET, and tries some simple benchmarks which appear to show that there is little or no actual penalty in hi use case. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmitri Maximov</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24451</link>
		<dc:creator>Dmitri Maximov</dc:creator>
		<pubDate>Sun, 10 Jan 2010 20:57:57 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24451</guid>
		<description>Hello Davy,

I suppose that this might be interesting for you: The cost of method calls (http://tips.x-tensive.com/2008/10/method-call-performance.html)

Virtual method calls are compared with delegate method calls as well as with interface method calls. Moreover, delegate creation &amp; generic method calls are also measured. Take a look at it.</description>
		<content:encoded><![CDATA[<p>Hello Davy,</p>
<p>I suppose that this might be interesting for you: The cost of method calls (<a href="http://tips.x-tensive.com/2008/10/method-call-performance.html" rel="nofollow">http://tips.x-tensive.com/2008/10/method-call-performance.html</a>)</p>
<p>Virtual method calls are compared with delegate method calls as well as with interface method calls. Moreover, delegate creation &amp; generic method calls are also measured. Take a look at it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24447</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Sun, 10 Jan 2010 19:32:30 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24447</guid>
		<description>@Al

Anders Hejlsberg discusses his reasons for making it non-virtual by default here:
http://www.artima.com/intv/nonvirtual.html</description>
		<content:encoded><![CDATA[<p>@Al</p>
<p>Anders Hejlsberg discusses his reasons for making it non-virtual by default here:<br />
<a href="http://www.artima.com/intv/nonvirtual.html" rel="nofollow">http://www.artima.com/intv/nonvirtual.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Al</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24445</link>
		<dc:creator>Al</dc:creator>
		<pubDate>Sun, 10 Jan 2010 19:06:05 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24445</guid>
		<description>Java has the opposite convention - all methods are virtual unless declared as &lt;strong&gt;final&lt;strong&gt;.

I sometimes wonder if MS did it the other way round just so they could be deliberately different (in the beginning C# was very similar to Java - still is, but less so now as C# has evolved more successfully)

No matter, you&#039;d have to be writing a seriously performance-sensitive application for it to make any difference - in which case you&#039;d probably not be using a language with automatic garbage collection anyway ....</description>
		<content:encoded><![CDATA[<p>Java has the opposite convention &#8211; all methods are virtual unless declared as <strong>final</strong><strong>.</p>
<p>I sometimes wonder if MS did it the other way round just so they could be deliberately different (in the beginning C# was very similar to Java &#8211; still is, but less so now as C# has evolved more successfully)</p>
<p>No matter, you&#8217;d have to be writing a seriously performance-sensitive application for it to make any difference &#8211; in which case you&#8217;d probably not be using a language with automatic garbage collection anyway &#8230;.</strong></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/comment-page-1/#comment-24443</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Sun, 10 Jan 2010 18:38:14 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/virtual-method-performance-penalty/#comment-24443</guid>
		<description>I&#039;ve found similar results when dealing with delegate calls and lambda functions.

My test also showed that the first execution of a method call has a more significant impact.

  It really emphasizes the principle measure, test, measure and test some more.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve found similar results when dealing with delegate calls and lambda functions.</p>
<p>My test also showed that the first execution of a method call has a more significant impact.</p>
<p>  It really emphasizes the principle measure, test, measure and test some more.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
