Skip to content

Commit

Permalink
chore(query): redis's hyper loglog
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li committed Feb 3, 2024
1 parent c63480d commit 45f6c91
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 177 deletions.
238 changes: 119 additions & 119 deletions src/common/hashtable/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,20 @@ impl FastHash for u128 {
#[inline(always)]
fn fast_hash(&self) -> u64 {
cfg_if::cfg_if! {
if #[cfg(target_feature = "sse4.2")] {
use std::arch::x86_64::_mm_crc32_u64;
let value = unsafe { _mm_crc32_u64(u64::MAX, *self as u64) };
unsafe { _mm_crc32_u64(value, (*self >> 64) as u64) }
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;
if #[cfg(target_feature = "sse4.2")] {
use std::arch::x86_64::_mm_crc32_u64;
let value = unsafe { _mm_crc32_u64(u64::MAX, *self as u64) };
unsafe { _mm_crc32_u64(value, (*self >> 64) as u64) }
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;

let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
hasher.write_u128(*self);
hasher.finish()
}
}
let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
hasher.write_u128(*self);
hasher.finish()
}
}
}
}

Expand All @@ -240,53 +240,53 @@ impl FastHash for i256 {
#[inline(always)]
fn fast_hash(&self) -> u64 {
cfg_if::cfg_if! {
if #[cfg(target_feature = "sse4.2")] {
use std::arch::x86_64::_mm_crc32_u64;
let mut value = u64::MAX;
for x in self.0 {
value = unsafe { _mm_crc32_u64(value, x as u64) };
value = unsafe { _mm_crc32_u64(value, (x >> 64) as u64) };
}
value
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;
if #[cfg(target_feature = "sse4.2")] {
use std::arch::x86_64::_mm_crc32_u64;
let mut value = u64::MAX;
for x in self.0 {
value = unsafe { _mm_crc32_u64(value, x as u64) };
value = unsafe { _mm_crc32_u64(value, (x >> 64) as u64) };
}
value
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;

let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
for x in self.0 {
hasher.write_i128(x);
}
hasher.finish()
}
let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
for x in self.0 {
hasher.write_i128(x);
}
hasher.finish()
}
}
}
}

impl FastHash for U256 {
#[inline(always)]
fn fast_hash(&self) -> u64 {
cfg_if::cfg_if! {
if #[cfg(target_feature = "sse4.2")] {
use std::arch::x86_64::_mm_crc32_u64;
let mut value = u64::MAX;
for x in self.0 {
value = unsafe { _mm_crc32_u64(value, x as u64) };
value = unsafe { _mm_crc32_u64(value, (x >> 64) as u64) };
}
value
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;
if #[cfg(target_feature = "sse4.2")] {
use std::arch::x86_64::_mm_crc32_u64;
let mut value = u64::MAX;
for x in self.0 {
value = unsafe { _mm_crc32_u64(value, x as u64) };
value = unsafe { _mm_crc32_u64(value, (x >> 64) as u64) };
}
value
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;

let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
for x in self.0 {
hasher.write_u128(x);
}
hasher.finish()
}
let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
for x in self.0 {
hasher.write_u128(x);
}
hasher.finish()
}
}
}
}

Expand Down Expand Up @@ -327,34 +327,34 @@ impl FastHash for [u8] {
#[inline(always)]
fn fast_hash(&self) -> u64 {
cfg_if::cfg_if! {
if #[cfg(target_feature = "sse4.2")] {
use crate::utils::read_le;
use std::arch::x86_64::_mm_crc32_u64;
let mut value = u64::MAX;
for i in (0..self.len()).step_by(8) {
if i + 8 < self.len() {
unsafe {
let x = (&self[i] as *const u8 as *const u64).read_unaligned();
value = _mm_crc32_u64(value, x);
}
} else {
unsafe {
let x = read_le(&self[i] as *const u8, self.len() - i);
value = _mm_crc32_u64(value, x);
}
}
if #[cfg(target_feature = "sse4.2")] {
use crate::utils::read_le;
use std::arch::x86_64::_mm_crc32_u64;
let mut value = u64::MAX;
for i in (0..self.len()).step_by(8) {
if i + 8 < self.len() {
unsafe {
let x = (&self[i] as *const u8 as *const u64).read_unaligned();
value = _mm_crc32_u64(value, x);
}
value
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;

let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
hasher.write(self);
hasher.finish()
unsafe {
let x = read_le(&self[i] as *const u8, self.len() - i);
value = _mm_crc32_u64(value, x);
}
}
}
value
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;

let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
hasher.write(self);
hasher.finish()
}
}
}
}

Expand All @@ -370,66 +370,66 @@ impl<const N: usize> FastHash for ([u64; N], NonZeroU64) {
#[inline(always)]
fn fast_hash(&self) -> u64 {
cfg_if::cfg_if! {
if #[cfg(target_feature = "sse4.2")] {
use std::arch::x86_64::_mm_crc32_u64;
let mut value = u64::MAX;
for x in self.0 {
value = unsafe { _mm_crc32_u64(value, x) };
}
value = unsafe { _mm_crc32_u64(value, self.1.get()) };
value
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;
if #[cfg(target_feature = "sse4.2")] {
use std::arch::x86_64::_mm_crc32_u64;
let mut value = u64::MAX;
for x in self.0 {
value = unsafe { _mm_crc32_u64(value, x) };
}
value = unsafe { _mm_crc32_u64(value, self.1.get()) };
value
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;

let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
for x in self.0 {
hasher.write_u64(x);
}
hasher.write_u64(self.1.get());
hasher.finish()
}
let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
for x in self.0 {
hasher.write_u64(x);
}
hasher.write_u64(self.1.get());
hasher.finish()
}
}
}
}

// For hash join string hash table.
#[inline(always)]
pub fn hash_join_fast_string_hash(key: &[u8]) -> u64 {
cfg_if::cfg_if! {
if #[cfg(target_feature = "sse4.2")] {
use crate::utils::read_le;
use std::arch::x86_64::_mm_crc32_u64;
if std::intrinsics::unlikely(key.is_empty()) {
u32::MAX as u64
} else {
let mut value = u64::MAX;
for i in (0..key.len()).step_by(8) {
if i + 8 < key.len() {
unsafe {
let x = (&key[i] as *const u8 as *const u64).read_unaligned();
value = _mm_crc32_u64(value, x);
}
} else {
unsafe {
let x = read_le(&key[i] as *const u8, key.len() - i);
value = _mm_crc32_u64(value, x);
}
if #[cfg(target_feature = "sse4.2")] {
use crate::utils::read_le;
use std::arch::x86_64::_mm_crc32_u64;
if std::intrinsics::unlikely(key.is_empty()) {
u32::MAX as u64
} else {
let mut value = u64::MAX;
for i in (0..key.len()).step_by(8) {
if i + 8 < key.len() {
unsafe {
let x = (&key[i] as *const u8 as *const u64).read_unaligned();
value = _mm_crc32_u64(value, x);
}
} else {
unsafe {
let x = read_le(&key[i] as *const u8, key.len() - i);
value = _mm_crc32_u64(value, x);
}
}
value
}
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;

let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
hasher.write(key);
hasher.finish()
value
}
} else {
use std::hash::Hasher;
use std::hash::BuildHasher;

let state = ahash::RandomState::with_seeds(SEEDS[0], SEEDS[1], SEEDS[2], SEEDS[3]);
let mut hasher = state.build_hasher();
hasher.write(key);
hasher.finish()
}
}
}

pub trait EntryRefLike: Copy {
Expand Down
Loading

0 comments on commit 45f6c91

Please sign in to comment.