Skip to content

Commit

Permalink
Refactor loops in S_hsplit(), Perl_hv_ksplit() and Perl_ptr_table_spl…
Browse files Browse the repository at this point in the history
…it().

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.
  • Loading branch information
nwc10 committed Jun 28, 2010
1 parent a50a349 commit 4c9d89c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
12 changes: 6 additions & 6 deletions hv.c
Expand Up @@ -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. */
Expand Down Expand Up @@ -1314,19 +1314,19 @@ 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) {
j -= i;
*oentry = HeNEXT(entry);
HeNEXT(entry) = aep[j];
aep[j] = entry;
continue;
}
else
oentry = &HeNEXT(entry);
}
entry = *oentry;
} while (entry);
}
}

Expand Down
12 changes: 7 additions & 5 deletions sv.c
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 4c9d89c

Please sign in to comment.