Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Have Repository::FileSystem automatically precompile files
We now precompile loaded modules automatically. However, we cannot do this
anymore as soon as we encounter a 'use lib' as this changes the precompilation
store used. Consider:
    use Test;
    use lib 'foo';
    use Test::Util; # which again uses Test
Test::Util would be loaded from a different precompilation store that contains
a different precompiled version of Test to the one already loaded.
  • Loading branch information
niner committed Nov 14, 2015
1 parent 2b19685 commit 3eea814
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
46 changes: 43 additions & 3 deletions src/core/CompUnit/PrecompilationRepository.pm
@@ -1,11 +1,21 @@
role CompUnit::PrecompilationRepository {
method load(CompUnit::PrecompilationId $id) returns CompUnit {
CompUnit
{
my $i;
role CompUnit::PrecompilationRepository {
has $!i = $i++;

method load(CompUnit::PrecompilationId $id) returns CompUnit {
CompUnit
}

method may-precomp() {
$i < 3 # number of next repo after None and the first Default
}
}
}

BEGIN CompUnit::PrecompilationRepository::<None> := CompUnit::PrecompilationRepository.new;

class CompUnit { ... }
class CompUnit::PrecompilationRepository::Default does CompUnit::PrecompilationRepository {
has CompUnit::PrecompilationStore $.store;

Expand All @@ -27,6 +37,36 @@ class CompUnit::PrecompilationRepository::Default does CompUnit::PrecompilationR
CompUnit::Handle
}
}

method precompile(CompUnit:D $compunit, CompUnit::PrecompilationId $id) {
my $io = self.store.destination($*PERL.compiler.id, $id);
my $path = $compunit.path;
die "Cannot pre-compile over a newer existing file: $io"
if $io.e && $io.modified > $path.modified;

my Mu $opts := nqp::atkey(%*COMPILING, '%?OPTIONS');
my $lle = !nqp::isnull($opts) && !nqp::isnull(nqp::atkey($opts, 'll-exception'))
?? ' --ll-exception'
!! '';
%*ENV<RAKUDO_PRECOMP_WITH> = CREATE-INCLUDE-SPECS(@*INC);

RAKUDO_MODULE_DEBUG("Precomping with %*ENV<RAKUDO_PRECOMP_WITH>")
if $*RAKUDO_MODULE_DEBUG;

my $cmd = "$*EXECUTABLE$lle --target={$*VM.precomp-target} --output=$io $path";
my $proc = shell("$cmd 2>&1", :out, :!chomp);
%*ENV<RAKUDO_PRECOMP_WITH>:delete;

my $result = '';
$result ~= $_ for $proc.out.lines;
$proc.out.close;
if $proc.status -> $status { # something wrong
$result ~= "Return status $status\n";
fail $result if $result;
}
note $result if $result;
True
}
}

# vim: ft=perl6 expandtab sw=4
13 changes: 10 additions & 3 deletions src/core/CompUnit/PrecompilationStore/File.pm
Expand Up @@ -22,13 +22,20 @@ class CompUnit::PrecompilationStore::File does CompUnit::PrecompilationStore {
$path ~~ :e ?? $path.Str !! Str
}

method destination(CompUnit::PrecompilationId $compiler-id,
CompUnit::PrecompilationId $precomp-id)
returns IO::Path
{
my $dest = self!dir($compiler-id, $precomp-id);
$dest.mkdir;
$dest.child($precomp-id.IO)
}

method store(CompUnit::PrecompilationId $compiler-id,
CompUnit::PrecompilationId $precomp-id,
Str:D $path)
{
my $dest = self!dir($compiler-id, $precomp-id);
$dest.mkdir;
$path.IO.copy($dest.child($precomp-id.IO));
$path.IO.copy(self.destination($compiler-id, $precomp-id));
}

method delete(CompUnit::PrecompilationId $compiler-id, CompUnit::PrecompilationId $precomp-id)
Expand Down
24 changes: 18 additions & 6 deletions src/core/CompUnit/Repository/FileSystem.pm
@@ -1,5 +1,6 @@
class CompUnit::Repository::FileSystem does CompUnit::Repository::Locally does CompUnit::Repository {
has %!loaded;
has $!precomp;

my %extensions =
Perl6 => <pm6 pm>,
Expand All @@ -25,9 +26,11 @@ class CompUnit::Repository::FileSystem does CompUnit::Repository::Locally does C
my $base := $!prefix.abspath ~ $dir-sep ~ $name.subst(:g, "::", $dir-sep) ~ '.';
my $compunit;

return %!loaded{$name} if %!loaded{$name}:exists;

if $handle {
return %!loaded{$name} = %seen{$base} = CompUnit.new(
$base, :name($name), :extension(''), :has-precomp, :$handle, :repo(self)
$base, :name($name), :extension(''), :has-precomp, :$handle, :repo(self)
);
}
else {
Expand All @@ -40,7 +43,7 @@ class CompUnit::Repository::FileSystem does CompUnit::Repository::Locally does C
!! $!prefix.abspath ~ $dir-sep ~ $file;

$compunit = %seen{$path} = CompUnit.new(
$path, :name($name), :extension(''), :has_source, :repo(self)
$path, :name($name), :extension(''), :has-source, :repo(self)
) if IO::Path.new-from-absolute-path($path).f;
}
}
Expand All @@ -64,8 +67,16 @@ class CompUnit::Repository::FileSystem does CompUnit::Repository::Locally does C
}

if $compunit {
$compunit.load(:$line);
return %!loaded{$compunit.name} = $compunit;
if $precomp.may-precomp and $precomp.precompile($compunit, $id) {
$handle = $precomp.load($id);
return %!loaded{$name} = %seen{$base} = CompUnit.new(
$base, :name($name), :extension(''), :has-precomp, :$handle, :repo(self)
);
}
else {
$compunit.load(:$line);
return %!loaded{$compunit.name} = $compunit;
}
}

return self.next-repo.need($spec, $precomp, :$line) if self.next-repo;
Expand Down Expand Up @@ -108,13 +119,14 @@ class CompUnit::Repository::FileSystem does CompUnit::Repository::Locally does C
}

method precomp-repository() returns CompUnit::PrecompilationRepository {
CompUnit::PrecompilationRepository::Default.new(
$!precomp := CompUnit::PrecompilationRepository::Default.new(
:store(
CompUnit::PrecompilationStore::File.new(
:prefix(self.prefix.child('.precomp')),
)
),
);
) unless $!precomp;
$!precomp
}
}

Expand Down

0 comments on commit 3eea814

Please sign in to comment.