Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

[CLOSED] Reusing Compiled Perl or Opcode for Apache Server

by mlodato (Acolyte)
on Aug 16, 2017 at 00:36 UTC ( [id://1197477]=perlquestion: print w/replies, xml ) Need Help??

mlodato has asked for the wisdom of the Perl Monks concerning the following question:

O wise ones, I have come to you in humility to ask a question for which I have little surrounding knowledge. Please take pity on me and ask for clarification so I can seek information to bring back to you.

I work on a project that has a Perl back end behind an Apache server. The development servers are very slow. For certain production-like servers, we precache our static content and preload our Perl modules. Doing this takes a long time. It would be nice if it didn't take so long a second time if the Perl code hasn't changed.

I was wondering - is there was a way to preload all of the Perl modules once and then serialize it in some way to be read in more quickly a second time?

Note that I don't yet know exactly what "preloading Perl modules" means, but I am actively looking into it and maybe you don't need that information to answer the question because preloading is a common term.

I have seen several posts saying that compiling Perl into C-like code is not yet a thing. That's fine, I'm not looking to optimize or hide the code. I have seen several posts saying that I can generate an executable with PAR::Packer. Maybe this can be used? I have seen several posts saying that Perl is first parsed into opcode before being run. I'm not sure if this is just for Perl 6, but if it's for Perl 5, I see no reason for that opcode to not be reusable...right? I have seen a post explaining that Perl can't be parsed. I find this confusing.

Edit: After some digging it looks to my untrained eyes like "preloading" just calls use, load_class from Class::Load, and ->new for each module

Replies are listed 'Best First'.
Re: Reusing Compiled Perl or Opcode for Apache Server
by LanX (Saint) on Aug 16, 2017 at 02:19 UTC
    I'm not sure what you want.

    If you run a CGI on your Apache which is loading many modules, do you probably need mod_perl or fastcgi?

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

        Thanks Alexander!

        I looked into these a little and it looks like we don't want FastCGI and already use mod_perl :-\

      Thanks Rolf!

      So we currently use mod_perl. We don't use fastcgi. I'm not exactly sure why but there's a comment mentioning "unconfirmed strange behavior". Basically, when we start up a server, it calls use on all the packages/modules so that they're in memory and available quickly when a response needs to be processed. I was wondering if there was a way to store the result of all these use calls in a way that would make it faster to load next time.

      Does that make any more sense? Apologies if it doesn't...

        I agree with RonW in that it is likely to be impossible FAPP. However, there are a number of alternative strategies which you could employ:

        • Don't load any modules at server start. If server start-up time is more important to you than the response time of an initial request then just don't bother with all this preloading.
        • Don't load all the modules, just the lighter and more frequently-used ones. This is the compromise option.
        • Don't keep stopping and starting the server. Just let it run and use Apache2::Reload to pick up code changes.
        • Live with it. How long does it take anyway? 3 seconds? 5? If more than that there may be other issues at play which you haven't disclosed. Apache (even with mod_perl2) isn't JBoss - it should only take a few seconds at most to start up.
        I was wondering if there was a way to store the result of all these use calls in a way that would make it faster to load next time.

        For a mod_perl application, I think it's impossible. Besides the many problems presented by Perl itself, mod_perl and Apache impose additional problems.

        For a stand-alone Perl application, it's remotely possible, depending on what the modules loaded by use do and how. Also, how your Perl application uses those modules, what your application is doing and how it does it.

        If any of those modules require shared libraries (also known as DLLs), probably impossible. There are also other things that use-ing a module can do that make loading a saved cache impossible.

        The problems arise because in Perl use doesn't simply load a module. Modules can and often do have initialization code that has effects that can't be saved and reloaded. They have to be done every time the module is loaded.

        Ideally, modules should be written to separate initialization from loading, but (1) initialization is often a big chuck of the "loading" time, (2) most people want the convenience of the automatic initialization and (3) too few people have a need to defer initialization that the extra logic to support a "load only" option in modules isn't worth the effort to maintain.

        It's very likely to be impossible. I can understand that your employer may want you to try. You could look at https://metacpan.org/pod/distribution/B-C/script/perlcc.PL, but very doubtful anyone will be able to help you. Even if you were to write a new application completely on your own, it would still be very difficult to make the end result work correctly.

        > Does that make any more sense?

        Not really. Mod_Perl is already caching at startup and is fast for every http request. (If not then you are doing it wrong)

        Frequently restarting Apache doesn't make sense, sorry.

        This rather sounds like XY problem.

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

Re: Reusing Compiled Perl or Opcode for Apache Server
by Laurent_R (Canon) on Aug 16, 2017 at 06:28 UTC
    Hi mlodato,

    we need to know more.

    Does your application use one of the usual web framework such as Mojolicious, Catalyst, or Dancer?

    Or is it using older technologies such as CGI?

      Unfortunately we don't use any of the newer frameworks. It looks like we use CGI and mod_perl, but everything else is home-grown

Re: Reusing Compiled Perl or Opcode for Apache Server
by Mr. Muskrat (Canon) on Aug 16, 2017 at 21:17 UTC

    Take a look at PerlPostConfigRequire in the mod_perl2 docs. You write a short Perl script that preloads any modules you want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1197477]
Approved by beech
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found