develooper Front page | perl.perl5.porters | Postings from July 2016

merging optimising sub signature work so far

Thread Next
Dave Mitchell
July 22, 2016 21:23
merging optimising sub signature work so far
Message ID:
I'm currently smoking this branch:


which if all goes well and there are no objections, I plan to then merge
into blead.

This branch represents kind of half the total work I intend to do for now
on subroutine signatures, and is a convenient point where I think it
can be merged into blead as an integrated whole.

What this branch does:

*  Moves the parsing of the signature (i.e. the '($a,$b=1,@c)' thing)
   from custom code in toke.c to normal yacc rules in perly.y;

*  creates three new ops, OP_ARGCHECK, OP_ARGELEM, OP_ARGDEFELEM,
   which are intended to (relatively) efficiently implement the checking
   of subroutine args and assigning them to lexical vars, with default
   handling.  Each signatured sub starts with a single OP_ARGCHECK which
   checks for the correct numbers of args, followed by a number of
   OP_ARGELEM ops, one per parameter. Each of these ops does the
   equivalent of 'my $x = $_[N];'. The op may have an OP_ARGDEFELEM
   child, which does the equivalent of '@_ > N ? $_[N] : defexpr', where
   defexpr is the child of the OP_ARGDEFELEM.

   These ops are generated during parsing, so are visible to hooks and to
   the peephole optimiser. They are intended to be a simple and convenient
   representation of the signature.

   For example, this:

         sub f ($a, $b = $a + 1, @c) { }

   is now compiled to this:

        e  <1> leavesub[1 ref] K/REFC,1 ->(end)
        -     <@> lineseq KP ->e
        1        <;> nextstate(main 83 p:5) v:%,469762048 ->2
        2        <+> argcheck(2,1,@) v ->3
        3        <;> nextstate(main 80 p:5) v:%,469762048 ->4
        4        <+> argelem(0)[$a:80,83] v/SV ->5
        5        <;> nextstate(main 81 p:5) v:%,469762048 ->6
        a        <+> argelem(1)[$b:81,83] vKS/SV ->b
        6           <|> argdefelem(other->7)[1] sK ->a
        9              <2> add[t3] sK/2 ->a
        7                 <0> padsv[$a:80,83] s ->8
        8                 <$> const(IV 1) s ->9
        b        <;> nextstate(main 82 p:5) v:%,469762048 ->c
        c        <+> argelem(2)[@c:82,83] v/AV ->d
        d        <;> nextstate(main 83 p:5) :%,469762048 ->e

*, now outputs pragmas before subs. This:

        use strict;
        sub f {

    used to deparse as

        sub f {
            use strict;
        use strict;

    it now deparses the same as the src code. This is important
    for 'use feature "signatures"'.

*   When deparsing a sub, it now chooses the format of the prototype and
    signature based on whether 'use feature "signatures"' is in scope:
    a proto of ($$) becomes :prototype($$), and a series of OP_ARG*
    ops at the start of the function are now deparsed as a ($a,$b,...) style
    signature if possible, rather than as a series of 'my $a = $[0];'

*   Fixes up a whole bunch of other deparsing issues, cutting down the
    number of unexpected failing test scripts from about 190 to 78,
    and the expected failures from 210 to 182.

What this branch doesn't do, but what I intend to follow up with before
too long:

*  Suppressing @_ population for suitable permutations of pragmata (exact
   details still TBD);

*  In the peephole optimiser, converting a series of OP_ARGELEM etc ops
   into a single OP_SIGNATURE (or whatever I end up calling it) op
   that will be very similar to the op from my OP_SIGNATURE branch
   from last year; except that now it will be possible for hooks to
   modify the optree before an OP_SIGNATURE is output, and if the
   optree is not representable as a single OP_SIGNATURE op, then
   it is left as a series of OP_ARGELEMs that will still execute (albeit
   more slowly), and still deparse.

*  In the peephole optimiser, convert things like 'my ($a,$b,@c) = @_'
   to use OP_SIGNATURE, so that non-signatured subs get a performance
   boost too. Again this will mostly be based on my older OP_SIGNATURE

You're only as old as you look.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About