<p>On Jan 18, 2012 12:55 PM, Martin v. Löwis &lt;<a href="mailto:martin@v.loewis.de">martin@v.loewis.de</a>&gt; wrote:<br>
&gt;<br>
&gt; Am 18.01.2012 17:01, schrieb PJ Eby:<br>
&gt; &gt; On Tue, Jan 17, 2012 at 7:58 PM, &quot;Martin v. Löwis&quot; &lt;<a href="mailto:martin@v.loewis.de">martin@v.loewis.de</a><br>
&gt; &gt; &lt;mailto:<a href="mailto:martin@v.loewis.de">martin@v.loewis.de</a>&gt;&gt; wrote:<br>
&gt; &gt;<br>
&gt; &gt;     Am 17.01.2012 22:26, schrieb Antoine Pitrou:<br>
&gt; &gt;     &gt; Only 2 bits are used in ob_sstate, meaning 30 are left. These 30 bits<br>
&gt; &gt;     &gt; could cache a &quot;hash perturbation&quot; computed from the string and the<br>
&gt; &gt;     &gt; random bits:<br>
&gt; &gt;     &gt;<br>
&gt; &gt;     &gt; - hash() would use ob_shash<br>
&gt; &gt;     &gt; - dict_lookup() would use ((ob_shash * 1000003) ^ (ob_sstate &amp; ~3))<br>
&gt; &gt;     &gt;<br>
&gt; &gt;     &gt; This way, you cache almost all computations, adding only a computation<br>
&gt; &gt;     &gt; and a couple logical ops when looking up a string in a dict.<br>
&gt; &gt;<br>
&gt; &gt;     That&#39;s a good idea. For Unicode, it might be best to add another slot<br>
&gt; &gt;     into the object, even though this increases the object size.<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; Wouldn&#39;t that break the ABI in 2.x?<br>
&gt;<br>
&gt; I was thinking about adding the field at the end, so I thought it<br>
&gt; shouldn&#39;t. However, if somebody inherits from PyUnicodeObject, it still<br>
&gt; might - so my new proposal is to add the extra hash into the str block,<br>
&gt; either at str[-1], or after the terminating 0. This would cause an<br>
&gt; average increase of four bytes of the storage (0 bytes in 50% of the<br>
&gt; cases, 8 bytes because of padding in the other 50%).<br>
&gt;<br>
&gt; What do you think?</p>
<p>So far it sounds like the very best solution of all, as far as backward compatibility is concerned.  If the extra bits are only used when two strings have a matching hash value, the only doctests that could be affected are ones testing for this issue.  ;-)<br>

</p>