Default ordering of Hash keys

Patrick R. Michaud pmichaud at pobox.com
Sun Aug 7 17:24:46 UTC 2011


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.

(Linebreaks added below for clarity.)

    pmichaud at kiwi:/zip/perl$ cat hash.pir
    .sub 'main' :main
        .local pmc hash
        hash = new ['Hash']
        $I0 = 0
      loop0:
        unless $I0 < 100 goto done0
        $S0 = $I0
        hash[$S0] = 1
        inc $I0
        goto loop0
      done0:
    
        .local pmc it
        it = iter hash
        .local string max
        max = ''
      loop_it:
        unless it goto done_it
        $P0 = shift it
        $S0 = $P0.'key'()
        print $S0
        print ' '
        goto loop_it
      done_it:
        print "\n"
    
        .return (0)
    .end
    
    pmichaud at kiwi:/zip/perl$ parrot-2.6.0/parrot hash.pir
    60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 
    79 80 81 82 83 84 85 86 87 88 89 10 11 12 13 14 15 16 17 
    90 18 91 19 92 93 94 95 96 97 98 99 20 21 22 23 24 25 26 
    27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 
    46 47 48 49 50 51 52 53 54 55 56 57 58 59 0 1 2 3 4 5 6 7 8 9 

    pmichaud at kiwi:/zip/perl$ parrot-2.9.1/parrot hash.pir
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 
    22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
    41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 
    60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 
    79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 

    pmichaud at kiwi:/zip/perl$ 

Pm


More information about the parrot-dev mailing list