Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: hash randomization and keys()

by ikegami (Patriarch)
on Jul 29, 2007 at 19:42 UTC ( [id://629430]=note: print w/replies, xml ) Need Help??


in reply to hash randomization and keys()

Given my interpretation of `perldoc -f keys`, and `perldoc perlrun`, every time I call keys() on a hash, even between runs of perl, I will get a different order.

No. The order of the items returned by keys, values and each for a given hash is the same for all three function, and the order doesn't change as long as the hash doesn't change.

The order may change between runs, and the order may change if the contents of the hash changes.

So your question remains. I don't know the answer, but I think what's random is the max bucket size. If so, you may not see a difference with just a few items.

Replies are listed 'Best First'.
Re^2: hash randomization and keys()
by grinder (Bishop) on Jul 30, 2007 at 07:09 UTC

    The hash seed is changing each time:

    PERL_HASH_SEED_DEBUG=1 perl -le 1

    Remember that a hash is mapping an arbitrary string into one of a finite number of buckets. Hash collisions are common, and so for two keys that hash to the same value, their contents will be found somewhere down in a linked list of values hanging off the bucket.

    The orginal attack was that one could concoct a series of keys that kept hashing to the same bucket. The post-5.8.1 hash seed initialises a constant in the hashing algorithm, which makes different buckets get used. But if you're using the same data, they'll follow a different but equivalent bucket usage, and so will appear "in the same order" when you pull them out again. You need something fairly low-level to view the buckets and SVs hanging off each one. I'm not sure off-hand what can be used: Devel::Peek doesn't go far enough.

    At another level, there is code in place to detect when single-bucket allocation is occurring, and then the hash is rehashed to break up the list across many buckets. Since your data is insufficiently pathological, you're not seeing the rehashing occurring from run to run.

    At least, I think that's what's going on...

    • another intruder with the mooring in the heart of the Perl

Re^2: hash randomization and keys()
by dug (Chaplain) on Jul 30, 2007 at 11:35 UTC
    No. The order of the items returned by keys, values and each for a given hash is the same for all three function, and the order doesn't change as long as the hash doesn't change.

    Ahh, of course. I saw that in the documentation as well, but apparently I wanted to believe my wrong interpretation of other parts of the documentation bad enough that I glossed over it.

    Thanks.

    -- Douglas Hunter

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://629430]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-07-13 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.