phash2/1 for atoms

Thomas Johnsson thomas@REDACTED
Fri Feb 24 17:03:56 CET 2006


erlang:phash2 as expected seems to give a "random" bit sequence for 
terms in general, eg.,

69> erlang:phash2(0).
88723725
70> erlang:phash2(1).
2614250
71> erlang:phash2({}).
87486268
72> erlang:phash2({0}).
84975979
73> erlang:phash2([]).
113427502
74> erlang:phash2([1,2]).
13006701

However, applying phash2 to short atoms does not seem quite so "randomly 
wellbehaved":

57> erlang:phash2(a).
97
58> erlang:phash2(aa).
1649
59> erlang:phash2(aaa).
26481
60> erlang:phash2(aaaa).
423793
61> erlang:phash2(aaaaa).
6780785
62> erlang:phash2(aaaaaa).
108492657
63> erlang:phash2(aaaaaaa).
125269777
64> erlang:phash2(aaaaaaaa).
125268225
65> erlang:phash2(aaaaaaaaa).
125243393
66> erlang:phash2(aaaaaaaaaa).
124846081
67> erlang:phash2(baaaaaaaaa).
124907521
68> erlang:phash2(aaaaaaaaab).
124846082

Is this intentional, or a bug?

Looking at the source in erts/emulator/beam/utils.c in the OTP 
distribution,  it seems that for atoms
       atom_tab(atom_val(term))->slot.bucket.hvalue
is always used, without using the "funny numbers" (HCONST_....) or 
UINT32_HASH.

-- Thomas







More information about the erlang-questions mailing list