http://qs321.pair.com?node_id=1034740


in reply to Re: performance of counting keys in a big hash
in thread performance of counting keys in a big hash

Use the force Luke!

/* hash structure: */ /* This structure must match the beginning of struct xpvmg in sv.h. */ struct xpvhv { HV* xmg_stash; /* class package */ union _xmgu xmg_u; STRLEN xhv_keys; /* total keys, including placeholders +*/ STRLEN xhv_max; /* subscript of last element of xhv_ar +ray */ };

and

/* the number of keys (including any placeholders) */ #define XHvTOTALKEYS(xhv) ((xhv)->xhv_keys) /* * HvKEYS gets the number of keys that actually exist(), and is provid +ed * for backwards compatibility with old XS code. The core uses HvUSEDK +EYS * (keys, excluding placeholders) and HvTOTALKEYS (including placehold +ers) */ #define HvKEYS(hv) HvUSEDKEYS(hv) #define HvUSEDKEYS(hv) (HvTOTALKEYS(hv) - HvPLACEHOLDERS_get( +hv)) #define HvTOTALKEYS(hv) XHvTOTALKEYS((XPVHV*) SvANY(hv)) #define HvPLACEHOLDERS(hv) (*Perl_hv_placeholders_p(aTHX_ MUTABLE +_HV(hv))) #define HvPLACEHOLDERS_get(hv) (SvMAGIC(hv) ? Perl_hv_placeholders_ge +t(aTHX_ (const HV *)hv) : 0)

HvKEYS(hv) is the thing that implements scalar keys %hash

---
$world=~s/war/peace/g

Replies are listed 'Best First'.
Re^3: performance of counting keys in a big hash
by hdb (Monsignor) on May 22, 2013 at 13:23 UTC

    I think I will have to carry you around the swamps of some unknown planet for much longer before I will ready to dive into Perl's source itself to answer questions in this community...

      :-)

      Seriously tho, its not as hard as people think. In fact a lot of the time its easy. All of the code for perl's hash implementation lives in three files, hv.h, hv_func.h, hv.c.

      hv_func.h is the home of the new hash functions if you want to look at the options.

      ---
      $world=~s/war/peace/g

Re^3: performance of counting keys in a big hash
by space_monk (Chaplain) on May 22, 2013 at 14:55 UTC

    Dammit Jim, I'm a doctor, not a C programmer! :-) ok it's a little cross genre, but what the hell

    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)