A tiny library to efficiently search strings for ASCII characters.
rust
use jetscii::AsciiChars;
let mut search = AsciiChars::new();
search.push(b'-');
search.push(b':');
let part_number = "86-J52:rev1";
let parts: Vec<_> = part_number.split(search).collect();
assert_eq!(&parts, &["86", "J52", "rev1"]);
We use a particular x86-64 SSE 4.2 instruction (PCMPESTRI
) to gain
great speedups. This method stays fast even when searching for one
character in a set of up to 8 choices.
When PCMPESTRI
is not available, we fall back to a
universally-supported byte iterator method.
Searching a 5MiB string of a
s with a single space at the end:
| Method | Speed |
|--------------------------------------------------|-----------|
| str.find(AsciiChars)
| 6501 MB/s |
| str.as_bytes().iter().position(|&v| v == b' ')
| 1620 MB/s |
| str.find(|c| c == ' ')
| 1090 MB/s |
| str.find(' ')
| 1085 MB/s |
| str.find(&[' '][..])
| 602 MB/s |
| str.find(" ")
| 293 MB/s |
Searching a 5MiB string of a
s with a single ampersand at the end:
| Method | Speed |
|--------------------------------------------------|-----------|
| str.find(AsciiChars)
| 6480 MB/s |
| str.as_bytes().iter().position(|&v| ...)
| 1620 MB/s |
| str.find(|c| ...)
| 1022 MB/s |
| str.find(&['<', '>', '&'][..])
| 361 MB/s |
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)