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 bench_do_sub_1.pl
Rate do_join_1 sub_join_1
do_join_1 17935/s -- -73%
sub_join_1 66991/s 274% --
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.