replace bytes::find with memchr::memmem::find

This is a much more efficient implementation with ~20% improved
throughput and latency with the simple signing benchmark.

signing/sign            time:   [1.3024 ms 1.3050 ms 1.3076 ms]
                        thrpt:  [2.2886 MiB/s 2.2933 MiB/s 2.2978 MiB/s]
                 change:
                        time:   [-19.016% -18.855% -18.691%] (p = 0.00 < 0.05)
                        thrpt:  [+22.987% +23.237% +23.482%]
                        Performance has improved.
This commit is contained in:
Wez Furlong
2023-06-14 10:02:13 -07:00
parent fb16925f99
commit 0fa0de10ce
3 changed files with 3 additions and 7 deletions
Generated
+1
View File
@@ -131,6 +131,7 @@ dependencies = [
"futures",
"indexmap",
"mailparse",
"memchr",
"nom",
"quick-error 2.0.1",
"regex",
+1
View File
@@ -14,6 +14,7 @@ license = "MIT"
[dependencies]
ed25519-dalek = "1.0.1"
mailparse = "0.14"
memchr = "2.5"
quick-error = "2.0.1"
nom = "7.1.0"
chrono = { version = "0.4.19", default-features = false, features = ["clock", "std"] }
+1 -7
View File
@@ -1,4 +1,5 @@
///! Various utility functions to operate on bytes
pub(crate) use memchr::memmem::find;
pub(crate) fn get_all_after<'a>(bytes: &'a [u8], end: &[u8]) -> &'a [u8] {
if let Some(mut end_index) = find(bytes, end) {
@@ -9,13 +10,6 @@ pub(crate) fn get_all_after<'a>(bytes: &'a [u8], end: &[u8]) -> &'a [u8] {
}
}
/// Find the offset of specific bytes in bytes
pub(crate) fn find(bytes: &[u8], search: &[u8]) -> Option<usize> {
bytes
.windows(search.len())
.position(|window| window == search)
}
pub(crate) fn replace(bytes: &mut [u8], from: char, to: char) {
for byte in bytes.iter_mut() {
if *byte == from as u8 {