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

inline keyword?

Thread Next
Ovid via perl5-porters
July 26, 2018 09:18
inline keyword?
Message ID:
I know people have tried to inline Perl subs before but with little success. Instead of going down that road, have we ever considered an inline keyword where the developer says what can be inlined? 


        my $total = 0;

        for ( 1 .. $int) {

            $total += reciprocal($_);


        return $total;


    inline reciprocal($int){

       return 1 / $int;



In the example above, inlining and replacing "reciprocal($_)" with "do { 1/$_ }" runs about 65% faster seconds (on my box).
Copying the args (do { my ($int) = $_; 1/$_ }) runs about 35% faster.
I've previously worked with a client that found a major speed-up in their big-data platform by inlining some code which, unfortunately, wound up being heavily duplicated as the sub contents where cut-n-pasted in several places. An "inline" keyword could handle this for them (though I realize this would be tricky). Initial thoughts:   
   - Would look just like sub   

   - Requires a single return statement as last statement   

   - Replaces subcall(@args) with an inline do {...} block matching the inline body.
   - Recursive inline: compile-time warning and no inlining, or compile-time exception (easier)
   - eval would be a pain
   - Probably lots more I haven't thought about
Naïve? Yes. But is it possible?
Best,Ovid-- IT consulting, training, specializing in Perl, databases, and agile development 
Buy my book! -

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