Skip to content

Pass through u128 hashes to the hashmap #18

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions src/accelerate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::sync::atomic::{AtomicUsize, Ordering};

use rustc_hash::FxHashMap;
use parking_lot::{MappedRwLockReadGuard, Mutex, RwLock, RwLockReadGuard};

use crate::passthroughhasher::PassthroughHashMap;

/// The global list of currently alive accelerators.
static ACCELERATORS: RwLock<(usize, Vec<Accelerator>)> = RwLock::new((0, Vec::new()));

Expand All @@ -12,7 +13,7 @@ static ID: AtomicUsize = AtomicUsize::new(0);
/// The type of each individual accelerator.
///
/// Maps from call hashes to return hashes.
type Accelerator = Mutex<FxHashMap<u128, u128>>;
type Accelerator = Mutex<PassthroughHashMap<u128, u128>>;

/// Generate a new accelerator.
pub fn id() -> usize {
Expand Down Expand Up @@ -58,6 +59,6 @@ pub fn get(id: usize) -> Option<MappedRwLockReadGuard<'static, Accelerator>> {
fn resize(len: usize) {
let mut pair = ACCELERATORS.write();
if len > pair.1.len() {
pair.1.resize_with(len, || Mutex::new(FxHashMap::default()));
pair.1.resize_with(len, || Mutex::new(PassthroughHashMap::default()));
}
}
6 changes: 3 additions & 3 deletions src/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::collections::hash_map::Entry;
use std::hash::Hash;

use parking_lot::Mutex;
use rustc_hash::FxHashMap;

use crate::Track;
use crate::passthroughhasher::PassthroughHashMap;
use crate::track::{Call, Sink};

/// Records calls performed on a trackable type.
Expand Down Expand Up @@ -82,7 +82,7 @@ pub struct CallSequence<C> {
/// The raw calls. In order, but deduplicated via the `map`.
vec: Vec<Option<(C, u128)>>,
/// A map from hashes of calls to the indices in the vector.
map: FxHashMap<u128, usize>,
map: PassthroughHashMap<u128, usize>,
/// A cursor for iteration in `Self::next`.
cursor: usize,
}
Expand All @@ -92,7 +92,7 @@ impl<C> CallSequence<C> {
pub fn new() -> Self {
Self {
vec: Vec::new(),
map: FxHashMap::default(),
map: PassthroughHashMap::default(),
cursor: 0,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ mod constraint;
mod hash;
mod input;
mod memoize;
mod passthroughhasher;
mod track;
mod tree;

Expand Down
40 changes: 40 additions & 0 deletions src/passthroughhasher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::collections::HashMap;
use std::hash::{BuildHasher, Hasher};

/// Hash Map that re-uses the u128 as the hash value
pub(crate) type PassthroughHashMap<Key, Value> =
HashMap<Key, Value, BuildPassthroughHasher>;

#[derive(Copy, Clone, Default)]
pub(crate) struct BuildPassthroughHasher;

#[derive(Default)]
pub(crate) struct PassthroughHasher {
value: u64,
}

impl Hasher for PassthroughHasher {
#[inline(always)]
fn finish(&self) -> u64 {
self.value
}

#[inline]
fn write(&mut self, _bytes: &[u8]) {
unimplemented!("Unsupported operation")
}

#[inline]
fn write_u128(&mut self, i: u128) {
// truncating conversion
self.value = i as u64;
}
}

impl BuildHasher for BuildPassthroughHasher {
type Hasher = PassthroughHasher;
#[inline]
fn build_hasher(&self) -> PassthroughHasher {
PassthroughHasher::default()
}
}
5 changes: 3 additions & 2 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_hash::FxHashMap;
use slab::Slab;

use crate::constraint::CallSequence;
use crate::passthroughhasher::PassthroughHashMap;

/// A tree data structure that associates a value with a key hash and a sequence
/// of (call, return hash) pairs.
Expand All @@ -18,7 +19,7 @@ pub struct CallTree<C, T> {
/// Leaf nodes, directly storing outputs.
leaves: Slab<LeafNode<T>>,
/// The initial node for the given key hash.
start: FxHashMap<u128, NodeId>,
start: PassthroughHashMap<u128, NodeId>,
/// Maps from parent nodes to child nodes. The key is a pair of an inner
/// node ID and a return hash for that call. The value is the node to
/// transition to.
Expand Down Expand Up @@ -50,8 +51,8 @@ impl<C, T> CallTree<C, T> {
Self {
inner: Slab::new(),
leaves: Slab::new(),
start: PassthroughHashMap::default(),
edges: FxHashMap::default(),
start: FxHashMap::default(),
}
}
}
Expand Down