summaryrefslogtreecommitdiff
path: root/sv.c
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2021-07-20 20:24:41 +0000
committerNicholas Clark <nick@ccl4.org>2021-07-26 07:06:00 +0000
commitbd4f91e548cd03e60692e013e23c0fff31ead346 (patch)
treef40095af9bb58bcf093ed39b2316afcc64afc080 /sv.c
parent745e94a0ed91f09322e8188e2b5b0df8cd9aeca8 (diff)
downloadperl-bd4f91e548cd03e60692e013e23c0fff31ead346.tar.gz
Simplify some logic in S_find_hash_subscript().
The code is looping to look for the hash key whose value is the SV at the address given in val. Previously the code in the loop would ignore hash entries which were &PL_sv_undef or &PL_sv_placeholder. Given that this check was *after* the equality check for val, but the the only way for the function to return success ("found") was to continue beyond this check. Hence, as-written it meant that the function would always return NULL ("not found") if val had either of these values, but would loop through the entire hash *first* before doing so. Hence move the check before the loop, to generate the same result, but with less work. Also, HeKEY(entry) can never be NULL, so this check was dead code and can be removed. (The C compiler probably already spotted this) Also, the code had special-case handling for HEf_SVKEY. What it used to do was actually identical to what sv_2mortal(newSVhek(...)) does, so just use that for everything. (Hashes themselves should never contain keys with this special flag length - it's only used in MAGIC structures, and for HE structures created while iterating tied hashes.)
Diffstat (limited to 'sv.c')
-rw-r--r--sv.c15
1 files changed, 5 insertions, 10 deletions
diff --git a/sv.c b/sv.c
index e53f68c16e..b199e1a947 100644
--- a/sv.c
+++ b/sv.c
@@ -16201,21 +16201,16 @@ S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val)
(HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
return NULL;
+ if (val == &PL_sv_undef || val == &PL_sv_placeholder)
+ return NULL;
+
array = HvARRAY(hv);
for (i=HvMAX(hv); i>=0; i--) {
HE *entry;
for (entry = array[i]; entry; entry = HeNEXT(entry)) {
- if (HeVAL(entry) != val)
- continue;
- if ( HeVAL(entry) == &PL_sv_undef ||
- HeVAL(entry) == &PL_sv_placeholder)
- continue;
- if (!HeKEY(entry))
- return NULL;
- if (HeKLEN(entry) == HEf_SVKEY)
- return sv_mortalcopy(HeKEY_sv(entry));
- return sv_2mortal(newSVhek(HeKEY_hek(entry)));
+ if (HeVAL(entry) == val)
+ return sv_2mortal(newSVhek(HeKEY_hek(entry)));
}
}
return NULL;