1 |
#include "EXTERN.h" |
2 |
#include "perl.h" |
3 |
#include "XSUB.h" |
4 |
|
5 |
typedef CV *B__CV; |
6 |
|
7 |
static OP *(*old_entersub)(pTHX); |
8 |
|
9 |
int count = 1; |
10 |
|
11 |
// this is, of course, a slower entersub |
12 |
static OP * |
13 |
faster_entersub (pTHX) |
14 |
{ |
15 |
dSP; |
16 |
dTOPss; |
17 |
|
18 |
if (SvTYPE (sv) == SVt_PVGV) |
19 |
sv = (SV *)GvCV (sv); |
20 |
|
21 |
if (sv) |
22 |
{ |
23 |
// only once for now |
24 |
PL_op->op_ppaddr = old_entersub; |
25 |
|
26 |
// only simple cv calls for now |
27 |
if (!PL_perldb && !PL_tainting |
28 |
&& SvTYPE (sv) == SVt_PVCV && !CvXSUB (sv) |
29 |
&& CvSTART (sv) // must exist |
30 |
&& CvSTART (sv)->op_type != OP_NULL) // shield against compiling an already-compiled op |
31 |
{ |
32 |
SV *bsv = newSViv (PTR2IV (sv)); |
33 |
|
34 |
ENTER; |
35 |
SAVETMPS; |
36 |
PUSHMARK (SP); |
37 |
// emulate B::CV typemap entry we don't have |
38 |
XPUSHs (sv_2mortal (sv_bless (newRV_noinc (bsv), gv_stashpv ("B::CV", 1)))); |
39 |
PUTBACK; |
40 |
call_pv ("Faster::entersub", G_VOID|G_DISCARD|G_EVAL); |
41 |
SPAGAIN; |
42 |
FREETMPS; |
43 |
LEAVE; |
44 |
} |
45 |
} |
46 |
|
47 |
return old_entersub (aTHX); |
48 |
} |
49 |
|
50 |
MODULE = Faster PACKAGE = Faster |
51 |
|
52 |
PROTOTYPES: ENABLE |
53 |
|
54 |
IV |
55 |
ppaddr (int optype) |
56 |
CODE: |
57 |
RETVAL = optype == OP_ENTERSUB |
58 |
? old_entersub |
59 |
: PL_ppaddr [optype]; |
60 |
OUTPUT: |
61 |
RETVAL |
62 |
|
63 |
void |
64 |
hook_entersub () |
65 |
CODE: |
66 |
old_entersub = PL_ppaddr [OP_ENTERSUB]; |
67 |
PL_ppaddr [OP_ENTERSUB] = faster_entersub; |
68 |
|
69 |
void |
70 |
patch_cv (B::CV cv, void *ptr) |
71 |
CODE: |
72 |
{ |
73 |
OP *op; |
74 |
|
75 |
NewOp (0, op, 1, OP); |
76 |
|
77 |
op->op_sibling = CvSTART (cv); |
78 |
op->op_type = OP_NULL; |
79 |
op->op_ppaddr = ptr; |
80 |
|
81 |
CvSTART (cv) = op; |
82 |
} |
83 |
|
84 |
|