Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.91k stars 542 forks source link

[PATCH] Clean: Actually use HvUSEDKEYS() instead of HvKEYS() #11349

Closed p5pRT closed 13 years ago

p5pRT commented 13 years ago

Migrated from rt.perl.org#90666 (status was 'resolved')

Searchable as RT90666$

p5pRT commented 13 years ago

From mfwitten@gmail.com

Subject​: [PATCH] Clean​: Actually use HvUSEDKEYS() instead of HvKEYS() Date​: Mon\, 14 Mar 2011 06​:57​:43 +0000 From​: Michael Witten \mfwitten@​gmail\.com

This​:

  commit 8aacddc1ea3837f8f1a911d90c644451fc7cfc86   Author​: Nick Ing-Simmons \nik@​tiuk\.ti\.com   Date​: Tue Dec 18 15​:55​:22 2001 +0000

  Tidied version of Jeffrey Friedl's \jfriedl@​yahoo\.com restricted hashes   - added delete of READONLY value inhibit & test for same   - re-tabbed

  p4raw-id​: //depot/perlio@​13760

essentially deprecated HvKEYS() in favor of HvUSEDKEYS(); this is explained in line 144 (now 313) of file `hv.h'​:

  /*   * HvKEYS gets the number of keys that actually exist()\, and is provided   * for backwards compatibility with old XS code. The core uses HvUSEDKEYS   * (keys\, excluding placeholdes) and HvTOTALKEYS (including placeholders)   */

This commit simply puts that into practice\, and is equivalent to running the following (at least with a35ef416833511da752c4b5b836b7a8915712aab checked out)​:

  git grep -l HvKEYS | sed /hv.h/d | xargs sed -i s/HvKEYS/HvUSEDKEYS/

Notice that HvKEYS is currently just an alias for HvUSEDKEYS​:

  $ git show a35ef416833511da752c4b5b836b7a8915712aab​:hv.h | sed -n 318p   #define HvKEYS(hv) HvUSEDKEYS(hv)

According to `make tests'​:

  All tests successful.


dist/Storable/Storable.xs | 2 +- dist/threads-shared/shared.xs | 2 +- doop.c | 4 ++-- dump.c | 8 ++++---- ext/B/B.xs | 4 ++-- hv.c | 8 ++++---- mg.c | 2 +- mro.c | 2 +- pp.c | 2 +- pp_hot.c | 2 +- t/benchmark/rt26188-speed-up-keys-on-empty-hash.t | 2 +- 11 files changed\, 19 insertions(+)\, 19 deletions(-)

Inline Patch ```diff diff --git a/dist/Storable/Storable.xs b/dist/Storable/Storable.xs index 317ada7..e6d403b 100644 --- a/dist/Storable/Storable.xs +++ b/dist/Storable/Storable.xs @@ -2289,7 +2289,7 @@ static int store_hash(pTHX_ stcxt_t *cxt, HV *hv) #ifdef HAS_RESTRICTED_HASHES HvTOTALKEYS(hv); #else - HvKEYS(hv); + HvUSEDKEYS(hv); #endif I32 i; int ret = 0; diff --git a/dist/threads-shared/shared.xs b/dist/threads-shared/shared.xs index 7f1cd06..13e4a56 100644 --- a/dist/threads-shared/shared.xs +++ b/dist/threads-shared/shared.xs @@ -1039,7 +1039,7 @@ sharedsv_array_mg_FETCHSIZE(pTHX_ SV *sv, MAGIC *mg) val = av_len((AV*) ssv); } else { /* Not actually defined by tie API but ... */ - val = HvKEYS((HV*) ssv); + val = HvUSEDKEYS((HV*) ssv); } SHARED_RELEASE; return (val); diff --git a/doop.c b/doop.c index 4e0d9e3..c11555f 100644 --- a/doop.c +++ b/doop.c @@ -1262,7 +1262,7 @@ Perl_do_kv(pTHX) dTARGET; if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) { - i = HvKEYS(keys); + i = HvUSEDKEYS(keys); } else { i = 0; @@ -1273,7 +1273,7 @@ Perl_do_kv(pTHX) RETURN; } - EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues)); + EXTEND(SP, HvUSEDKEYS(keys) * (dokeys + dovalues)); PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */ while ((entry = hv_iternext(keys))) { diff --git a/dump.c b/dump.c index a340a9c..b5ba17e 100644 --- a/dump.c +++ b/dump.c @@ -1831,13 +1831,13 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo break; case SVt_PVHV: Perl_dump_indent(aTHX_ level, file, " ARRAY = 0x%"UVxf, PTR2UV(HvARRAY(sv))); - if (HvARRAY(sv) && HvKEYS(sv)) { + if (HvARRAY(sv) && HvUSEDKEYS(sv)) { /* Show distribution of HEs in the ARRAY */ int freq[200]; #define FREQ_MAX ((int)(sizeof freq / sizeof freq[0] - 1)) int i; int max = 0; - U32 pow2 = 2, keys = HvKEYS(sv); + U32 pow2 = 2, keys = HvUSEDKEYS(sv); NV theoret, sum = 0; PerlIO_printf(file, " ("); @@ -1879,13 +1879,13 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo } while ((keys = keys >> 1)) pow2 = pow2 << 1; - theoret = HvKEYS(sv); + theoret = HvUSEDKEYS(sv); theoret += theoret * (theoret-1)/pow2; PerlIO_putc(file, '\n'); Perl_dump_indent(aTHX_ level, file, " hash quality = %.1"NVff"%%", theoret/sum*100); } PerlIO_putc(file, '\n'); - Perl_dump_indent(aTHX_ level, file, " KEYS = %"IVdf"\n", (IV)HvKEYS(sv)); + Perl_dump_indent(aTHX_ level, file, " KEYS = %"IVdf"\n", (IV)HvUSEDKEYS(sv)); Perl_dump_indent(aTHX_ level, file, " FILL = %"IVdf"\n", (IV)HvFILL(sv)); Perl_dump_indent(aTHX_ level, file, " MAX = %"IVdf"\n", (IV)HvMAX(sv)); Perl_dump_indent(aTHX_ level, file, " RITER = %"IVdf"\n", (IV)HvRITER_get(sv)); diff --git a/ext/B/B.xs b/ext/B/B.xs index c1071c5..627f851 100644 --- a/ext/B/B.xs +++ b/ext/B/B.xs @@ -1959,12 +1959,12 @@ void HvARRAY(hv) B::HV hv PPCODE: - if (HvKEYS(hv) > 0) { + if (HvUSEDKEYS(hv) > 0) { SV *sv; char *key; I32 len; (void)hv_iterinit(hv); - EXTEND(sp, HvKEYS(hv) * 2); + EXTEND(sp, HvUSEDKEYS(hv) * 2); while ((sv = hv_iternextsv(hv, &key, &len))) { mPUSHp(key, len); PUSHs(make_sv_object(aTHX_ sv)); diff --git a/hv.c b/hv.c index 56598d7..69b9290 100644 --- a/hv.c +++ b/hv.c @@ -800,7 +800,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen, xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */ if (!counter) { /* initial entry? */ } else if (xhv->xhv_keys > xhv->xhv_max) { - /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit + /* Use only the old HvUSEDKEYS(hv) > HvMAX(hv) condition to limit bucket splits on a rehashed hash, as we're not going to split it again, and if someone is lucky (evil) enough to get all the keys in one list they could exhaust our memory @@ -1625,7 +1625,7 @@ S_clear_placeholders(pTHX_ HV *hv, U32 items) if (--items == 0) { /* Finished. */ HvTOTALKEYS(hv) -= (IV)HvPLACEHOLDERS_get(hv); - if (HvKEYS(hv) == 0) + if (HvUSEDKEYS(hv) == 0) HvHASKFLAGS_off(hv); HvPLACEHOLDERS_set(hv, 0); return; @@ -2006,7 +2006,7 @@ S_hv_auxinit(HV *hv) { =for apidoc hv_iterinit Prepares a starting point to traverse a hash table. Returns the number of -keys in the hash (i.e. the same as C). The return value is +keys in the hash (i.e. the same as C). The return value is currently only meaningful for hashes without tie magic. NOTE: Before version 5.004_65, C used to return the number of @@ -2821,7 +2821,7 @@ S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags) xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */ if (!next) { /* initial entry? */ - } else if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) { + } else if (xhv->xhv_keys > xhv->xhv_max /* HvUSEDKEYS(hv) > HvMAX(hv) */) { hsplit(PL_strtab); } } diff --git a/mg.c b/mg.c index e821415..c5c58f2 100644 --- a/mg.c +++ b/mg.c @@ -1732,7 +1732,7 @@ Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg) if (hv) { (void) hv_iterinit(hv); if (! SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)) - i = HvKEYS(hv); + i = HvUSEDKEYS(hv); else { while (hv_iternext(hv)) i++; diff --git a/mro.c b/mro.c index 30be935..1185417 100644 --- a/mro.c +++ b/mro.c @@ -666,7 +666,7 @@ S_mro_clean_isarev(pTHX_ HV * const isa, const char * const name, if(svp) { HV * const isarev = (HV *)*svp; (void)hv_delete(isarev, name, len, G_DISCARD); - if(!HvARRAY(isarev) || !HvKEYS(isarev)) + if(!HvARRAY(isarev) || !HvUSEDKEYS(isarev)) (void)hv_delete(PL_isarev, key, klen, G_DISCARD); } } diff --git a/pp.c b/pp.c index d91faa4..0069fba 100644 --- a/pp.c +++ b/pp.c @@ -6339,7 +6339,7 @@ PP(pp_boolkeys) } } - XPUSHs(boolSV(HvKEYS(hv) != 0)); + XPUSHs(boolSV(HvUSEDKEYS(hv) != 0)); RETURN; } diff --git a/pp_hot.c b/pp_hot.c index f8a61cb..531a33e 100644 --- a/pp_hot.c +++ b/pp_hot.c @@ -999,7 +999,7 @@ PP(pp_aassign) || SvMAGICAL(sv) || ! (SvTYPE(sv) == SVt_PVAV || SvTYPE(sv) == SVt_PVHV) || (SvTYPE(sv) == SVt_PVAV && AvFILL((AV*)sv) != -1) - || (SvTYPE(sv) == SVt_PVHV && HvKEYS((HV*)sv) != 0) + || (SvTYPE(sv) == SVt_PVHV && HvUSEDKEYS((HV*)sv) != 0) ) ) { EXTEND_MORTAL(lastrelem - firstrelem + 1); diff --git a/t/benchmark/rt26188-speed-up-keys-on-empty-hash.t b/t/benchmark/rt26188-speed-up-keys-on-empty-hash.t index 155aa3f..4770382 100644 --- a/t/benchmark/rt26188-speed-up-keys-on-empty-hash.t +++ b/t/benchmark/rt26188-speed-up-keys-on-empty-hash.t @@ -86,5 +86,5 @@ __END__ /* quick bailout if the hash is empty anyway. I don't know if placeholders are included in the KEYS count, so a defensive check */ - if (! HvKEYS(hv) && !(flags & HV_ITERNEXT_WANTPLACEHOLDERS) ) + if (! HvUSEDKEYS(hv) && !(flags & HV_ITERNEXT_WANTPLACEHOLDERS) ) return NULL; -- ```

1.7.4.18.g68fe8


Flags​:   category=core   severity=low

p5pRT commented 13 years ago

From @cpansprout

On Sun May 15 18​:17​:06 2011\, mfwitten wrote​:

Subject​: [PATCH] Clean​: Actually use HvUSEDKEYS() instead of HvKEYS() Date​: Mon\, 14 Mar 2011 06​:57​:43 +0000 From​: Michael Witten \mfwitten@&#8203;gmail\.com

Thank you. Applied as 1b95d04f713d9c56.

p5pRT commented 13 years ago

The RT System itself - Status changed from 'new' to 'open'

p5pRT commented 13 years ago

@cpansprout - Status changed from 'open' to 'resolved'

p5pRT commented 13 years ago

From @cpansprout

On Wed May 18 16​:37​:14 2011\, sprout wrote​:

On Sun May 15 18​:17​:06 2011\, mfwitten wrote​:

Subject​: [PATCH] Clean​: Actually use HvUSEDKEYS() instead of HvKEYS() Date​: Mon\, 14 Mar 2011 06​:57​:43 +0000 From​: Michael Witten \mfwitten@&#8203;gmail\.com

Thank you. Applied as 1b95d04f713d9c56.

Alas\, that stopped Storable from working in 5.6. I’ve just fixed it with commit b33df119cd5e3.