Skip to content

Commit

Permalink
Make IO::Path.parent about 2.7x as fast
Browse files Browse the repository at this point in the history
By using the new `cloned-with-path' method and refactor the rest of
the code to ternaries *and* use

  !foo.first(* ne bar)

instead of

  !grep {$_ ne bar}, foo
  • Loading branch information
lizmat committed May 21, 2020
1 parent 7238b09 commit 71cb0c5
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/core.c/IO/Path.pm6
Expand Up @@ -341,24 +341,14 @@ my class IO::Path is Cool does IO {
my $curdir := $!SPEC.curdir;
my $updir := $!SPEC.updir;

if self.is-absolute {
nqp::create(self)!SET-SELF(
$!SPEC.join($.volume, $.dirname, ''), $!SPEC, $!CWD, False)
}
elsif $.dirname eq $curdir and $.basename eq $curdir {
nqp::create(self)!SET-SELF(
$!SPEC.join($.volume,$curdir,$updir), $!SPEC, $!CWD, False)
}
elsif $.dirname eq $curdir && $.basename eq $updir
or !grep({$_ ne $updir}, $!SPEC.splitdir($.dirname)) {
nqp::create(self)!SET-SELF( # If all updirs, then add one more
$!SPEC.join($.volume,$!SPEC.catdir($.dirname,$updir),$.basename),
$!SPEC, $!CWD, False)
}
else {
nqp::create(self)!SET-SELF(
$!SPEC.join($.volume, $.dirname, ''), $!SPEC, $!CWD, False)
}
nqp::clone(self).cloned-with-path: self.is-absolute
?? $!SPEC.join($.volume, $.dirname, '')
!! $.dirname eq $curdir && $.basename eq $curdir
?? $!SPEC.join($.volume,$curdir,$updir)
!! ($.dirname eq $curdir && $.basename eq $updir)
|| !$!SPEC.splitdir($.dirname).first(* ne $updir)
?? $!SPEC.join($.volume,$!SPEC.catdir($.dirname,$updir),$.basename)
!! $!SPEC.join($.volume, $.dirname, '')
}

method child (IO::Path:D: \child) {
Expand Down

0 comments on commit 71cb0c5

Please sign in to comment.