Skip to content

Commit 62c5e2a

Browse files
committed
hv.c: Inline macro to test if an HV has env magic
Previous code would invoke an entire `mg_find()` call just to check if the given HV happens to have `PERL_MAGIC_env`. This hardcodes knowledge of the `PERL_MAGIC_env` type in a lot of places that don't really care about it.
1 parent 6a4f62c commit 62c5e2a

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

Diff for: embed.fnc

+3
Original file line numberDiff line numberDiff line change
@@ -4564,6 +4564,9 @@ opx |void |sv_kill_backrefs \
45644564
|NN SV * const sv \
45654565
|NULLOK NOCHECK AV * const av
45664566
#endif
4567+
#if defined(PERL_IN_HV_C) || defined(PERL_IN_PP_HOT_C)
4568+
S |bool |hv_is_env |NN HV *hv
4569+
#endif
45674570
#if defined(PERL_IN_HV_C) || defined(PERL_IN_SV_C)
45684571
op |SV * |hfree_next_entry \
45694572
|NN HV *hv \

Diff for: embed.h

+3
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,9 @@
14251425
# define new_he() S_new_he(aTHX)
14261426
# endif
14271427
# endif /* defined(PERL_IN_HV_C) */
1428+
# if defined(PERL_IN_HV_C) || defined(PERL_IN_PP_HOT_C)
1429+
# define hv_is_env(a) S_hv_is_env(aTHX_ a)
1430+
# endif
14281431
# if defined(PERL_IN_LOCALE_C)
14291432
# define get_locale_string_utf8ness_i(a,b,c,d) S_get_locale_string_utf8ness_i(aTHX_ a,b,c,d)
14301433
# define ints_to_tm(a,b,c,d,e,f,g,h,i) S_ints_to_tm(aTHX_ a,b,c,d,e,f,g,h,i)

Diff for: hv.c

+8-12
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
604604
return (void *) entry;
605605
}
606606
#ifdef ENV_IS_CASELESS
607-
else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
607+
else if (hv_is_env(hv)) {
608608
U32 i;
609609
for (i = 0; i < klen; ++i)
610610
if (isLOWER(key[i])) {
@@ -669,7 +669,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
669669
return SvTRUE_NN(svret) ? (void *)hv : NULL;
670670
}
671671
#ifdef ENV_IS_CASELESS
672-
else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
672+
else if (hv_is_env(hv)) {
673673
/* XXX This code isn't UTF8 clean. */
674674
char * const keysave = (char * const)key;
675675
/* Will need to free this, so set FREEKEY flag. */
@@ -714,7 +714,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
714714
return NULL;
715715
}
716716
#ifdef ENV_IS_CASELESS
717-
else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
717+
else if (hv_is_env(hv)) {
718718
/* XXX This code isn't UTF8 clean. */
719719
const char *keysave = key;
720720
/* Will need to free this, so set FREEKEY flag. */
@@ -737,8 +737,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
737737
if (!HvARRAY(hv)) {
738738
if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
739739
#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
740-
|| (SvRMAGICAL((const SV *)hv)
741-
&& mg_find((const SV *)hv, PERL_MAGIC_env))
740+
|| (hv_is_env(hv))
742741
#endif
743742
) {
744743
char *array;
@@ -944,9 +943,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
944943

945944
not_found:
946945
#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
947-
if (!(action & HV_FETCH_ISSTORE)
948-
&& SvRMAGICAL((const SV *)hv)
949-
&& mg_find((const SV *)hv, PERL_MAGIC_env)) {
946+
if (!(action & HV_FETCH_ISSTORE) && hv_is_env(hv)) {
950947
unsigned long len;
951948
const char * const env = PerlEnv_ENVgetenv_len(key,&len);
952949
if (env) {
@@ -1196,7 +1193,7 @@ Perl_hv_pushkv(pTHX_ HV *hv, U32 flags)
11961193
HE *entry;
11971194
bool tied = SvRMAGICAL(hv) && (mg_find(MUTABLE_SV(hv), PERL_MAGIC_tied)
11981195
#ifdef DYNAMIC_ENV_FETCH /* might not know number of keys yet */
1199-
|| mg_find(MUTABLE_SV(hv), PERL_MAGIC_env)
1196+
|| hv_is_env(hv)
12001197
#endif
12011198
);
12021199
PERL_ARGS_ASSERT_HV_PUSHKV;
@@ -1348,7 +1345,7 @@ S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
13481345
return NULL; /* element cannot be deleted */
13491346
}
13501347
#ifdef ENV_IS_CASELESS
1351-
else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
1348+
else if (hv_is_env(hv)) {
13521349
/* XXX This code isn't UTF8 clean. */
13531350
keysv = newSVpvn_flags(key, klen, SVs_TEMP);
13541351
if (k_flags & HVhek_FREEKEY) {
@@ -3063,8 +3060,7 @@ Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
30633060
}
30643061
}
30653062
#if defined(DYNAMIC_ENV_FETCH) && defined(VMS) /* set up %ENV for iteration */
3066-
if (!entry && SvRMAGICAL((const SV *)hv)
3067-
&& mg_find((const SV *)hv, PERL_MAGIC_env)) {
3063+
if (!entry && hv_is_env(hv)) {
30683064
prime_env_iter();
30693065
}
30703066
#endif

Diff for: inline.h

+12
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ S_strip_spaces(pTHX_ const char * orig, STRLEN * const len)
283283
}
284284
#endif
285285

286+
/* ------------------------------- hv.h ------------------------------- */
287+
288+
#if defined(PERL_IN_HV_C) || defined(PERL_IN_PP_HOT_C)
289+
PERL_STATIC_INLINE bool
290+
S_hv_is_env(pTHX_ HV *hv)
291+
{
292+
PERL_ARGS_ASSERT_HV_IS_ENV;
293+
294+
return SvRMAGICAL((SV *)hv) && (bool)mg_find((SV *)hv, PERL_MAGIC_env);
295+
}
296+
#endif
297+
286298
/* ------------------------------- iperlsys.h ------------------------------- */
287299
#if ! defined(PERL_IMPLICIT_SYS) && defined(USE_ITHREADS)
288300

Diff for: pp_hot.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -2266,8 +2266,7 @@ S_padhv_rv2hv_common(pTHX_ HV *hv, U8 gimme, bool is_keys, bool has_targ)
22662266
keys then check its length, and whether we do either with or without
22672267
an %ENV lookup first. prime_env_iter() returns quickly if nothing
22682268
needs doing. */
2269-
if (SvRMAGICAL((const SV *)hv)
2270-
&& mg_find((const SV *)hv, PERL_MAGIC_env)) {
2269+
if (hv_is_env(hv))
22712270
prime_env_iter();
22722271
}
22732272
#endif

Diff for: proto.h

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)