Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Pre-compiled Perl?

by bbs2web (Novice)
on Apr 03, 2017 at 16:05 UTC ( #1186845=perlquestion: print w/replies, xml ) Need Help??
bbs2web has asked for the wisdom of the Perl Monks concerning the following question:

I was under the impression (probably miss guided) that Perl compiled scripts and stored the compiled code somewhere/somehow. If the modification time of the script hadn't changed it would subsequently not need to recompile every time the script was run.

I see some references to compiled Perl scripts but this primarily appears to be around avoiding having to rely on Perl on destination systems.

Are there ways of getting my code to execute faster?

perl -d:DProf ./test.pl 198.19.28.102 user password -d; dprofpp; { "data": [ { "{#BGPPEER}": "African Bank - HO - Neotel" }, { "{#BGPPEER}": "African Bank - HO - Teraco" }, { "{#BGPPEER}": "African Bank - Neotel - Midrand" }, { "{#BGPPEER}": "Liquid Telecoms - A" }, { "{#BGPPEER}": "Liquid Telecoms - B" } ] } Total Elapsed Time = 0.354916 Seconds User+System Time = 0.304916 Seconds Exclusive Times %Time ExclSec CumulS #Calls sec/call Csec/c Name 13.1 0.040 0.040 138 0.0003 0.0003 Class::MOP::Method::Gener +ated::_eval_closure 9.84 0.030 0.026 23 0.0013 0.0011 Moose::Util::TypeConstrai +nts::BEGIN 6.56 0.020 0.020 4 0.0050 0.0050 B::Hooks::EndOfScope::BEG +IN 6.56 0.020 0.038 15 0.0013 0.0026 Class::MOP::BEGIN 6.56 0.020 0.213 24 0.0008 0.0089 Moose::BEGIN 6.56 0.020 0.019 176 0.0001 0.0001 Class::MOP::Mixin::HasAtt +ributes::has_attribute 6.23 0.019 0.018 258 0.0001 0.0001 Class::MOP::Class::Immuta +ble::Class::MOP::Class::get_meta_instance 6.23 0.019 0.019 1106 0.0000 0.0000 Class::MOP::get_metaclass +_by_name 6.23 0.019 0.019 1062 0.0000 0.0000 Class::MOP::Package::_pac +kage_stash 5.90 0.018 0.018 1712 0.0000 0.0000 Package::Stash::PP::_deco +nstruct_variable_name 5.25 0.016 0.031 1168 0.0000 0.0000 Package::Stash::PP::get_s +ymbol 3.28 0.010 0.010 1 0.0100 0.0100 Net::SSLeay::load_error_s +trings 3.28 0.010 0.010 1 0.0100 0.0100 Params::Util::bootstrap 3.28 0.010 0.010 6 0.0017 0.0016 IO::Socket::BEGIN 3.28 0.010 0.010 14 0.0007 0.0007 Package::Stash::PP::BEGIN

Replies are listed 'Best First'.
Re: Pre-compiled Perl?
by hippo (Abbot) on Apr 03, 2017 at 16:34 UTC
    Are there ways of getting my code to execute faster?

    There are two obvious FAQ entries relating to this: How can I compile my Perl program into byte code or C? and How can I make my Perl program run faster? which may enlighten you.

    Perhaps the other obvious action you can take is to avoid Moose. Moose is a great, big, lumbering animal and adds a lot of overhead to any non-persistent system. Consider the lighter weight alternatives, or roll your own or else run the Moosish code as a daemon and have your script just interface with that.

Re: Pre-compiled Perl?
by afoken (Abbot) on Apr 03, 2017 at 17:35 UTC

    Getting rid of Moose sounds like a good idea. There are lighter variations (Moo, Mouse) that may work, but perl can do OO just fine without extra modules.

    Also consider using Devel::NYTProf for a much more detailed analysis of your code. Devel::DProf is deprecated, and Devel::NYTProf is the suggested replacement.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Pre-compiled Perl?
by shmem (Chancellor) on Apr 03, 2017 at 16:48 UTC
    Are there ways of getting my code to execute faster?

    Yes of course. I don't know how complex your script is, and what tasks it is meant to perform. But from the profiling you can clearly see that the most time is spent with setting up Moose, which is quite heavy and best suited for longer running, complex applications. Check whether you can do without.

    Once upon a time, there was a bytecode compiler for perl, but that project has been discontinued. So sorry - no precompiled perl (afaik any "precompiled perl" nowadays isn't, that's just the perl binary, the necessary modules and the script lumped together, with or without obfuscation.)

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      > Once upon a time, there was a bytecode compiler for perl, but that project has been discontinued.

      IIRC it didn't worth it anymore because systems became so fast that compiling didn't count much ... Or at least less than fetching from disc.

      For completeness: We had a discussion if precompiling Moose would help (it wouldn't AFAIR) ... Will add a link later.

      updated links

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        What happened to perlcc? was a very fun node! thanks for that.

        Anyway if i recall P6 was announced with the ability to write out machine-indipendent code (is that called bytecode?) many years ago: are you or other aware if this intention was removed from the smiling butterfly too?



        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Pre-compiled Perl?
by Laurent_R (Canon) on Apr 03, 2017 at 16:44 UTC
    I was under the impression (probably miss guided) that Perl compiled scripts and stored the compiled code somewhere/somehow. If the modification time of the script hadn't changed it would subsequently not need to recompile every time the script was run.
    No, that's wrong. The short answer is that a Perl script is recompiled each time it runs.
      Sort of picking nits here, but AFAIK Perl5 is never really "compiled." It's executed directly from the parse tree. The parse tree is bulky and full of pointers, making it difficult to store and reload later. (Back in the day, there was a dump/undump mechanism that tried to do this, but it hasn't worked for a long time.)
        Yeah, I understand what you mean, but, to me, creating the parse tree is a form of compilation. The difference between compiled languages and interpreted languages was meaningful 20 or 25 years ago, but that distinction is very much blurred nowadays.

        Contrary to some other languages (e.g. shell, TCL, awk, makefile), Perl has clearly a compile phase and a run time phase. Whether the compile phase produces an op tree or an executable file has become to a large extent irrelevant in my view. But, yes, it's not really a compilation in the sense of what you do with a C program, you don't produce a binary executable program.

Re: Pre-compiled Perl?
by danaj (Friar) on Apr 04, 2017 at 16:19 UTC

    Perl quickly demi-compiles everything to an intermediate representation every time you run a program. It doesn't save this intermediate. "Quickly" is of course relative, but it's typically a non-issue on modern computers with reasonable size programs. It was different in the days of 486's (oh the joys of dynamic loading modules in Perl 4 to save startup cost), and it can be an issue with many-thousand line programs. Also a non-issue for most people if your program runs a long time, as do many web frameworks. There is also actual work done for startup on some modules, which is typically very fast but it can add up.

    One place it comes up a lot is with small command-line scripts written using Moose. Moose is awesome, but very heavy. Fortunately there is Moo which is a near drop-in replacement for Moose that is much lighter weight. Install Class::XSAccessor as well for a substantial performance improvement for method get/set calls. Moo is, of course, not a complete replacement for Moose, but it does an awful lot. Try it.

    You could also try Mouse. These days it isn't as recommended because Moo solves the problem for most people, and Mouse has its own quirks. But it is a lot faster than Moose, so you could give it a try to see if it's worth investigating. It may be as simple as replacing "Moose" with "Mouse" in your files.

    There are mechanisms for compiled Perl, or saving the state after the compile phase to avoid the startup, but they mostly just save you the startup time, and have a dodgy history of working. I would recommend against going this direction unless you understand the problem well enough to know this is something you really want. Rieni Urban does some work for CPanel on a compiled Perl but I'm really not up on its status. Restricted Perls such as RPerl are unlikely to help given your problems seem to be with Moose.

    I really recommend as well using Devel::NYTProf. It should give a better understanding of what is taking your time. Regardless of changing the OO framework (Moose/Mouse/Moo/Mo/bless), it may be that you're doing something obviously slow once you see the graphs (e.g. maybe you're calling a method get for the same value in a tight loop and could just cache it, or your type constraints are killing you and maybe there's a faster way).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1186845]
Approved by Corion
Front-paged by Discipulus
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2017-12-15 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (443 votes). Check out past polls.

    Notices?