I have a need to check if a sub-slice exists in a bigger slice.
This is what I currently have:
/// Determine whether a slice contains a sub-slice.
#[inline]
fn contains(source: &[Bytes], find: &[Bytes]) -> bool {
let length = find.len();
source.par_windows(length).any(|window| window == find)
}
Originally this was a check on substrings (&str
) and I could just: source.contains(find)
. I am now using slices of Bytes
because I've compressed these strings and this is how they are now represented.
The source.contains(find)
way of doing this using &str
was way more performant. And this has become a major bottleneck in my program.
Is there any guidance on how I might be able to speed this up ? I was thinking possibly checking from both ends, but I'm not certain how I would do that if it would be near as performant as when I was doing source.contains(find)
.