John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I recall reading that Perl 5.8 changed the function it uses to make hash keys on strings to use the "newhash" function on burbleburble.

It bugs me to have to code this function in Perl if the function already exists somewhere in the Perl core! Is it exposed somehow?

—John

Replies are listed 'Best First'.
Re: Calling the 'newhash' ?
by Zaxo (Archbishop) on Feb 27, 2003 at 22:10 UTC

    It's defined as a C macro in hv.h. From that file:

    /* hash a key */ /* FYI: This is the "One-at-a-Time" algorithm by Bob Jenkins * from requirements by Colin Plumb. * (http://burtleburtle.net/bob/hash/doobs.html) */ /* The use of a temporary pointer and the casting games * is needed to serve the dual purposes of * (a) the hashed data being interpreted as "unsigned char" (new since + 5.8, * a "char" can be either signed or signed, depending on the compi +ler) * (b) catering for old code that uses a "char" */ #define PERL_HASH(hash,str,len) \ STMT_START { \ register const char *s_PeRlHaSh_tmp = str; \ register const unsigned char *s_PeRlHaSh = (const unsigned cha +r *)s_PeRlHaSh_tmp; \ 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
    It should be straightforward to produce an extension function from that with Inline::C or standard XS.

    After Compline,
    Zaxo

      Ah, the "One-at-a-Time" algorithm. That's quite simple in comparison to the first one on the cited page, his "new hash". That takes 12 bytes at a time with a 12-byte internal state, but it is actually faster (order of 6n instead of 9n) because it handles more in a gulp.

      I was thinking that Perl used the "newhash" now.

      —John

Re: Calling the 'newhash' ?
by BrowserUk (Patriarch) on Feb 27, 2003 at 22:53 UTC

    Is this the hash you are thinking of? If so, I have an implementation in perl which seems reasonably quick but I haven't managed to verify it as a correct implementation yet. Of course, an inline C or XS version would be much quicker if you have that option open to you.


    ..and remember there are a lot of things monks are supposed to be but lazy is not one of them

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      The listing at the beginning of that page -- yes, that's what I'm looking for. I'd be happy to start with your code, since I'd need to verify anything I wrote anyway (and I'll let you know the results).

      —John