Skip to content

Commit

Permalink
Make .index quite a bit faster
Browse files Browse the repository at this point in the history
Especially the Cool variants.  Mostly by making them share the same
proto and getting rid of the |c signature.
  • Loading branch information
lizmat committed Sep 28, 2019
1 parent 696eea2 commit 1d4c21d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
25 changes: 23 additions & 2 deletions src/core.c/Cool.pm6
Expand Up @@ -211,9 +211,30 @@ my class Cool { # declared in BOOTSTRAP
self.Str.indices(|c)
}

method index(Cool:D: |c) {
self.Str.index(|c)
proto method index(|) {*}
multi method index(Cool:D: Cool:D $needle --> Int:D) {
nqp::if(
nqp::islt_i((my int $i = nqp::index(self.Str,$needle.Str)),0),
Nil,
nqp::p6box_i($i)
)
}
multi method index(Cool:D: Str:D $needle --> Int:D) {
nqp::if(
nqp::islt_i((my int $i = nqp::index(self.Str,$needle)),0),
Nil,
nqp::p6box_i($i)
)
}
multi method index(Cool:D: Cool:D $needle, Cool:D $pos --> Int:D) {
self.Str.index: $needle.Str, $pos.Int
}
multi method index(Cool:D: Cool:D $needle, Int:D $pos --> Int:D) {
self.Str.index: $needle.Str, $pos
}
multi method index(Cool:D: Str:D $needle, Int:D $pos --> Int:D) {
self.Str.index: $needle, $pos
}

method rindex(Cool:D: |c) {
self.Str.rindex(|c)
Expand Down
34 changes: 21 additions & 13 deletions src/core.c/Str.pm6
Expand Up @@ -255,42 +255,50 @@ my class Str does Stringy { # declared in BOOTSTRAP
)
}

# TODO Use coercer in 1 candidate when RT131014
proto method index(|) {*}
multi method index(Str:D: Cool:D $needle --> Int:D) {
self.index: $needle.Str
nqp::if(
nqp::islt_i((my int $i = nqp::index($!value,$needle.Str)),0),
Nil,
nqp::p6box_i($i)
)
}
multi method index(Str:D: Str:D $needle --> Int:D) {
nqp::if(
nqp::islt_i((my int $i =
nqp::index($!value,nqp::getattr($needle,Str,'$!value'))),
0
),
nqp::islt_i((my int $i = nqp::index($!value,$needle)),0),
Nil,
nqp::p6box_i($i)
)
}
multi method index(Str:D: Cool:D $needle, Cool:D $pos --> Int:D) {
self.index: $needle.Str, $pos.Int
}
multi method index(Str:D: Cool:D $needle, Int:D $pos --> Int:D) {
nqp::if(
nqp::isbig_I(nqp::decont($pos)) || nqp::islt_i($pos,0),
INDEX-OOR(self,$pos),
nqp::if(
nqp::islt_i((my int $i = nqp::index($!value,$needle.Str,$pos)),0),
Nil,
nqp::p6box_i($i)
)
)
}
multi method index(Str:D: Str:D $needle, Int:D $pos --> Int:D) {
nqp::if(
nqp::isbig_I(nqp::decont($pos)) || nqp::islt_i($pos,0),
self!INDEX-OOR($pos),
INDEX-OOR(self,$pos),
nqp::if(
nqp::islt_i((my int $i = nqp::index(
$!value,nqp::getattr($needle,Str,'$!value'),$pos
)),0),
nqp::islt_i((my int $i = nqp::index($!value,$needle,$pos)),0),
Nil,
nqp::p6box_i($i)
)
)
}
method !INDEX-OOR($pos) {
sub INDEX-OOR($string, $pos) {
Failure.new(X::OutOfRange.new(
:what("Position in index"),
:got($pos),
:range("0..{self.chars}")
:range("0..$string.chars()")
))
}

Expand Down

0 comments on commit 1d4c21d

Please sign in to comment.