<div dir="ltr">On 11 April 2013 09:23, Walter Bright <span dir="ltr"><<a href="mailto:newshound2@digitalmars.com" target="_blank">newshound2@digitalmars.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 4/10/2013 4:00 AM, Manu wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
It doesn't have all sources. It could load a library. That can never be guaranteed.<br>
</blockquote>
<br>
Currently, a virtual call is done with:<br>
<br>
   (*vtbl[i])(args...)<br>
<br>
It makes me wonder if the following would be faster:<br>
<br>
   method_fp = vtbl[i];<br>
   if (method_fp == &method)<br>
       method(args...)<br>
   else<br>
       (*method_fp)(args...)<br>
<br>
Anyone care to dummy this up and run a benchmark?<br></blockquote><div><br></div><div style>**On a non-x86 machine.</div><div style>It would depend how derived the class is, but I reckon there's good chance it would be slower. In the end, the work is the same, but you've introduced another hazard on top, a likely branch misprediction.</div>
<div style>I think the common case with classes is that you're calling through a base-pointer, so virtual calls become more expensive, and the non-virtuals may save a cache miss, but gain a misprediction. I suspect the cache miss (a more costly hazard) would occur less often than the mispredicion, which, given a greater volume, might add up to be similar.</div>
<div style>This is also very, very hard to profile realistically. Isolated test cases can't reveal the truth on this matter, and in the end, we remain in the same place, where non-virtual-by-default is clearly better.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
It's also possible to, for the per-class static data, have a flag that says "isfinal", which is set to false at runtime whenever a new is done for a derived class. Then, the method test above becomes:<br>
<br>
    if (isfinal)<br>
</blockquote></div><br></div><div class="gmail_extra" style>Branch still exists, vcall is still required if you have a base pointer. Accessors might benefit, but incur the cost of a branch. Aside from virtual (or lack thereof), if() is possibly the second most dangerous keyword ;)</div>
<div class="gmail_extra" style>It's getting better with time though. Super-long pipeline architectures are slowly dying off.</div></div>