Default ordering of Hash keys

Luben Karavelov karavelov at spnet.net
Sun Aug 7 23:55:43 UTC 2011


 On Sun, 7 Aug 2011 12:24:46 -0500, "Patrick R. Michaud" 
 <pmichaud at pobox.com> wrote:
> Starting with Parrot 1.4.0 and continuing through Parrot 2.6.0,
> iterating a Hash would return elements in a different order than
> the order used to create the Hash.
>
> Starting with Parrot 2.9.1, hash iterators appear to be using
> the creation order again, and I'm curious to know why this
> is the case.  See code sample and output below.
>
> Yes, I recognize that Parrot returning ordered keys for hashes
> is technically valid, since Parrot is allowed to return hash
> keys in whatever order it wants.  But Rakudo experienced some
> significant pain with the Parrot 1.4.0 release due to an
> unexpected reliance on hash key ordering, and so it might be
> worth knowing why/how Parrot has reverted to the pre-1.4.0 behavior.
>

 For some types of keys (that have zero/NULL as valid value, i.e.
 integer, ptr, cstring) parrot iterates keys in index order, not
 ordered by creation.

 For the rest of key types it iterates them in memory order, that is
 the same as create order, if there were no key deleted. This is faster
 because it has less computation and a lot less data cache fails,
 especially with big hashes.

 The change arround 2.9 was part of optimization of parrot hashes done
 by me and chromatic. We were optimizing for the common case that is
 having String hash keys.

-- 
 Luben Karavelov


More information about the parrot-dev mailing list