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

Hi,

I have a script running on a webserver using the modules below. The problem is this seems to cause my script to delay before it gets run. Is there anyway to speed this up, eg, is there a way to load the modules just once so that they're ready to be used by my script?

Thanks for any help.

JS.

use CGI qw(:standard escapeHTML); use Net::LDAP; use Net::LDAP::Constant qw( LDAP_REFERRAL LDAP_CONSTRAINT_VIOLATION LD +AP_COMPARE_TRUE); use URI; use URI::ldap; use Date::Manip; use POSIX qw(strftime); use Log::Dispatch; use Log::Dispatch::File; use Log::Dispatch::Email::MailSendmail; use HTML::Entities; use strict; no strict "vars";

Replies are listed 'Best First'.
Re: modules slowing webpage
by dragonchild (Archbishop) on May 10, 2004 at 15:03 UTC
    Look at mod_perl. This is exactly what it's designed to do.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: modules slowing webpage
by matija (Priest) on May 10, 2004 at 15:04 UTC
    Yes there is. It's called mod_perl.

    It not only pre-loads your modules, it keeps your program in memory, so it doesn't need to reload your program either.

    There are some issues that you have to be carefull with when you use mod_perl, but if you use strict, most of them are moot.

Re: modules slowing webpage
by eric256 (Parson) on May 10, 2004 at 15:15 UTC

    Having seen the comments (and expecting them as i read the post) I wondered if there isn't a different way? I know that you can use storable to store and load stuff directly to memory (much faster than Data::Dumper). Could this process be used to store the 'loaded' version of all the modules? Kinda store the whole programs initial state to memory?


    ___________
    Eric Hodges
      If you really can't use mod_perl, then you could try to defer the module loading. Normally this is done by removing the "use" statements and put "require" statements at the point where the functionality is needed. Of course this is error prone and you have to fully qualify function calls.

      Maybe it's worth to take a look at the autouse module, which automates the deferred load of modules.

      I know that you can use storable to store and load stuff directly to memory (much faster than Data::Dumper). Could this process be used to store the 'loaded' version of all the modules?

      Over the last decade there have been several implementations along those lines, at first involving dumping a memory snapshot for the process, and then more recently using stored bytecode with the B modules.

      However, for production web work, I think mod_perl's a better answer.

        mod_perl is only a better answer if its an option. Yes I know move to a different host if your doesn't have mod_perl, but then thats not realy an answer is it? Perhaps it could be considered one. The real question is why do we have to wait for perl to compile a module, i mean its going to be compiled the same everytime isn't it? At least on the same system, so why not save it right after that compiling process? Its possible, maybe even likely, that this is impossible or just very difficult, or just impractical. Could someone with more intimate knowledge of perl, than myself, explain why this would be bad or impossible?

        Thanks.


        ___________
        Eric Hodges
      Thanks Eric. That's what I meant to ask actually! I have experience with mod_perl, but am having problems getting it to work at the moment (on AIX) and have a bad feeling I might have to rebuild the perl!
Re: modules slowing webpage
by Anonymous Monk on May 10, 2004 at 19:18 UTC

    see if you can get fastcgi to work with your server. there's a mod_fastcgi for apache and a FCGI/CGI::Fast module for perl.

    i like it better than mod_perl because your scripts don't share a global space with every other script on the server.