I'm a Perl-internals newbie, so the following may be invalid, inaccurate, or just plain wrong.. but here's what seems to be going on, from what I can tell:
%h_delete = %h_list = %h_undef = (0 .. 50000); delete @h_delete{keys %h_delete}; %h_list = (); undef %h_undef; use Devel::Peek; Dump \%h_delete; Dump \%h_list; Dump \%h_undef;

Output:

SV = RV(0x830eb38) at 0x838d3a4 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x8171818 SV = PVHV(0x8171d20) at 0x8171818 REFCNT = 2 FLAGS = (SHAREKEYS) IV = 0 NV = 0 ARRAY = 0x40376008 KEYS = 0 FILL = 0 MAX = 32767 RITER = -1 EITER = 0x0 SV = RV(0x830eb38) at 0x838d3a4 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x81765cc SV = PVHV(0x8171d50) at 0x81765cc REFCNT = 2 FLAGS = (SHAREKEYS) IV = 0 NV = 0 ARRAY = 0x403d8008 KEYS = 0 FILL = 0 MAX = 32767 RITER = -1 EITER = 0x0 SV = RV(0x830eb38) at 0x838d3a4 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x81765fc SV = PVHV(0x8171d80) at 0x81765fc REFCNT = 2 FLAGS = (SHAREKEYS) IV = 0 NV = 0 ARRAY = 0x0 KEYS = 0 FILL = 0 MAX = 7 RITER = -1 EITER = 0x0

All the hashes have KEYS = 0, and you would think Perl would be smart enough to notice this when you call keys. The only difference in the 3rd one (%h_undef) is that it has no ARRAY pointer (with MAX set accordingly). I would guess that keys travels that entire array looking for keys, and that's where the slowdown is. This seems to be the case from looking at Perl_hv_iternext_flags. A definitive explanation from someone more versed in the internals is welcome ;)

I don't see why the KEYS = 0 case couldn't be optimized; perhaps you should file a bug and see!

blokhead


In reply to Re: undef speedup ?! by blokhead
in thread undef speedup ?! by powerman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.