Refactor loops in S_hsplit(), Perl_hv_ksplit() and Perl_ptr_table_split().
authorNicholas Clark <nick@ccl4.org>
Sun, 27 Jun 2010 21:38:58 +0000 (22:38 +0100)
committerNicholas Clark <nick@ccl4.org>
Mon, 28 Jun 2010 15:02:58 +0000 (16:02 +0100)
Change from for() to do ... while() loops. Move variable initialisation to
variable declaration. Avoid needing to use the comma operator to allow
multiple statements in for(). Avoid using a continue statement where it
isn't actually needed to change flow control. Avoid relying on the optimiser
to know that the for loop conditional doesn't need testing on the first pass.

A current gcc's optimiser produces identical code despite these changes.
However, for the reasons given I consider the code to be much clearer.

hv.c
sv.c

diff --git a/hv.c b/hv.c
index 8aaa23a..880a46d 100644 (file)
--- a/hv.c
+++ b/hv.c
@@ -1148,19 +1148,19 @@ S_hsplit(pTHX_ HV *hv)
        if (!entry)                             /* non-existent */
            continue;
        bep = aep+oldsize;
-       for (; entry; entry = *oentry) {
+       do {
            if ((HeHASH(entry) & newsize) != (U32)i) {
                *oentry = HeNEXT(entry);
                HeNEXT(entry) = *bep;
                *bep = entry;
                right_length++;
-               continue;
            }
            else {
                oentry = &HeNEXT(entry);
                left_length++;
            }
-       }
+           entry = *oentry;
+       } while (entry);
        /* I think we don't actually need to keep track of the longest length,
           merely flag if anything is too long. But for the moment while
           developing this code I'll track it.  */
@@ -1314,7 +1314,7 @@ Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
 
        if (!entry)                             /* non-existent */
            continue;
-       for (; entry; entry = *oentry) {
+       do {
            register I32 j = (HeHASH(entry) & newsize);
 
            if (j != i) {
@@ -1322,11 +1322,11 @@ Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
                *oentry = HeNEXT(entry);
                HeNEXT(entry) = aep[j];
                aep[j] = entry;
-               continue;
            }
            else
                oentry = &HeNEXT(entry);
-       }
+           entry = *oentry;
+       } while (entry);
     }
 }
 
diff --git a/sv.c b/sv.c
index cc8fe49..9a4a082 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -10875,20 +10875,22 @@ Perl_ptr_table_split(pTHX_ PTR_TBL_t *const tbl)
     tbl->tbl_max = --newsize;
     tbl->tbl_ary = ary;
     for (i=0; i < oldsize; i++, ary++) {
-       PTR_TBL_ENT_t **curentp, **entp, *ent;
-       if (!*ary)
+       PTR_TBL_ENT_t **entp = ary;
+       PTR_TBL_ENT_t *ent = *ary;
+       PTR_TBL_ENT_t **curentp;
+       if (!ent)
            continue;
        curentp = ary + oldsize;
-       for (entp = ary, ent = *ary; ent; ent = *entp) {
+       do {
            if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
                *entp = ent->next;
                ent->next = *curentp;
                *curentp = ent;
-               continue;
            }
            else
                entp = &ent->next;
-       }
+           ent = *entp;
+       } while (ent);
     }
 }