Skip to content

Commit

Permalink
Make .contains 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 27, 2019
1 parent 44a6a0d commit 696eea2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
20 changes: 18 additions & 2 deletions src/core.c/Cool.pm6
Expand Up @@ -187,8 +187,24 @@ my class Cool { # declared in BOOTSTRAP
self.Str.substr-eq($needle, $pos)
}

method contains(Cool:D: |c) {
self.Str.contains(|c)
proto method contains(|) {*}
multi method contains(Cool:D: Cool:D $needle --> Bool:D) {
nqp::hllbool(nqp::isne_i(nqp::index(self.Str,$needle.Str,0),-1))
}
multi method contains(Cool:D: Str:D $needle --> Bool:D) {
nqp::hllbool(nqp::isne_i(nqp::index(self.Str,$needle,0),-1))
}
multi method contains(Cool:D: Cool:D $needle, Int:D $pos --> Bool:D) {
self.Str.contains($needle.Str, $pos)
}
multi method contains(Cool:D: Str:D $needle, Int:D $pos --> Bool:D) {
self.Str.contains($needle, $pos)
}
multi method contains(Cool:D: Cool:D $needle, Cool:D $pos --> Bool:D) {
self.Str.contains($needle.Str, $pos.Int)
}
multi method contains(Cool:D: Str:D $needle, Cool:D $pos --> Bool:D) {
self.Str.contains($needle, $pos.Int)
}

method indices(Cool:D: |c) {
Expand Down
26 changes: 16 additions & 10 deletions src/core.c/Str.pm6
Expand Up @@ -177,28 +177,34 @@ my class Str does Stringy { # declared in BOOTSTRAP
)
}

# TODO Use coercer in 1 candidate when RT131014
proto method contains(|) {*}
multi method contains(Str:D: Cool:D $needle --> Bool:D) {
self.contains: $needle.Str
nqp::hllbool(nqp::isne_i(nqp::index($!value,$needle.Str,0),-1))
}
multi method contains(Str:D: Str:D $needle --> Bool:D) {
nqp::hllbool(nqp::isne_i(
nqp::index($!value,nqp::getattr($needle,Str,'$!value'),0),-1
))
nqp::hllbool(nqp::isne_i(nqp::index($!value,$needle,0),-1))
}
multi method contains(Str:D: Cool:D $needle, Int(Cool:D) $pos --> Bool:D) {
self.contains: $needle.Str, $pos
multi method contains(Str:D: Cool:D $needle, Int:D $pos --> Bool:D) {
nqp::hllbool(
nqp::if(
(nqp::isge_i($pos,0) && nqp::islt_i($pos,nqp::chars($!value))),
nqp::isne_i(nqp::index($!value,$needle.Str,$pos),-1)
)
)
}
multi method contains(Str:D: Str:D $needle, Int:D $pos --> Bool:D) {
nqp::hllbool(
nqp::if(
(nqp::isge_i($pos,0) && nqp::islt_i($pos,nqp::chars($!value))),
nqp::isne_i(
nqp::index($!value,nqp::getattr($needle,Str,'$!value'),$pos),-1)
nqp::isne_i(nqp::index($!value,$needle,$pos),-1)
)
)
}
multi method contains(Str:D: Cool:D $needle, Cool:D $pos --> Bool:D) {
self.contains($needle.Str, $pos.Int)
}
multi method contains(Str:D: Str:D $needle, Cool:D $pos --> Bool:D) {
self.contains($needle, $pos.Int)
}

# TODO Use coercer in 1 candidate when RT131014
proto method indices(|) {*}
Expand Down

0 comments on commit 696eea2

Please sign in to comment.