Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added unsafe code to remove ranks.len() (spares 8 byte) #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn hashmod<T: Hash>(iter: u64, v: &T, n: usize) -> u64 {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Mphf<T> {
bitvecs: Box<[BitVector]>,
ranks: Box<[Box<[u64]>]>,
ranks: *const Box<[u64]>,
phantom: PhantomData<T>,
}

Expand All @@ -119,6 +119,14 @@ impl<T> HeapSizeOf for Mphf<T> {
}
}

impl<T> Drop for Mphf<T> {
fn drop(&mut self) {
unsafe {
Box::from_raw(std::slice::from_raw_parts_mut(self.ranks as *mut Box<[u64]>, self.bitvecs.len()));
}
}
}

const MAX_ITERS: u64 = 100;

impl<'a, T: 'a + Hash + Clone + Debug> Mphf<T> {
Expand Down Expand Up @@ -239,9 +247,10 @@ impl<'a, T: 'a + Hash + Clone + Debug> Mphf<T> {
}

let ranks = Self::compute_ranks(&bitvecs);
debug_assert!(ranks.len() == bitvecs.len());
let r = Mphf {
bitvecs: bitvecs.into_boxed_slice(),
ranks: ranks,
ranks: Box::into_raw(ranks) as *const Box<[u64]>,
phantom: PhantomData,
};

Expand Down Expand Up @@ -316,9 +325,10 @@ impl<T: Hash + Clone + Debug> Mphf<T> {
}

let ranks = Self::compute_ranks(&bitvecs);
debug_assert!(ranks.len() == bitvecs.len());
let r = Mphf {
bitvecs: bitvecs.into_boxed_slice(),
ranks: ranks,
ranks: Box::into_raw(ranks) as *const Box<[u64]>,
phantom: PhantomData,
};

Expand Down Expand Up @@ -366,7 +376,10 @@ impl<T: Hash + Clone + Debug> Mphf<T> {
fn get_rank(&self, hash: u64, i: usize) -> u64 {
let idx = hash as usize;
let bv = self.bitvecs.get(i).expect("that level doesn't exist");
let ranks = self.ranks.get(i).expect("that level doesn't exist");

assert!(i <= isize::max_value() as usize);
// Here you need no check while self.ranks.len() == self.bitvecs.len()
let ranks = &unsafe { &*self.ranks.add(i) };

// Last pre-computed rank
let mut rank = ranks[idx / 512];
Expand Down Expand Up @@ -494,9 +507,10 @@ impl<T: Hash + Clone + Debug + Sync + Send> Mphf<T> {
}

let ranks = Self::compute_ranks(&bitvecs);
debug_assert!(ranks.len() == bitvecs.len());
let r = Mphf {
bitvecs: bitvecs.into_boxed_slice(),
ranks: ranks,
ranks: Box::into_raw(ranks) as *const Box<[u64]>,
phantom: PhantomData,
};

Expand Down Expand Up @@ -728,9 +742,10 @@ impl<'a, T: 'a + Hash + Clone + Debug + Send + Sync> Mphf<T> {
}

let ranks = Self::compute_ranks(&bitvecs);
debug_assert!(ranks.len() == bitvecs.len());
let r = Mphf {
bitvecs: bitvecs.into_boxed_slice(),
ranks: ranks,
ranks: Box::into_raw(ranks) as *const Box<[u64]>,
phantom: PhantomData,
};

Expand Down