This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use HvTOTALKEYS() in place of HvARRAY() in various boolean tests
authorNicholas Clark <nick@ccl4.org>
Fri, 3 Sep 2021 08:21:35 +0000 (08:21 +0000)
committerNicholas Clark <nick@ccl4.org>
Fri, 3 Sep 2021 10:37:55 +0000 (10:37 +0000)
This is a better choice for an "is it empty?" optimisation, as HvARRAY()
can be non-NULL even when there actually are no keys.

dump.c
hv.c
sv.c

diff --git a/dump.c b/dump.c
index c5c2d9e..07a84b7 100644 (file)
--- a/dump.c
+++ b/dump.c
@@ -686,7 +686,7 @@ Perl_dump_packsubs_perl(pTHX_ const HV *stash, bool justperl)
 
     PERL_ARGS_ASSERT_DUMP_PACKSUBS_PERL;
 
-    if (!HvARRAY(stash))
+    if (!HvTOTALKEYS(stash))
         return;
     for (i = 0; i <= (I32) HvMAX(stash); i++) {
         const HE *entry;
@@ -2207,12 +2207,12 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
         }
         if (nest < maxnest) {
             HV * const hv = MUTABLE_HV(sv);
-            STRLEN i;
-            HE *he;
 
-            if (HvARRAY(hv)) {
+            if (HvTOTALKEYS(hv)) {
+                STRLEN i;
                 int count = maxnest - nest;
                 for (i=0; i <= HvMAX(hv); i++) {
+                    HE *he;
                     for (he = HvARRAY(hv)[i]; he; he = HeNEXT(he)) {
                         U32 hash;
                         SV * keysv;
diff --git a/hv.c b/hv.c
index 05ed075..1738c78 100644 (file)
--- a/hv.c
+++ b/hv.c
@@ -1164,7 +1164,7 @@ S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
         }
     }
     xhv = (XPVHV*)SvANY(hv);
-    if (!HvARRAY(hv))
+    if (!HvTOTALKEYS(hv))
         return NULL;
 
     if (is_utf8 && !(k_flags & HVhek_KEYCANONICAL)) {
@@ -1809,7 +1809,7 @@ Perl_hv_clear(pTHX_ HV *hv)
     EXTEND_MORTAL(1);
     PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(hv);
     orig_ix = PL_tmps_ix;
-    if (SvREADONLY(hv) && HvARRAY(hv) != NULL) {
+    if (SvREADONLY(hv) && HvTOTALKEYS(hv)) {
         /* restricted hash: convert all keys to placeholders */
         STRLEN i;
         for (i = 0; i <= xhv->xhv_max; i++) {
diff --git a/sv.c b/sv.c
index 286abc5..8d66500 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -9905,7 +9905,7 @@ Perl_sv_resetpvn(pTHX_ const char *s, STRLEN len, HV * const stash)
 
     /* reset variables */
 
-    if (!HvARRAY(stash))
+    if (!HvTOTALKEYS(stash))
         return;
 
     Zero(todo, 256, char);
@@ -16235,7 +16235,7 @@ S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val)
 
     PERL_ARGS_ASSERT_FIND_HASH_SUBSCRIPT;
 
-    if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
+    if (!hv || SvMAGICAL(hv) || !HvTOTALKEYS(hv) ||
                         (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
         return NULL;