perf(stacker): compare short keys without memcmp calls

double_check_trick compared [u8] sub-slices, which can be lowered to a
memcmp call (it was on aarch64: ~13% of SpecializedPostingsWriter::subscribe).
Comparing fixed-size arrays compiles to loads and compares.

Index Body only (OTel logs, positions): -5%.
This commit is contained in:
François Massot
2026-10-10 18:05:03 +02:00
parent 57085cc012
commit b0439faf24
+5 -1
View File
@@ -69,7 +69,11 @@ fn short_compare(left: &[u8], right: &[u8]) -> bool {
#[inline(always)]
fn double_check_trick<const SIZE: usize>(left: &[u8], right: &[u8]) -> bool {
left[0..SIZE] == right[0..SIZE] && left[left.len() - SIZE..] == right[right.len() - SIZE..]
// Fixed-size array comparisons compile to plain loads and compares; comparing `[u8]`
// sub-slices can be lowered to a `memcmp` call (seen on aarch64).
let head = |slice: &[u8]| -> [u8; SIZE] { slice[..SIZE].try_into().unwrap() };
let tail = |slice: &[u8]| -> [u8; SIZE] { slice[slice.len() - SIZE..].try_into().unwrap() };
head(left) == head(right) && tail(left) == tail(right)
}
#[cfg(test)]