<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, May 27, 2014 at 6:54 AM, J. Vuletich (mail lists) <span dir="ltr">&lt;<a href="mailto:juanlists@jvuletich.org" target="_blank">juanlists@jvuletich.org</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><u></u>





<div style="font-family:Arial;font-size:14px"><div><div class="h5">
<p>Quoting Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>&gt;:</p>
<blockquote style="border-left-width:2px;border-left-style:solid;border-left-color:blue;margin-left:2px;padding-left:12px" type="cite">
<div dir="ltr">Hi Phillipe,<br>
<div class="gmail_extra"><br>
<br>
<div class="gmail_quote">On Mon, May 26, 2014 at 12:51 AM, Philippe Marschall <span dir="ltr">&lt;<a href="mailto:philippe.marschall@netcetera.ch" target="_blank">philippe.marschall@netcetera.ch</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<p>Hi<br>
<br>
I have been investigating why Dictionary look up performance with String keys is not as good as I would expected. Something I noted is that String &gt;&gt; #= is implemented in terms of #compare:with:collated:. There is no short circuit if Strings are not the same size. In my case some Strings have the same prefix but a different length eg &#39;Content-Type&#39; and &#39;Content-Length&#39;. In that case a #compare:with:collated: is performed even though we know in advance the answer will be false because they have different sizes.</p>

</blockquote>
<div> </div>
<div>Why not rewrite</div>
<div> </div>
<div>String&gt;&gt;= aString </div>
<div>&quot;Answer whether the receiver sorts equally as aString.</div>
<div>The collation order is simple ascii (with case differences).&quot;</div>
<div>aString isString ifFalse: [ ^ false ].</div>
<div>^ (self compare: self with: aString collated: AsciiOrder) = 2</div>
<div> </div>
<div>as</div>
<div> </div>
<div>
<div>String&gt;&gt;= aString </div>
<div>&quot;Answer whether the receiver sorts equally as aString.</div>
<div>The collation order is simple ascii (with case differences).&quot;</div>
<div> </div>
<div><span style="white-space:pre-wrap">(</span>aString isString</div>
<div><span style="white-space:pre-wrap">and: [self size = aString size]) i</span>fFalse: [^false].</div>
<div>^ (self compare: self withSize: with: aString collated: AsciiOrder) = 2</div>
</div>
<div> </div>
<div>?</div></div></div></div></blockquote></div></div></div></blockquote><div><br></div><div>This makes a huge difference, over 3 times faster:</div><div><br></div><div>| bs t1 t2 |</div><div>bs := ByteString allInstances first: 10000.</div>
<div>t1 := [bs do: [:a| bs do: [:b| a = b]]] timeToRun.</div><div>(FileStream fileNamed: &#39;/Users/eliot/Squeak/Squeak4.5/String-=.st&#39;) fileIn.</div><div>t2 := [bs do: [:a| bs do: [:b| a = b]]] timeToRun.</div><div>
{ t1. t2 } #(13726 4467)</div><div>4467 - 13726 / 137.26 -67.46% </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div style="font-family:Arial;font-size:14px"><div><div class="h5"><blockquote style="border-left-width:2px;border-left-style:solid;border-left-color:blue;margin-left:2px;padding-left:12px" type="cite"><div dir="ltr"><div class="gmail_extra">
<div class="gmail_quote">
</div>
<div class="gmail_extra"> </div>
One /could/ add a replacement compare:with:collated: primitive primitiveCompareString which took the sizes as arguments to avoid asking twice.  But it wouldn&#39;t be safe.  One could abuse the primitive and lie about the size.  So I suspect it is best to add the size check to String&gt;&gt;#= and accept the duplication of the primitive finding the sizes of the two strings.  The cost in the primitive is minimal.  A WideString version of the primitive might pay its way, but if Spur and Sista arrive soon the primitive shouldn&#39;t be faster than the optimised Smalltalk code.<br>

--<br>
best,
<div>Eliot</div>
</div>
</div>
</blockquote>
</div></div><p>BTW, any good reason for not prefixing all the implementors of #= with this?<br>
<br>
&quot;Any object is equal to itself&quot;<br>
self == argument ifTrue: [ ^ true ].<br></p></div></blockquote><div><br></div><div>It doesn&#39;t make much difference:</div><div><br></div><div>| bs t1 t2 |</div><div>bs := ByteString allInstances first: 10000.</div><div>
t1 := [bs do: [:a| bs do: [:b| a = b]]] timeToRun.</div><div>(FileStream fileNamed: &#39;/Users/eliot/Squeak/Squeak4.5/String-=.st&#39;) fileIn.</div><div>t2 := [bs do: [:a| bs do: [:b| a = b]]] timeToRun.</div><div>{ t1. t2 } #(4628 4560)</div>
<div><br></div><div>4560 - 4628 / 46.28 -1.47%</div><div><br></div><div>So is it worth it?  If you feel it is I&#39;ve no objection other than it feels a little kludgey for such little benefit.  And there are the Symbols if one needs quick comparison and can bear the cost of slow interning.</div>
</div>-- <br>best,<div>Eliot</div>
</div></div>