<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Sat, Nov 24, 2018 at 7:40 AM Luciano Notarfrancesco <<a href="mailto:luchiano@gmail.com">luchiano@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">On Fri, Nov 23, 2018 at 12:36 AM Chris Muller <<a href="mailto:asqueaker@gmail.com" target="_blank">asqueaker@gmail.com</a>> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">> >> I'm a bit puzzled. I thought  (small)Integers being their own hash is a good thing?<br>
<br>
I was wondering exactly the same thing!<br>
<br>
> > I would call it simple but not necessarily good.<br>
> > The problem with it is that consecutive numbers generate long chains in HashedCollections:<br>
> ><br>
> > a := (1 to: 1000) asArray.<br>
> > s := Set withAll: a.<br>
> > [ 1 to: 1000000 do: [ :each | s includes: each ] ] timeToRun.<br>
> > "==> 7014"<br>
> ><br>
> > The solution in Squeak is to use PluggableSet instead of Set, because it applies #hashMultiply on the hash value:<br>
> ><br>
> > ps := PluggableSet integerSet.<br>
> > ps addAll: a.<br>
> > [ 1 to: 1000000 do: [ :each | ps includes: each ] ] timeToRun.<br>
> > "==> 95"<br>
> ><br>
> > IIRC in Pharo SmallInteger's hash is based on #hashMultiply to avoid the long chains. That was probably the main reason for the push to make #hashMultply a numbered primitive.<br>
> ><br>
><br>
> Interesting!<br>
<br>
Indeed!  When making a #hash methods, one always focuses on hash<br>
distribution and finding the elements, but its easy to forget about<br>
performance of NOT finding an element.<br>
<br></blockquote><div><br></div><div>Yes! I had this problem blow in my face a couple of years ago, and Juan agreed to include the change in Cuis to make SmallIntegers NOT their own hash.</div></div></div></blockquote><div><br></div><div>This seems to be a basic error.  The idea of a hash function is to produce a well-distributed set of integers for some set of values.  Since the SmallIntegers are themselves perfectly distributed (each unique SmallInteger is a unique value), it is impossible to produce a better distributed hash function than  the integers themselves. For some application it may indeed be possible to produce a better distributed set of hashes for the integers; for example an application which considers only powers of two could use the log base 2 to produce a smaller and better distributed set fo values modulo N than the integers themselves.  But in general the integers are definitionally well-distributed.  In fact, unless one has a perfect hash function one is in danger of producing a less well-distributed set of values from a hash function than the SmallIntegers themselves.</div><div><br></div><div>This argument doesn't apply as integers grow beyond the SmallInteger, but not because we want better distribution of values than the large integers themselves, but b because we want to avoid large integer arithmetic.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div>I think it is a good idea in general when programming a hash method in Smalltalk to make it somewhat random.</div></div></div></blockquote><div><br></div><div>As I've indicated, I think this is impossible in general.  It is only in specific applications, using specific subsets of the SmallIn tigers for which a better hash function could be derived, but this would be specific to that application.  For purposes such as these we have PluggableDictionary et al which can exploit an application-specific hash.  But in general the SmallIntegers are ideal values for their own hashes.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div> It will never be perfect for every use case, and it doesn't need to be cryptographically secure, but there shouldn't be simple use cases (e.g., consecutive integers) that produce hashes that are not uniformly distributed. This can be achieved with minimal performance impact (and potentially big performance gains in hashed collections) quite simply by sending some hashMultiply message in your hash method.<br></div><div><br></div><div>Regards,</div><div>Luciano</div></div></div>
</blockquote></div><div><br></div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div></div>