perlcc made easy or, how to make a cgi moose app
DESCRIPTION
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.TRANSCRIPT
perlccor Reducing the startup cost of perl programs.
In the beginning…http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/9512/msg00900.html
(What’s a modem?)http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/9512/msg00904.html
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/9601/msg00124.html
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
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
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.
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
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
What B::C is not
Doesn’t speed up your program.
(mostly)
Can’t statically compile in XS(mostly)
Can’t live independent of libperl
(mostly)
What B::C can do
Speeds page hit of traditional CGI apps.
( No running process when idle )
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.
Reducing your Moose startup time
(Eventually) https://code.google.com/p/perl-compiler/issues/detail?id=350
#damnitstevan
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
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 world\n”’; ./a.out
• perlcc -o foo foo.pl
• Optimizations (-O1/-O2/-O3)
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
Example
$> cpanm B::C
$> perlcc -o foo foo.pl
$> ./foo
Hello World!
Tomorrow
• B::C Working on latest perl
• Compiled perl modules to .so files
• Byte Loader?
• B::CC
More information?• irc.perl.org #compiler
• Issues: https://code.google.com/p/perl-compiler/issues/list
• Contribute: https://github.com/rurban/perl-compiler
• master (unstable)
• release (most recently released to CPAN)
• CPAN: https://metacpan.org/pod/B::C
• Me: Todd Rinaldo <[email protected]>