<br><br><div class="gmail_quote">On Sat, Feb 21, 2009 at 3:36 PM, Igor Stasenko <span dir="ltr">&lt;<a href="mailto:siguctua@gmail.com">siguctua@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
2009/2/21 Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt;:<br>
<div><div></div><div class="Wj3C7c">&gt;<br>
&gt; Hi Igor,<br>
&gt;<br>
&gt; On Fri, Feb 20, 2009 at 11:37 PM, Igor Stasenko &lt;<a href="mailto:siguctua@gmail.com">siguctua@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Here the method:<br>
&gt;&gt;<br>
&gt;&gt; isContextHeader: aHeader<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp;self inline: true.<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp;^ ((aHeader &gt;&gt; 12) bitAnd: 16r1F) = 13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;MethodContext&quot;<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;or: [((aHeader &gt;&gt; 12) bitAnd: 16r1F) = 14 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;BlockContext&quot;<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;or: [((aHeader &gt;&gt; 12) bitAnd: 16r1F) = 4]] &nbsp; &nbsp; &nbsp;&quot;PseudoContext&quot;<br>
&gt;&gt;<br>
&gt;&gt; i think it wouldn&#39;t hurt to rewrite it as:<br>
&gt;&gt;<br>
&gt;&gt; isContextHeader: aHeader<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp;self inline: true.<br>
&gt;&gt; &nbsp;| hdr |<br>
&gt;&gt; &nbsp;hdr := aHeader bitAnd: (16r1F &lt;&lt; 12).<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp;^ hdr = (13 &lt;&lt; 12) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;MethodContext&quot;<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;or: [ hdr = (14 &lt;&lt; 12) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;BlockContext&quot;<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;or: [ hdr = (4 &lt;&lt; 12)]] &nbsp;&quot;PseudoContext&quot;<br>
&gt;&gt;<br>
&gt;&gt; which will allow GCC to optimize it more easily.<br>
&gt;&gt; I&#39;m not sure if it can optimize it in its current state.<br>
&gt;&gt; This may impact a small speedup of copy operations and any other<br>
&gt;&gt; operations which need to determine a number of pointer fields in<br>
&gt;&gt; object (users of #lastPointerOf:)<br>
&gt;<br>
&gt;<br>
&gt; First you should look at the assembly that gcc generates to be sure anything is needed. &nbsp;e.g.<br>
&gt; cat &gt;t.c &lt;&lt;END<br>
&gt; long isContext(long aHeader) {<br>
&gt; &nbsp; &nbsp; return ((aHeader &gt;&gt; 12) &amp; 0x1F) == 13<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; || ((aHeader &gt;&gt; 12) &amp; 0x1F) == 14<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; || ((aHeader &gt;&gt; 12) &amp; 0x1F) == 4;<br>
&gt; }<br>
&gt; END<br>
&gt; gcc -O3 -S -fomit-frame-pointer t.c; cat t.s<br>
&gt; &nbsp; &nbsp; .text<br>
&gt; .globl _isContext<br>
&gt; _isContext:<br>
&gt; &nbsp; &nbsp; movl &nbsp; &nbsp;4(%esp), %edx<br>
&gt; &nbsp; &nbsp; sarl &nbsp; &nbsp;$12, %edx<br>
&gt; &nbsp; &nbsp; andl &nbsp; &nbsp;$31, %edx<br>
&gt; &nbsp; &nbsp; leal &nbsp; &nbsp;-13(%edx), %eax<br>
&gt; &nbsp; &nbsp; cmpl &nbsp; &nbsp;$1, %eax<br>
&gt; &nbsp; &nbsp; jbe L2<br>
&gt; &nbsp; &nbsp; cmpl &nbsp; &nbsp;$4, %edx<br>
&gt; &nbsp; &nbsp; je &nbsp;L2<br>
&gt; &nbsp; &nbsp; xorl &nbsp; &nbsp;%eax, %eax<br>
&gt; &nbsp; &nbsp; ret<br>
&gt; L2:<br>
&gt; &nbsp; &nbsp; movl &nbsp; &nbsp;$1, %eax<br>
&gt; &nbsp; &nbsp; ret<br>
&gt; &nbsp; &nbsp; .subsections_via_symbols<br>
&gt;<br>
&gt; So you don&#39;t need to do anything; it has done everything for you.<br>
&gt; However, one point is important. &nbsp;Using 16r1F &lt;&lt; 12 et al as your masks and constants to compare against is much worse on many systems, most importantly x86, than shifting down by 12 and comparing against small constants, because the instruction set can encode small constants far more compactly, and that means better code density in the icache which is significant for performance. &nbsp;e.g. on x86 a constant in the range -128 to 127 typically takes a byte whereas anything else will take 4.<br>

&gt; But what I really think is that this is too low a level to worry about. &nbsp;Much more important to focus on<br>
&gt; - context to stack mapping<br>
&gt; - in-line cacheing via a JIT<br>
&gt; - exploiting multicore via Hydra<br>
&gt; and beyond (e.g. speculative inlining)<br>
&gt; than worrying about tiny micro-optimizations like this :)<br>
<br>
</div></div>Thanks Eliot.<br>
In fact, this method drawn my attention because of its number of<br>
checks. Typically, all code which dealing with object formats contain<br>
many branches. And from places where this method is called, there are<br>
additional checks surrounding it.<br>
So, the real problem is the overwhelming number of checks to do<br>
something, and i think this having own impacts on performance.<br>
I hope that a new object format with 64 bit header, which you plan to<br>
use, will allow us to avoid so many branches in code which having high<br>
usage frequency.</blockquote><div><br></div><div>In fact the StackVM makes a big improvement to this very method because in the StackVM there are only MethodContexts and so the method reads</div><div><br></div><div><div>
isContextHeader: aHeader</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&lt;inline: true&gt;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot;c.f. {BlockContext. MethodContext. PseudoContext} collect: [:class| class -&gt; class indexIfCompact]&quot;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>^(self compactClassIndexOfHeader: aHeader) == ClassMethodContextCompactIndex</div><div><br></div><div>which is f course equivalent to</div><div><br></div><div>
<div>isContextHeader: aHeader</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>^((aHeader &gt;&gt; 12) bitAnd: 16r1F) = 13</div><div><br></div><div>:)</div></div></div><div>&nbsp;</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="Ih2E3d">&gt; Best<br>
&gt; Eliot<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; Best regards,<br>
&gt;&gt; Igor Stasenko AKA sig.<br>
&gt;<br>
<br>
<br>
</div>--<br>
<div><div></div><div class="Wj3C7c">Best regards,<br>
Igor Stasenko AKA sig.<br>
</div></div></blockquote></div><br>