Skip to content

Commit

Permalink
Make IO::Path.parent(N) about 2x as fast
Browse files Browse the repository at this point in the history
- don't use UInt:D in the signature, it slows down dispatch
- test for values < 0 specifically, and generate a specific error if wrong
- use low-level loop instead of xx with .=
  • Loading branch information
lizmat committed May 22, 2020
1 parent 718d305 commit f93ce87
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/core.c/IO/Path.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,26 @@ my class IO::Path is Cool does IO {
nqp::create(self)!SET-SELF(
$resolved, $!SPEC, $volume ~ $sep, True);
}

proto method parent(|) {*}
multi method parent(IO::Path:D: UInt:D $depth) {
my $io = self;
$io .= parent xx $depth;
$io;
multi method parent(IO::Path:D: Int:D $depth is copy) {
if $depth > 0 {
my $io := self;
nqp::while(
$depth--,
$io := $io.parent
);
$io
}
else {
$depth
?? X::OutOfRange.new(
what => 'Depth of .parent',
got => $depth,
range => "0..*"
).throw
!! self
}
}
multi method parent(IO::Path:D:) { # XXX needs work
my $curdir := $!SPEC.curdir;
Expand Down

0 comments on commit f93ce87

Please sign in to comment.