From 0fa0de10ced93fbebf7c8309948a2a85f9023ae1 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 14 Jun 2023 09:56:34 -0700 Subject: [PATCH] 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. --- Cargo.lock | 1 + Cargo.toml | 1 + src/bytes.rs | 8 +------- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7227bb12..6c757e08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,6 +131,7 @@ dependencies = [ "futures", "indexmap", "mailparse", + "memchr", "nom", "quick-error 2.0.1", "regex", diff --git a/Cargo.toml b/Cargo.toml index c3d7fb86..83d913a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/bytes.rs b/src/bytes.rs index ec82e3a7..b00a65a5 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -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 { - 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 {