optimizer

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for Perl extension optimizer.

0.08  2012-02-28 <rurban@cpan.org>
        - for 5.15 set CopARYBASE_get(PL_curcop) to 0 (there's no ppport for this)

0.07  2011-09-02 Reini Urban <rurban@cpan.org>
        - prepend "_" to private subs
	- disable relocatetopad as the op_sv is wrong and the cv looks strange
	- added testcover to Makefile

0.06_07  2011-08-18 Reini Urban <rurban@cpan.org>
        - fix Artur's name

0.06_06  2011-08-17 Reini Urban <rurban@cpan.org>
        - t/04-use.t: 7-8 fail threaded at pad_alloc (TODO)

0.06_05  2010-08-28 Reini Urban <rurban@cpan.org>
	- explain CPAN test failures with B::Generate and PERL_DL_NONLAZY=1
	  If you want it, use it without PERL_DL_NONLAZY being set.
	- remove PERL_DL_NONLAZY=1 from our tests.

0.06_04  2010-08-24 Reini Urban <rurban@cpan.org>
	- added pod documentation
	- fix z_ tests
	- fix MANIFEST

0.06_03  2010-08-24 Reini Urban <rurban@cpan.org>
	all tests pass but 04-use.t 7+8 with threads in relocatetopad
	- fix compiler warnings
	- added eg/and_not.pl
	- add some minor patches from Jim Cromie (not his 0.06_02)
	- simplify OP_SUBST in c_extend_peep

0.06  2008-07-13 19:44:57 Reini Urban <rurban@cpan.org>
	- 5.10 and 5.11 patch, fixes for 5.8.8
	- Iop_seqmax and B::OP::seq workaround for 5.10 callbacks
	- added eg/pad.pl
	- added t/04-use.t

README  view on Meta::CPAN

    If you just want to use this function to get a callback after every code
    block is compiled so you can do any arbitrary work on it use the
    "sub-detect" option, you will be passed LEAVE* ops after the standard
    peep optimizer has been run, this minimises the risk for bugs as we use
    the standard one. The op tree you are handed is also stable so you are
    free to work on it. This is usefull if you are limited by "CHECK" and
    "INIT" blocks as this works with string eval and "require" aswell. Only
    one callback per package is allowed.

STATUS
    relocatetopad() fails with threaded perls.

5.10 Changes
    Since Perl 5.10 there are no op_seqmax and op_seq numbers in CORE
    anymore, so we add a package global op_seqmax for the op-tree numbering,
    for $B::OP::seq also. This is not thread-safe.

AUTHOR
      Simon Cozens, C<simon@cpan.org>

    Extended functionality and current maintainer:

optimizer.pm  view on Meta::CPAN

sub unimport {
    optimizer::install(sub {callbackoptimizer($_[0], sub{})});
}

sub callbackoptimizer {
    my ($op, $callback) = @_;
    while ($$op) {
	$op->seq(optimizer::op_seqmax_inc());
        _update($op) if $op->isa("B::COP");
	# crashes: wrong op_sv, strange cv
        #_relocatetopad($op, $op->find_cv()) if $op->name eq "const"; # For thread safety

        $callback->($op);
        $op = $op->next;
        last unless $op->can("next"); # Shouldn't get here
    }
}

sub peepextend {
    # Oh boy
    my ($o, $callback) = @_;

optimizer.pm  view on Meta::CPAN

    while ($$o) {
        #warn ("Trying op $o ($$o) -> ".$o->name."\n");
        if ($o->isa("B::COP")) {
            $o->seq(optimizer::op_seqmax_inc());
            _update($o); # For warnings

        } elsif ($o->name eq "const") {
            optimizer::_die("Bareword ",$o->sv->sv, " not allowed while \"strict subs\" in use")
                if ($o->private & 8);
	    # crashes: wrong op_sv, strange cv
            #_relocatetopad($o, $o->find_cv());
            $o->seq(optimizer::op_seqmax_inc());
        } elsif ($o->name eq "concat") {
            if ($o->next && $o->next->name eq "stringify" and !($o->flags &64)) {
                if ($o->next->private & 16) {
                    $o->targ($o->next->targ);
                    $o->next->targ(0);
                }
                #$o->null;
            }
            $o->seq(optimizer::op_seqmax_inc());

optimizer.pm  view on Meta::CPAN


=item unimport

Override with an empty B<callbackoptimizer>, effectively disabling any
installed optimizer.

=back

=head1 STATUS

relocatetopad fails with threaded perls.

=head1 5.10 Changes

Since Perl 5.10 there are no op_seqmax and op_seq numbers in CORE
anymore, so we add a package global op_seqmax for the op-tree
numbering, for $B::OP::seq also. This is not thread-safe.

=head1 AUTHOR

  Simon Cozens, C<simon@cpan.org>

optimizer.xs  view on Meta::CPAN


static void
install(pTHX_ SV* subref)
{
    /* We'll do the argument checking in Perl */
    PL_peepp = peep_callback;
    peep_in_perl = newSVsv(subref); /* Copy to be safe */
}

static void
_relocatetopad(pTHX_ OP* op, CV* cv)
{
#ifdef USE_ITHREADS
  /* Relocate const->op_sv to the pad for thread safety.
   * Despite being a "constant", the SV is written to,
   * for reference counts, sv_upgrade() etc. */
  if ((cc_opclass(aTHX_ op) == OPc_SVOP) && ((SVOP*)op)->op_sv) {
    SV** tmp_pad;
    AV* padlist;
    SV** svp;
    SVOP* o = (SVOP*)op;

optimizer.xs  view on Meta::CPAN

PEEP_install(SV* subref)
    CODE:
    install(aTHX_ subref);

void
PEEP_uninstall()
    CODE:
    uninstall(aTHX);

void
PEEP__relocatetopad(o,cvref)
      B::OP  o
      SV*  cvref
    CODE:
    if (cvref) { 
      CV* cv = INT2PTR(CV*, SvIV(SvRV(cvref)));
      _relocatetopad(aTHX_ o, cv);
    }

t/04-use.t  view on Meta::CPAN

   'enternextstatenullleave');
ok(`$X -Mblib -e'use optimizer callback => sub { print \$_[0]->name() }; 1;'`,
   'enternextstatenullleave');
ok(`$X -Mblib -e'use optimizer q(sub-detect) => sub { print \$_[0]->name() }; 1;'`,
   'leave');
ok(`$X -Mblib -e'use optimizer q(extend-c) => sub { print \$_[0]->name() }; 1;'`,
   'enternextstateleave');
ok(`$X -Mblib -e'use optimizer 'C'; print 1;'`,
   '1');

# the next two failed with ithreads in relocatetopad
#TODO: {
  #local $TODO = "fails with ithreads in pad_alloc" if $Config{useithreads};

  ok(`$X -Mblib -e'use optimizer 'perl'; print 1;'`,
     '1');
  ok(`$X -Mblib -e'no optimizer; print 1;'`,
     '1');
#}



( run in 1.106 second using v1.01-cache-2.11-cpan-5511b514fd6 )