perlcc made easy or, how to make a cgi moose app

24
perlcc or Reducing the startup cost of perl programs.

Upload: todd-rinaldo

Post on 05-Dec-2014

579 views

Category:

Technology


1 download

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

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

perlccor Reducing the startup cost of perl programs.

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

In the beginning…http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/9512/msg00900.html

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

(What’s a modem?)http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/9512/msg00904.html

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

http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/9601/msg00124.html

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

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

Page 6: perlcc made easy or, how to make a CGI Moose app
Page 7: perlcc made easy or, how to make a CGI Moose app

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

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

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.

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

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

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

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

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

What B::C is not

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

Doesn’t speed up your program.

(mostly)

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

Can’t statically compile in XS(mostly)

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

Can’t live independent of libperl

(mostly)

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

What B::C can do

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

Speeds page hit of traditional CGI apps.

( No running process when idle )

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

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.

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

Reducing your Moose startup time

(Eventually) https://code.google.com/p/perl-compiler/issues/detail?id=350

#damnitstevan

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

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

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

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)

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

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

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

Example

$> cpanm B::C

$> perlcc -o foo foo.pl

$> ./foo

Hello World!

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

Tomorrow

• B::C Working on latest perl

• Compiled perl modules to .so files

• Byte Loader?

• B::CC

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

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]>