In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/f4adce6b773b2b6071e0d1627f4dce6ec0c3caa7?hp=2116934e2bf4f3c1445ea039e29ff377c3de2648>
- Log ----------------------------------------------------------------- commit f4adce6b773b2b6071e0d1627f4dce6ec0c3caa7 Author: Nicholas Clark <[email protected]> Date: Sun Jun 13 16:25:53 2010 +0200 Merge flags and argc parameters to S_tied_handle_method(). This generates slightly smaller object code overall, which means that the "hot" code (the non-overloaded paths through the ops) will be smaller, and hence more likely to stay in the CPU cache. M embed.fnc M pp_sys.c M proto.h commit 74f0b5509afd269c59f1396fde551295bbeec7d5 Author: Nicholas Clark <[email protected]> Date: Sun Jun 13 15:02:14 2010 +0200 In S_tied_handle_method() default to mortalizing extra arguments. Convert the gimme argument to a flags argument, and add a flag bit to signal that mortalization is not required. Only "BINMODE" needs this. M embed.fnc M pp_sys.c M proto.h commit 0240605ecd7a040cc6058409c65a3ebc6b7d489b Author: Nicholas Clark <[email protected]> Date: Sun Jun 13 12:38:16 2010 +0200 Add a gimme parameter to S_tied_handle_method(). This allows "GETC" to use it. M embed.fnc M pp_sys.c M proto.h commit bc0c81caab3813b2d61b70f94e5075bbf3a3ef69 Author: Nicholas Clark <[email protected]> Date: Sun Jun 13 12:12:43 2010 +0200 Change S_tied_handle_method() to varargs to allow extra SV parameters. This enables "BINMODE", "EOF" and "SYSSEEK" to use it. M embed.fnc M embed.h M pp_sys.c M proto.h commit ebc1fde647268c1d49a6096baf6ca8a708363f5b Author: Nicholas Clark <[email protected]> Date: Sun Jun 13 10:34:56 2010 +0200 Merge simple tied handle method calls into S_tied_handle_method(). M embed.fnc M embed.h M pp_sys.c M proto.h commit c33ef3ac654cbe35caea1d36f34c68f0e4a134ba Author: Nicholas Clark <[email protected]> Date: Sat Jun 12 22:29:03 2010 +0200 For tied file handle calls, use PUSH* when we know that the stack has space. Previously the code was using XPUSH* in many cases, often to push back values recently popped off the stack. M pp_sys.c commit 2addaaf3b439e1adb64bd833a257ff963c73f45a Author: Nicholas Clark <[email protected]> Date: Sat Jun 12 20:51:19 2010 +0200 Remove unecessary EXTEND()s from pp_sys.c If we've just POP*ed something from the stack, it will already have space. If we conditionally POP* something from the stack, avoid the EXTEND() in that path. Also, use PUSH() rather than XPUSH() where we know that the stack is large enough. Changes to pp_close, pp_binmode, pp_enterwrite, pp_readlink and pp_alarm. M pp_sys.c ----------------------------------------------------------------------- Summary of changes: embed.fnc | 3 + pp_sys.c | 185 ++++++++++++++++++++++++++++++++---------------------------- proto.h | 8 +++ 3 files changed, 110 insertions(+), 86 deletions(-) diff --git a/embed.fnc b/embed.fnc index 36d8c1a..e9287ea 100644 --- a/embed.fnc +++ b/embed.fnc @@ -1716,6 +1716,9 @@ s |OP* |doform |NN CV *cv|NN GV *gv|NN OP *retop sR |int |dooneliner |NN const char *cmd|NN const char *filename # endif s |SV * |space_join_names_mortal|NN char *const *array +so |OP * |tied_handle_method|NN const char *const methname|NN SV **sp \ + |NN IO *const io|NN MAGIC *const mg \ + |const U32 flags|... #endif #if defined(PERL_IN_REGCOMP_C) || defined(PERL_DECL_PROT) diff --git a/pp_sys.c b/pp_sys.c index 44b8cf4..1dadea8 100644 --- a/pp_sys.c +++ b/pp_sys.c @@ -552,28 +552,71 @@ PP(pp_open) RETURN; } +/* These are private to this function, which is private to this file. + Use 0x04 rather than the next available bit, to help the compiler if the + architecture can generate more efficient instructions. */ +#define MORTALIZE_NOT_NEEDED 0x04 +#define TIED_HANDLE_ARGC_SHIFT 3 + +static OP * +S_tied_handle_method(pTHX_ const char *const methname, SV **sp, + IO *const io, MAGIC *const mg, const U32 flags, ...) +{ + U32 argc = flags >> TIED_HANDLE_ARGC_SHIFT; + + PERL_ARGS_ASSERT_TIED_HANDLE_METHOD; + + /* Ensure that our flag bits do not overlap. */ + assert((MORTALIZE_NOT_NEEDED & G_WANT) == 0); + assert((G_WANT >> TIED_HANDLE_ARGC_SHIFT) == 0); + + PUSHMARK(sp); + PUSHs(SvTIED_obj(MUTABLE_SV(io), mg)); + if (argc) { + const U32 mortalize_not_needed = flags & MORTALIZE_NOT_NEEDED; + va_list args; + va_start(args, flags); + do { + SV *const arg = va_arg(args, SV *); + if(mortalize_not_needed) + PUSHs(arg); + else + mPUSHs(arg); + } while (--argc); + va_end(args); + } + + PUTBACK; + ENTER_with_name("call_tied_handle_method"); + call_method(methname, flags & G_WANT); + LEAVE_with_name("call_tied_handle_method"); + return NORMAL; +} + +#define tied_handle_method(a,b,c,d) \ + S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR) +#define tied_handle_method1(a,b,c,d,e) \ + S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR | (1 << TIED_HANDLE_ARGC_SHIFT),e) +#define tied_handle_method2(a,b,c,d,e,f) \ + S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR | (2 << TIED_HANDLE_ARGC_SHIFT), e,f) + PP(pp_close) { dVAR; dSP; GV * const gv = (MAXARG == 0) ? PL_defoutgv : MUTABLE_GV(POPs); + if (MAXARG == 0) + EXTEND(SP, 1); + if (gv) { IO * const io = GvIO(gv); if (io) { MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar); if (mg) { - PUSHMARK(SP); - XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg)); - PUTBACK; - ENTER_with_name("call_CLOSE"); - call_method("CLOSE", G_SCALAR); - LEAVE_with_name("call_CLOSE"); - SPAGAIN; - RETURN; + return tied_handle_method("CLOSE", SP, io, mg); } } } - EXTEND(SP, 1); PUSHs(boolSV(do_close(gv, TRUE))); RETURN; } @@ -653,14 +696,7 @@ PP(pp_fileno) if (gv && (io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) { - PUSHMARK(SP); - XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg)); - PUTBACK; - ENTER_with_name("call_FILENO"); - call_method("FILENO", G_SCALAR); - LEAVE_with_name("call_FILENO"); - SPAGAIN; - RETURN; + return tied_handle_method("FILENO", SP, io, mg); } if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) { @@ -726,20 +762,18 @@ PP(pp_binmode) if (gv && (io = GvIO(gv))) { MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar); if (mg) { - PUSHMARK(SP); - XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg)); - if (discp) - XPUSHs(discp); - PUTBACK; - ENTER_with_name("call_BINMODE"); - call_method("BINMODE", G_SCALAR); - LEAVE_with_name("call_BINMODE"); - SPAGAIN; - RETURN; + /* This takes advantage of the implementation of the varargs + function, which I don't think that the optimiser will be able to + figure out. Although, as it's a static function, in theory it + could. */ + return S_tied_handle_method(aTHX_ "BINMODE", SP, io, mg, + G_SCALAR|MORTALIZE_NOT_NEEDED + | (discp + ? (1 << TIED_HANDLE_ARGC_SHIFT) : 0), + discp); } } - EXTEND(SP, 1); if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) { if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) report_evil_fh(gv, io, PL_op->op_type); @@ -878,7 +912,7 @@ PP(pp_untie) CV *cv; if (gv && isGV(gv) && (cv = GvCV(gv))) { PUSHMARK(SP); - XPUSHs(SvTIED_obj(MUTABLE_SV(gv), mg)); + PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg)); mXPUSHi(SvREFCNT(obj) - 1); PUTBACK; ENTER_with_name("call_UNTIE"); @@ -1200,17 +1234,13 @@ PP(pp_getc) if (gv && (io = GvIO(gv))) { MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar); if (mg) { - const I32 gimme = GIMME_V; - PUSHMARK(SP); - XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg)); - PUTBACK; - ENTER; - call_method("GETC", gimme); - LEAVE; - SPAGAIN; - if (gimme == G_SCALAR) + const U32 gimme = GIMME_V; + S_tied_handle_method(aTHX_ "GETC", SP, io, mg, gimme); + if (gimme == G_SCALAR) { + SPAGAIN; SvSetMagicSV_nosteal(TARG, TOPs); - RETURN; + } + return NORMAL; } } if (!gv || do_eof(gv)) { /* make sure we have fp with something */ @@ -1268,14 +1298,15 @@ PP(pp_enterwrite) CV *cv = NULL; SV *tmpsv = NULL; - if (MAXARG == 0) + if (MAXARG == 0) { gv = PL_defoutgv; + EXTEND(SP, 1); + } else { gv = MUTABLE_GV(POPs); if (!gv) gv = PL_defoutgv; } - EXTEND(SP, 1); io = GvIO(gv); if (!io) { RETPUSHNO; @@ -2007,43 +2038,40 @@ PP(pp_eof) GV *gv; IO *io; MAGIC *mg; + /* + * in Perl 5.12 and later, the additional parameter is a bitmask: + * 0 = eof + * 1 = eof(FH) + * 2 = eof() <- ARGV magic + * + * I'll rely on the compiler's trace flow analysis to decide whether to + * actually assign this out here, or punt it into the only block where it is + * used. Doing it out here is DRY on the condition logic. + */ + unsigned int which; - if (MAXARG) + if (MAXARG) { gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */ + which = 1; + } else { EXTEND(SP, 1); - if (PL_op->op_flags & OPf_SPECIAL) + if (PL_op->op_flags & OPf_SPECIAL) { gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */ - else + which = 2; + } + else { gv = PL_last_in_gv; /* eof */ + which = 0; + } } if (!gv) RETPUSHNO; if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) { - PUSHMARK(SP); - XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg)); - /* - * in Perl 5.12 and later, the additional paramter is a bitmask: - * 0 = eof - * 1 = eof(FH) - * 2 = eof() <- ARGV magic - */ - EXTEND(SP, 1); - if (MAXARG) - mPUSHi(1); /* 1 = eof(FH) - simple, explicit FH */ - else if (PL_op->op_flags & OPf_SPECIAL) - mPUSHi(2); /* 2 = eof() - ARGV magic */ - else - mPUSHi(0); /* 0 = eof - simple, implicit FH */ - PUTBACK; - ENTER; - call_method("EOF", G_SCALAR); - LEAVE; - SPAGAIN; - RETURN; + return tied_handle_method1("EOF", SP, io, mg, newSVuv(which)); } if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */ @@ -2082,14 +2110,7 @@ PP(pp_tell) if (gv && (io = GvIO(gv))) { MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar); if (mg) { - PUSHMARK(SP); - XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg)); - PUTBACK; - ENTER; - call_method("TELL", G_SCALAR); - LEAVE; - SPAGAIN; - RETURN; + return tied_handle_method("TELL", SP, io, mg); } } else if (!gv) { @@ -2123,20 +2144,14 @@ PP(pp_sysseek) if (gv && (io = GvIO(gv))) { MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar); if (mg) { - PUSHMARK(SP); - XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg)); #if LSEEKSIZE > IVSIZE - mXPUSHn((NV) offset); + SV *const offset_sv = newSVnv((NV) offset); #else - mXPUSHi(offset); + SV *const offset_sv = newSViv(offset); #endif - mXPUSHi(whence); - PUTBACK; - ENTER; - call_method("SEEK", G_SCALAR); - LEAVE; - SPAGAIN; - RETURN; + + return tied_handle_method2("SEEK", SP, io, mg, offset_sv, + newSViv(whence)); } } @@ -3710,7 +3725,6 @@ PP(pp_readlink) #endif tmps = POPpconstx; len = readlink(tmps, buf, sizeof(buf) - 1); - EXTEND(SP, 1); if (len < 0) RETPUSHUNDEF; PUSHp(buf, len); @@ -4639,7 +4653,6 @@ PP(pp_alarm) int anum; anum = POPi; anum = alarm((unsigned int)anum); - EXTEND(SP, 1); if (anum < 0) RETPUSHUNDEF; PUSHi(anum); diff --git a/proto.h b/proto.h index 271107f..31ef03d 100644 --- a/proto.h +++ b/proto.h @@ -5350,6 +5350,14 @@ STATIC SV * S_space_join_names_mortal(pTHX_ char *const *array) #define PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL \ assert(array) +STATIC OP * S_tied_handle_method(pTHX_ const char *const methname, SV **sp, IO *const io, MAGIC *const mg, const U32 flags, ...) + __attribute__nonnull__(pTHX_1) + __attribute__nonnull__(pTHX_2) + __attribute__nonnull__(pTHX_3) + __attribute__nonnull__(pTHX_4); +#define PERL_ARGS_ASSERT_TIED_HANDLE_METHOD \ + assert(methname); assert(sp); assert(io); assert(mg) + #endif #if defined(PERL_IN_REGCOMP_C) || defined(PERL_DECL_PROT) -- Perl5 Master Repository
