<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><br></div><div>I think it is a good idea in general when programming a hash method in Smalltalk to make it somewhat random. 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.</div><div><br></div><div>Regards,</div><div>Luciano<br></div><div><br></div></div></div>