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