Skip to content

Instantly share code, notes, and snippets.

@tokuhirom
Created September 21, 2015 03:20
Show Gist options
  • Save tokuhirom/ec10ec21b08556d2a12e to your computer and use it in GitHub Desktop.
Save tokuhirom/ec10ec21b08556d2a12e to your computer and use it in GitHub Desktop.
pre-compile all perl6 modules
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.010000;
use autodie;
use File::Find;
use Getopt::Long;
use File::Temp;
use Capture::Tiny qw/capture_merged/;
my $perl6 = 'perl6-m';
GetOptions(
'perl6=s' => \$perl6
);
my $fh = File::Temp->new;
print {$fh} '@*INC ==> grep /site\/lib/ ==> map { .subst(/^file\#/, "").print }';
$fh->flush;
my $sitelib = qx!$perl6 @{[$fh->filename]}!;
$sitelib or die "cannot get site lib";
chdir $sitelib or die "chdir failed: $sitelib:$!";
my %after;
find({
wanted => sub {
return 1 unless -f $_;
return 1 unless /\.pm6?\z/;
precompile($_);
return 1;
},
no_chdir => 1,
}, '.');
while (%after) {
for my $file (keys %after) {
delete $after{$file} if precompile($file);
}
}
sub precompile {
my $file = shift @_;
my $comptarget='mbc';
my $compext='moarvm';
my @cmd = ($perl6, "--target=$comptarget", "--output=$file.${compext}", $file);
print "@cmd\n";
my $retval = 1;
my $out = capture_merged {
$retval = system(@cmd);
};
if ($out =~ /When pre-compiling a module, its dependencies must be pre-compiled first./) {
$after{$file}++;
return 0; # fail
} else {
print $out;
}
if ($retval==0) {
return 1; # succeeded
} else {
return 0;
}
}
@tokuhirom
Copy link
Author

before

tokuhirom:p6-HTTP-Server-Tiny/ (master✗) $ perl6-m -Ilib --stagestats bin/crustup eg/hello.psgi6
Stage start      :   0.000
Stage parse      :   9.830
Stage syntaxcheck:   0.000
Stage ast        :   0.000
Stage optimize   :   0.005
Stage mast       :   0.025
Stage mbc        :   0.001
Stage moar       :   0.000
[INFO] [1] run-threads: workers:1
http server is ready: http://127.0.0.1:5000/

after

tokuhirom:p6-HTTP-Server-Tiny/ (master✗) $ perl6-m -Ilib --stagestats bin/crustup eg/hello.psgi6
Stage start      :   0.000
Stage parse      :   6.299
Stage syntaxcheck:   0.000
Stage ast        :   0.000
Stage optimize   :   0.003
Stage mast       :   0.016
Stage mbc        :   0.000
Stage moar       :   0.000
[INFO] [1] run-threads: workers:1
http server is ready: http://127.0.0.1:5000/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment