develooper Front page | perl.perl5.porters | Postings from September 2012

Named prototypes (again) (was: Optimize assigning to scalars from @_)

Peter Martini
September 2, 2012 12:38
Named prototypes (again) (was: Optimize assigning to scalars from @_)
Message ID:
I know its not directly related to this RT ticket (which is why I changed
the subject), but it's pretty close - I'd asked about adding named
prototypes 2 months or so ago, and I've got it mostly working (although I'm
now reworking it a bit in light of Father C's recent PADLIST changes).

What I'd proposed, and mostly implemented so far, is:

1. When parsing a prototype, if an alphanumeric is detected, restart the
parsing as a list of named parameters.
2. The named parameter list would be what the various modules seem to have
converged on:
    a comma separated list of [qualifier list] [white space] [sigil]
[name], eg
    my $a, my $b, my $c
    And if no qualifier is specified, assume 'my'
3. The last parameter can be greedy, @ or %; otherwise everything must
start with $
    A % would die if an odd number of parameters are listed to construct it
4. All parameters are optional, and will be declared but undefined if not
passed in.
5. (The syntax would means in the future we could allow my $var = 5, to set
a default, but that's a can of worms I don't want to think about now)
6. An additional sub attribute, proto, which can be used to specify the
traditional proto definition:
    sub something(my $a, my $b, my $c) : proto($$$) { }
7. @_ would not be modified in anyway, so $_[0] accesses or even my ($a2,
$b2, $c2) = @_ would still work, if someone wanted to.

Anyway, in the simplest (and I think default) case of sub ($a, $b, $c) { },
this would be exactly equivalent to the sub { my ($a, $b, $c) = @_; } case
mentioned, independent of any additions to the optimizer or re-writing ops
- everything would be handled by a flag on the CV and assignment in

Thoughts?  And yes, I can post the code I've implemented and my TODO list
if a working, tested version of the above might make it into blead.

On Sun, Sep 2, 2012 at 3:03 PM, Dave Mitchell <> wrote:

> On Sun, Sep 02, 2012 at 11:05:02AM -0700, Father Chrysostomos via RT wrote:
> > Optimising my($foo,$bar)=... in general might be possible.  The speed
> > gain might be negligible, though, as the padsv op is already super-fast.
> I already have a plan for this. The speedup might actually be
> considerable, both in terms of replacing several ops with a single one,
> and in terms of making the all the individual SAVEt_CLEARSV's into a
> single "SAVE pad index range" operation that is pushed on the savestack
> once and popped once.
> In more detail: the basic idea is to is to replace the expression
>     my (X,Y,Z)
> with a single op that contains a range of pad indexes. It will also have
> a flag bit to indicate whether it should get @_ at the same time. So,
>     my ($a,$b,$c) = @_
> goes from being
>     pushmark s
>     gv[*_] s
>     rv2av[t5] lK/1
>     pushmark sRM*/128
>     padsv[$a:1,2] lRM*/LVINTRO
>     padsv[$b:1,2] lRM*/LVINTRO
>     padsv[$c:1,2] lRM*/LVINTRO
>     aassign[t6] vKS
> to
>     padlist[$a..$c]
>     aassign[t6] vKS
> The new op's action is to
>     1. If the SPECIAL flag is set, push GvAV(PL_defgv);
>     2. if not in void context, then
>             for (index_min..index_max) { push PL_curpad[$_]};
>     3. push a single item onto the savestack, which is a
>        SAVEt_CLEARSV_RANGE (say) that holds a pointer to the pad index list
>        (rather than pushing a whole separate bunch of SAVEt_CLEARSV's)
>        At scope exit time there will thus be only one entry to pop from
>        the scope stack.
>     4. Do appropriate PUSHMARKs as needed.
> I think this could be a big performance win. It speeds up sub entry:
>     my ($a,$b,$c) = @_;
> general assignments:
>     my ($a,$b,$c) = /.../g;
> and even just void declarations:
>     my ($a,$b,$c);
> (this latter avoiding a bunch of pointless retrieving lexicals from
> the pad and pushing them on the stack.)
> I plan to look into this further once I've finished off the regex copying
> stuff.
> --
> All wight. I will give you one more chance. This time, I want to hear
> no Wubens. No Weginalds. No Wudolf the wed-nosed weindeers.
>     -- Life of Brian
> Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About