Based on recent work on the B::C module, it's now possible to build a working binary which can do away with the majority of startup expense a typical perl program would have.
In this talk, I'll explain:
- What the perlcc compiler does and doesn't do.
- How to setup the perl compiler.
- How to pre-compile a perl script into a binary for faster execution.
- Discuss the common mistakes one makes when building a perl binary and how to work around them.
5. Perl run states
• BEGIN
• Code is always executed as soon as it’s seen.
• UNITCHECK
• Code is run (LIFO) just after each file is compiled
• CHECK
• Code is run LIFO after ALL compilation
• B::C hooks here to do it’s magic
• INIT
• Runs FIFO at beginning of run time
• Does NOT run if module is loaded at runtime.
• END
• Run LIFO after exit()
http://perldoc.perl.org/perlmod.html
6.
7. B.pm
• Compiler backend.
• Allows a Perl program to delve into its own innards
• Used by B::C to generate state information at the end
of the CHECK block
8. O.pm
• Interface to the compiler
• perl -MO=WHATEVA program.pl
• puts program into -c mode
• creates a CHECK block which runs
B::WHATEVA::compile with some info from the
compile process.
9. B::C
• Reduces your compile time to near zero by freezing
state at end of CHECK block
• perl -MO=-qq,C,-O3,-omyprog.c myprog.pl
• captures perl running state and writes out myprog.c
• compiled binary will start at INIT state
10. File handle gotchas
• opening file handles during compile and expecting
them to functon at run time
• BEGIN { open($fh, ‘<‘, ‘/var/log/foo’) or die }
• sub later { while(<$fh>) {…} }
• This also goes for things like numeric handles to
external libaries ( i.e. openssl handles (IO::Socket::SSL)
• https://rt.cpan.org/Public/Bug/Display.html?id=95452
16. Speeds page hit of
traditional CGI apps.
( No running process when idle )
17. Shrink your program memory
size.
• Package removal during C code generation
• COW strings stored as C strings
• duplicates consolidated to 1 string.
• Strings, Arrays, Hashes optimized to perfect size.
18. Reducing your Moose
startup time
(Eventually)
https://code.google.com/p/perl-compiler/issues/detail?id=350
#damnitstevan
19. B::C Time Line
• 1995 - Announcement
• 1996 - Compiler development started (B, O, B::C)
• 1998 - a8581515f - Integrated into core (5.005)
• November 2003 - Perl 5.6.2
• December 2007 - Removed in 5.10.0
• June 2014 - Mostly Stable - 5.14.4
20. Today
• cPanel initiated effort to get B::C working.
• cPanel Perl code can compile and run against 5.14.4
• perlcc script does all the magic now
• perlcc -e ‘print “Hello worldn”’; ./a.out
• perlcc -o foo foo.pl
• Optimizations (-O1/-O2/-O3)
21. In the Past
$> cpanm B::C
#Installs perlcc into your perl bin directory
$> perl -MO=-qq,C,-O3,-fno-fold,-ot/C-COMPILED/
CORE—io--print.c t/C-COMPILED/CORE—io—print.t
$> perl script/cc_harness -q t/CORE-CPANEL/io/CORE
—io—print.c -o t/CORE-CPANEL/io/CORE—io—print.bin
$> ./t/CORE-CPANEL/io/CORE—io—print.bin