Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

How about some way to inline subroutines at compile time. Sort of like other compile-time optimizations but with a little added smarts. "use constant" will currently inline a sub ref which is good and pretty fast. But it seems like it would be even faster to turn the inlined sub ref into a do block.

$ perl -Mstrict -Mwarnings -MO=Deparse -e ' use constant FOO => sub { join( shift, @_, ); }; printf( "%s\n", FOO->( ", ", 1 .. 10, ), ); ' use constant ('FOO', sub { BEGIN {${^WARNING_BITS} = "UUUUUUUUUUUU"} use strict 'refs'; join shift @_, @_; } ); BEGIN {${^WARNING_BITS} = "UUUUUUUUUUUU"} use strict 'refs'; printf "%s\n", sub { join shift @_, @_; } ->(', ', 1..10); -e syntax OK

Wouldn't it be nice if that were turned into this instead?

perl -Mstrict -Mwarnings -e ' printf "%s\n", do { local @_ = (", ", 1..10); join shift @_, @_; }; '

Update: Added benchmark so people can debunk/prove the need for this.

Update: Looks like I had the benchmarks backward and that a sub ref is actually faster than a do block. So, nevermind...

#!/usr/bin/env perl use strict; use warnings; use Benchmark(); Benchmark::cmpthese( -1, { sub_join_1 => sub { sub { join shift @_, @_; }->( ", ", 1 .. 100 ); + }, do_join_1 => sub { do { local @_ = ( ", ", 1 .. 100 ); join shift + @_, @_; }; }, } ); __END__ $ perl Rate do_join_1 sub_join_1 do_join_1 17935/s -- -73% sub_join_1 66991/s 274% --

In reply to Re: what would you like to see in perl5.12? by bennymack
in thread what would you like to see in perl5.12? by ysth

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the monks are mute...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2018-01-20 10:27 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (226 votes). Check out past polls.