perlcc
or 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 worldn”’; ./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 <toddr@cpan.org>

perlcc made easy or, how to make a CGI Moose app

  • 1.
    perlcc or Reducing thestartup cost of perl programs.
  • 2.
  • 3.
  • 4.
  • 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
  • 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 tothe 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 yourcompile 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
  • 11.
  • 12.
    Doesn’t speed upyour program. (mostly)
  • 13.
  • 14.
    Can’t live independentof libperl (mostly)
  • 15.
  • 16.
    Speeds page hitof traditional CGI apps. ( No running process when idle )
  • 17.
    Shrink your programmemory 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 startuptime (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 initiatedeffort 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
  • 22.
    Example $> cpanm B::C $>perlcc -o foo foo.pl $> ./foo Hello World!
  • 23.
    Tomorrow • B::C Workingon latest perl • Compiled perl modules to .so files • Byte Loader? • B::CC
  • 24.
    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 <toddr@cpan.org>