Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^3: Perl script compressor

by davido (Cardinal)
on Dec 08, 2019 at 21:40 UTC ( #11109857=note: print w/replies, xml ) Need Help??

in reply to Re^2: Perl script compressor
in thread Perl script compressor

I suggest using strace while running your script. If your script uses strict, warnings,, or any other modules, they also get opened and read into memory. If your Perl is configured to use, that will be opened and read in. And if it's been read in once, with an OS like Linux there's a chance the files are hot and ready in a cache anyway. But strace will demonstrate to you that the top level script you load is not the largest component that gets read in from a file.

The bulk of startup time has little to do with just reading the program file in from (hopefully) an SSD. I created two Hello World scripts; one with 13368 lines, consuming 1.1 megabytes on disk, and one with seven lines, consuming 96 bytes on disk. They both start by printing "Hello world\n", and end by printing "Goodbye world\n", but in the first script there are 13361 80-column lines of comments between the two print statements. Perl must read the entire file before getting to the final Goodbye world. Here are the timings:

$ time ./ Hello world Goobye world real 0m0.022s user 0m0.014s sys 0m0.008s $ time ./ Hello world Goodbye world real 0m0.008s user 0m0.008s sys 0m0.001s

A tremendous increase from eight milliseconds to twenty two. We go from running 45 times per second to 125 *if* the bloated script is 1 megabyte in size, and if all that bloat (including the parts that you bring in from CPAN and core-Perl libs) can be reduced to 96 bytes. What if the source script is 64kb? Let's try that:

$ time ./ Hello world Goodbye world real 0m0.009s user 0m0.004s sys 0m0.005s

So now we're talking about 1 millisecond difference. Instead of 125 runs per second, we have 111 per second, for a much more typically-sized script.

If startup time is a problem you won't solve it by minifying your Perl script. It's better solved by converting over to a daemon process that stays resident, or if that's really impossible, scaling out horizontally.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11109857]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2020-06-07 03:20 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (42 votes). Check out past polls.