Skip to content

Commit

Permalink
remove repeated PL_stack_sp derefs in Perl_eval_sv/Perl_call_sv
Browse files Browse the repository at this point in the history
Reduce scope of local SP and remove excessive reads and writes to
PL_stack_sp in Perl_eval_sv/Perl_call_sv. EXTEND macro refetches the
possibly realloced SP on its own, Perl_stack_grow returns the new SP as a
retval and therefore in a register. By using PL_stack_sp instead of
Perl_stack_grow, an extra redundant mem read is done. Also dont keep
SP around for long periods unused, it wastes a C stack slot or non-vol
reg and makes the callframe bigger. The EXTEND could be placed
in the !(flags & G_METHOD_NAMED) branch, but that will be done in another
patch for bisectability.

VC 2003 -O1 machine code sizes of the functions
Perl_eval_sv before 0x28a after 0x286
Perl_call_sv before 0x3cd after 0x3cb

The savings look small since in x86 "*var+=4" and "var+=4" are the same
number of bytes to encode the instruction, only the mod R/M bitfield vals
are different. RISC CPUs benefit more from this patch.

commit c106c2b "G_METHOD_NAMED flag for call_method and call_sv"
added skipping the push SV onto stack

The EXTEND and PL_stack_sp direct manipulation code is from
commit a0d0e21 "perl 5.000". The reason is unknown why it did
"SV** sp = stack_sp;" and later "EXTEND(stack_sp, 1);" instead of using
SP, since EXTEND at that time, and to this day requires C auto sp be in
scope.
  • Loading branch information
bulk88 authored and tonycoz committed Oct 12, 2015
1 parent a0c8eb5 commit 5b434c7
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions perl.c
Expand Up @@ -2699,7 +2699,7 @@ I32
Perl_call_sv(pTHX_ SV *sv, VOL I32 flags)
/* See G_* flags in cop.h */
{
dVAR; dSP;
dVAR;
LOGOP myop; /* fake syntax tree node */
METHOP method_op;
I32 oldmark;
Expand Down Expand Up @@ -2729,9 +2729,14 @@ Perl_call_sv(pTHX_ SV *sv, VOL I32 flags)
SAVEOP();
PL_op = (OP*)&myop;

EXTEND(PL_stack_sp, 1);
if (!(flags & G_METHOD_NAMED))
*++PL_stack_sp = sv;
{
dSP;
EXTEND(SP, 1);
if (!(flags & G_METHOD_NAMED)) {
PUSHs(sv);
PUTBACK;
}
}
oldmark = TOPMARK;
oldscope = PL_scopestack_ix;

Expand Down Expand Up @@ -2842,9 +2847,8 @@ Perl_eval_sv(pTHX_ SV *sv, I32 flags)
/* See G_* flags in cop.h */
{
dVAR;
dSP;
UNOP myop; /* fake syntax tree node */
VOL I32 oldmark = SP - PL_stack_base;
VOL I32 oldmark;
VOL I32 retval = 0;
int ret;
OP* const oldop = PL_op;
Expand All @@ -2860,8 +2864,13 @@ Perl_eval_sv(pTHX_ SV *sv, I32 flags)
SAVEOP();
PL_op = (OP*)&myop;
Zero(&myop, 1, UNOP);
EXTEND(PL_stack_sp, 1);
*++PL_stack_sp = sv;
{
dSP;
oldmark = SP - PL_stack_base;
EXTEND(SP, 1);
PUSHs(sv);
PUTBACK;
}

if (!(flags & G_NOARGS))
myop.op_flags = OPf_STACKED;
Expand Down

0 comments on commit 5b434c7

Please sign in to comment.