Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Use of Autoloader in Modern Times

by bulk88 (Priest)
on Nov 15, 2012 at 22:28 UTC ( #1004071=note: print w/replies, xml ) Need Help??

in reply to Use of Autoloader in Modern Times

A require is a require. Whether you do it at "startup" or "runtime" is irrelevant over the lifetime of the process. This is not C or Java, your whole program is not recompiled from a single require. There is memory savings to be had from not keeping so many subs and globs in memory.

Replies are listed 'Best First'.
Re^2: Use of Autoloader in Modern Times
by chromatic (Archbishop) on Nov 16, 2012 at 00:31 UTC
    Whether you do it at "startup" or "runtime" is irrelevant over the lifetime of the process.

    ... until you run a preforking server and want to take as much advantage of COW as you can.

      Which is one of the reasons why "load dependency at compile-time" vs "load dependency at run-time" should be decided by the environment (when possible). Modules that define new syntax must be loaded at compile-time. A huge fraction of modules don't need to be loaded at compile-time so it can be very handy to allow the invoker to pick which behavior.

      Almost all unit tests should delay loading modules since the process isn't going to live very long and isn't going to use the vast majority of functionality eventually reached by the dependency tree (so no reason to wait for all of the too-early initialization over and over). But you should have at least one unit test that specifies "load all dependencies at compile-time" so that you can very quickly tell if any dependencies are missing, even if you haven't figured out how to exercise every single one of them.

      Pre-forking servers should load dependencies at compile-time in the parent but at run-time in the children.

      Lots of command-line tools (especially in a corporate software development house) can benefit from loading dependencies at run-time, so that I can do something useful with the frobnitz repository management subsystem from my laptop without having to install all of the PDF, XML, e-mail, GD, and plotting modules despite parts of the frobnitz repository needing to know how to send e-mails with PDF and/or XML attachements, some of which contain bar charts as png files (while also not having to constantly groom the dependency tree to make selected dependencies optional or at run-time to get that benefit).

      - tye        

      ...until you run a preforking server and want to take as much advantage of COW as you can.

      Could you expand on that a bit? How does copy on write and preforking incline one to use autoloading?

      My own personal preference is to avoid it like a plaguish thing, but even my most massive module isn't on the same order as some CPAN monsters.

      (Update: fix the italics tags)

        If you have a preforking server and a modern operating system, you'll tend to save memory by loading everything before forking off children, because they'll share memory pages until something modifies them. That's the opposite of autoloading.

Re^2: Use of Autoloader in Modern Times
by Tommy (Chaplain) on Nov 15, 2012 at 23:08 UTC

    ...Right, but it's an extra I/O op (or more) just to pull in the sub out of the .al file, each on an individual basis...

    Are the savings worth the cost?

    $ perl -MMIME::Base64 -e 'print decode_base64 "YWNlQHRvbW15YnV0bGVyLm1lCg=="'

      Presumably that will depend on the individual script's use case.

      If only 1 of every 1000 runs will perform the conditional require, then it's probably negligible. If 999 of every 1000 runs needs it, then it is probably not negligible.

      If in doubt, benchmark!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004071]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2018-06-23 00:53 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.