Skip to content

Commit

Permalink
pp_(i_)negate: Don't needlessly call S_negate_string
Browse files Browse the repository at this point in the history
Arguably it will be more common for users to want to negate an IV or NV,
rather than a PV. Prior to this commit, `S_negate_string` was always called
at the start of the OP and, if the target wasn't `SvPOKp`, that function
just immediately returned. This commit moves the `SvPOKp` check into the
`pp_` functions to avoid needless function calls.
  • Loading branch information
richardleach committed Sep 7, 2023
1 parent 359c2cd commit c904517
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pp.c
Expand Up @@ -2652,8 +2652,11 @@ S_negate_string(pTHX)
STRLEN len;
const char *s;
SV * const sv = *PL_stack_sp;
if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))

assert(SvPOKp(sv));
if (SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
return FALSE;

s = SvPV_nomg_const(sv, len);
if (isIDFIRST(*s)) {
if (LIKELY(TARG!=sv)) {
Expand Down Expand Up @@ -2681,11 +2684,12 @@ PP(pp_negate)
if (rpp_try_AMAGIC_1(neg_amg, AMGf_numeric))
return NORMAL;

if (S_negate_string(aTHX))
SV * const sv = *PL_stack_sp;

if (SvPOKp(sv) && S_negate_string(aTHX))
return NORMAL;

{
SV * const sv = *PL_stack_sp;

if (SvIOK(sv)) {
/* It's publicly an integer */
Expand Down Expand Up @@ -3056,11 +3060,11 @@ PP(pp_i_negate)
if (rpp_try_AMAGIC_1(neg_amg, 0))
return NORMAL;

if (S_negate_string(aTHX))
return NORMAL;
SV * const sv = *PL_stack_sp;

if (SvPOKp(sv) && S_negate_string(aTHX))
return NORMAL;
{
SV * const sv = *PL_stack_sp;
IV const i = SvIV_nomg(sv);
TARGi((IV)-(UV)i, 1);
if (LIKELY(targ != sv))
Expand Down

0 comments on commit c904517

Please sign in to comment.