From 3c1c91e6a101ca0257b7e1fcb72641813c192f5b Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Mon, 9 Oct 2023 22:32:38 -0400 Subject: [PATCH] Correct memchr implementations --- benches/benchmarks.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/benches/benchmarks.rs b/benches/benchmarks.rs index 00e076c..2083831 100644 --- a/benches/benchmarks.rs +++ b/benches/benchmarks.rs @@ -100,8 +100,14 @@ fn xml5(c: &mut Criterion) { }); group.bench_function("memchr", |b| { b.iter(|| { - memchr::memchr3(b'<', b'>', b'&', haystack.as_bytes()) - .or_else(|| memchr::memchr2(b'\'', b'"', haystack.as_bytes())) + match ( + memchr::memchr3(b'<', b'>', b'&', haystack.as_bytes()), + memchr::memchr2(b'\'', b'"', haystack.as_bytes()), + ) { + (None, rhs) => rhs, + (lhs, None) => lhs, + (Some(lhs), Some(rhs)) => Some(lhs.min(rhs)), + } }); }); } @@ -172,12 +178,15 @@ fn big_16(c: &mut Criterion) { }); group.bench_function("memchr", |b| { b.iter(|| { - memchr::memchr3(b'A', b'B', b'C', haystack.as_bytes()) - .or_else(|| memchr::memchr3(b'D', b'E', b'F', haystack.as_bytes())) - .or_else(|| memchr::memchr3(b'G', b'H', b'I', haystack.as_bytes())) - .or_else(|| memchr::memchr3(b'J', b'K', b'L', haystack.as_bytes())) - .or_else(|| memchr::memchr3(b'M', b'N', b'O', haystack.as_bytes())) - .or_else(|| memchr::memchr(b'P', haystack.as_bytes())) + let indexes = [ + memchr::memchr3(b'A', b'B', b'C', haystack.as_bytes()), + memchr::memchr3(b'D', b'E', b'F', haystack.as_bytes()), + memchr::memchr3(b'G', b'H', b'I', haystack.as_bytes()), + memchr::memchr3(b'J', b'K', b'L', haystack.as_bytes()), + memchr::memchr3(b'M', b'N', b'O', haystack.as_bytes()), + memchr::memchr(b'P', haystack.as_bytes()), + ]; + indexes.iter().copied().min_by_key(|x| x.unwrap_or(usize::MAX)).unwrap() }) }); }