From 716ae3b03e04a6b0ae19b882828eecf4ffc4c458 Mon Sep 17 00:00:00 2001 From: "bulk88 (via RT)" Date: Thu, 22 Nov 2012 16:33:34 -0800 Subject: [PATCH] av_exists: dont make a mortal never to use it Make av_exists slightly smaller and faster by reducing the liveness of a mortal SV and using a NN SvTRUE_nomg. There were 3 cases, where this mortal would be created, yet a return happened and the mortal went unused and was wasted. So move the mortal creation point closer to where it is first used. Also var sv will never be null, so use a NN version of SvTRUE_nomg created in commit [perl #115870]. The retbool line isn't actually required for optimization reasons, but was created just in case something in the future changes or some unknown compiler does something inefficiently. For me with 32 bit x86 VC 2003, before av_exists was 0x1C2, after 0x1B8. Comment from committer: Includes SvTRUE_nomg_NN from [perl #115870]. --- av.c | 21 ++++++++++++--------- sv.h | 1 + 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/av.c b/av.c index 6d2b949..cfa5399 100644 --- a/av.c +++ b/av.c @@ -934,7 +934,6 @@ Perl_av_exists(pTHX_ AV *av, I32 key) const MAGIC * const regdata_magic = mg_find((const SV *)av, PERL_MAGIC_regdata); if (tied_magic || regdata_magic) { - SV * const sv = sv_newmortal(); MAGIC *mg; /* Handle negative array indices 20020222 MJD */ if (key < 0) { @@ -948,14 +947,18 @@ Perl_av_exists(pTHX_ AV *av, I32 key) else return FALSE; } - - mg_copy(MUTABLE_SV(av), sv, 0, key); - mg = mg_find(sv, PERL_MAGIC_tiedelem); - if (mg) { - magic_existspack(sv, mg); - return cBOOL(SvTRUE_nomg(sv)); - } - + { + SV * const sv = sv_newmortal(); + mg_copy(MUTABLE_SV(av), sv, 0, key); + mg = mg_find(sv, PERL_MAGIC_tiedelem); + if (mg) { + magic_existspack(sv, mg); + { + I32 retbool = SvTRUE_nomg_NN(sv); + return cBOOL(retbool); + } + } + } } } diff --git a/sv.h b/sv.h index 35b360f..fe29a41 100644 --- a/sv.h +++ b/sv.h @@ -1709,6 +1709,7 @@ Like sv_utf8_upgrade, but doesn't do magic on C. #define SvTRUE(sv) ((sv) && (SvGMAGICAL(sv) ? sv_2bool(sv) : SvTRUE_common(sv, sv_2bool_nomg(sv)))) #define SvTRUE_nomg(sv) ((sv) && ( SvTRUE_common(sv, sv_2bool_nomg(sv)))) #define SvTRUE_NN(sv) (SvGMAGICAL(sv) ? sv_2bool(sv) : SvTRUE_common(sv, sv_2bool_nomg(sv))) +#define SvTRUE_nomg_NN(sv) ( SvTRUE_common(sv, sv_2bool_nomg(sv))) #define SvTRUE_common(sv,fallback) ( \ !SvOK(sv) \ ? 0 \ -- 2.7.4