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

inline keyword?

Thread Next
From:
Ovid via perl5-porters
Date:
July 26, 2018 09:18
Subject:
inline keyword?
Message ID:
1048903725.2613771.1532596722966@mail.yahoo.com
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? 

    subsum_reciprocals_to($int){

        my $total = 0;

        for ( 1 .. $int) {

            $total += reciprocal($_);

        }    

        return $total;

    }    

    inline reciprocal($int){

       return 1 / $int;

    }

    saysum_reciprocals_to(50_000_000);

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 developmenthttp://www.allaroundtheworld.fr/. 
Buy my book! - http://bit.ly/beginning_perl

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About