in reply to How does perl implement Perl's hash?


This is defined in hv.v of the perl5.005_03 source. It looks like a variation on Daniel J. Bernstein's comp.lang.c "Times 33 with addition" hash function:
#define PERL_HASH(hash,str,len) \ STMT_START { \ register char *s_PeRlHaSh = str; \ register I32 i_PeRlHaSh = len; \ register U32 hash_PeRlHaSh = 0; \ while (i_PeRlHaSh--) \ hash_PeRlHaSh = hash_PeRlHaSh * 33 + *s_PeRlHaSh++; \ (hash) = hash_PeRlHaSh; \ } STMT_END

This is defined in hv.v of the perl-5.7.2 source. The article mentioned in the comment is here.

/* FYI: This is the "One-at-a-Time" algorithm by Bob Jenkins */ /* from requirements by Colin Plumb. */ /* (http://burtleburtle.net/bob/hash/doobs.html) */ #define PERL_HASH(hash,str,len) \ STMT_START { \ register const char *s_PeRlHaSh = str; \ register I32 i_PeRlHaSh = len; \ register U32 hash_PeRlHaSh = 0; \ while (i_PeRlHaSh--) { \ hash_PeRlHaSh += *s_PeRlHaSh++; \ hash_PeRlHaSh += (hash_PeRlHaSh << 10); \ hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \ } \ hash_PeRlHaSh += (hash_PeRlHaSh << 3); \ hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \ (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \ } STMT_END

For anyone who is interested, these hashing functions are also implemented in Ralf Engershall's Str library.

--
John.

Replies are listed 'Best First'.
Re: Re: How does perl implement Perl's hash?
by John M. Dlugosz (Monsignor) on Aug 13, 2002 at 19:16 UTC
    Thanks for the clear answers, jmcnamara.

    So where is the stuff that sauoq referred to?

    FWIW, the one I decided on for my project is similar, but uses a prime close to the golden ratio.

    I don't see how Jenkins' hash can be any faster on machines that have multiplication as a fast instruction.