Skip to content

Commit 22af5b5

Browse files
zakcutnermarmeladema
authored andcommitted
Use XOR hash for scalar implementation
1 parent bec8864 commit 22af5b5

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/avx2/mod.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ mod rust;
44
pub use self::{original::*, rust::*};
55

66
use crate::{bits, memchr::MemchrSearcher, memcmp};
7-
use std::{
8-
arch::x86_64::*,
9-
mem,
10-
ops::{AddAssign, SubAssign},
11-
};
7+
use std::{arch::x86_64::*, mem};
128

139
#[derive(Clone, Copy, Default, PartialEq)]
1410
struct ScalarHash(usize);
@@ -17,23 +13,21 @@ impl From<&[u8]> for ScalarHash {
1713
#[inline(always)]
1814
fn from(bytes: &[u8]) -> Self {
1915
bytes.iter().fold(Default::default(), |mut hash, &b| {
20-
hash += b;
16+
hash.push(b);
2117
hash
2218
})
2319
}
2420
}
2521

26-
impl AddAssign<u8> for ScalarHash {
22+
impl ScalarHash {
2723
#[inline(always)]
28-
fn add_assign(&mut self, b: u8) {
29-
self.0 += usize::from(b);
24+
fn push(&mut self, b: u8) {
25+
self.0 ^= usize::from(b);
3026
}
31-
}
3227

33-
impl SubAssign<u8> for ScalarHash {
3428
#[inline(always)]
35-
fn sub_assign(&mut self, b: u8) {
36-
self.0 -= usize::from(b);
29+
fn pop(&mut self, b: u8) {
30+
self.0 ^= usize::from(b);
3731
}
3832
}
3933

@@ -167,15 +161,15 @@ macro_rules! avx2_searcher {
167161
let mut hash = ScalarHash::from(&haystack[..end]);
168162

169163
while end < haystack.len() {
170-
hash += *unsafe { haystack.get_unchecked(end) };
164+
hash.push(*unsafe { haystack.get_unchecked(end) });
171165
end += 1;
172166

173167
let start = end - self.size();
174168
if hash == self.scalar_hash && haystack[start..end] == *self.needle {
175169
return true;
176170
}
177171

178-
hash -= *unsafe { haystack.get_unchecked(start) };
172+
hash.pop(*unsafe { haystack.get_unchecked(start) });
179173
}
180174

181175
false

0 commit comments

Comments
 (0)