<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Oct 26, 2015 at 4:08 AM, Rafael Espíndola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">> -O0<br>
> real    0m0.455s<br>
> real    0m0.430s (5.5% faster)<br>
><br>
> -O3<br>
> real    0m0.487s<br>
> real    0m0.452s (7.2% faster)<br>
<br>
</span>Nice!<br>
<span class=""><br>
> +static void qsort(StringPair **Begin, StringPair **End, int Pos) {<br>
> +  if (End - Begin < 2)<br>
> +    return;<br>
> +<br>
> +  // Safeguard for pathetic input.<br>
> +  if (Pos > 10000)<br>
> +    return;<br>
<br>
</span>We get here if two strings have a lot of characters in common, right?<br>
This if then avoids running out of stack.</blockquote><div><br></div><div>Right.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> +  // Partition items. Items in [Begin, P) are less than the pivot, [P, Q)<br>
> +  // are the same as the pivot, and [Q, End) are greater than the pivot.<br>
<br>
</span>We are actually sorting the other way, no? Things larger than the<br>
pivot go to be start of the array.</blockquote><div><br></div><div>Fixed.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class=""><br>
> +  int Pivot = charTailAt(*Begin, Pos);<br>
> +  StringPair **P = Begin;<br>
> +  StringPair **Q = End;<br>
> +  for (StringPair **R = Begin + 1; R < Q;) {<br>
> +    int C = charTailAt(*R, Pos);<br>
> +    if (C > Pivot)<br>
> +      std::swap(*P++, *R++);<br>
> +    else if (C < Pivot)<br>
> +      std::swap(*--Q, *R);<br>
> +    else<br>
> +      R++;<br>
>    }<br>
> -  return SizeB - SizeA;<br>
> +  qsort(Begin, P, Pos);<br>
> +  if (Pivot != -1)<br>
> +    qsort(P, Q, Pos + 1);<br>
<br>
</span>This is the recursive call that can causes us to run out of stack. If<br>
you write this as<br>
<br>
qsort(Begin, P, Pos);<br>
qsort(Q, End, Pos);<br>
if (Pivot != -1)<br>
 loop back to stort P, Q, Pos + 1.<br>
<br>
We should be able to remove the earlier check for Pos being too large.<br></blockquote><div><br></div><div>Funny you should mention that. I thought at that time that might be a bit too much, but since you asked too, we probably should do. Please take a look at the new patch. (If you don't like goto, I can use a for(;;) instead.)</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Thanks you so much for ding this, it should help MC too!<br>
<br>
Cheers,<br>
Rafael<br>
</blockquote></div><br></div></div>