Skip to content

Commit bb4b681

Browse files
authored
Use fxhash for hash maps (#15)
1 parent 1bd03df commit bb4b681

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ keywords = ["incremental", "memoization", "tracked", "constraints"]
1414

1515
[workspace.dependencies]
1616
comemo-macros = { version = "0.4.0", path = "macros" }
17+
fxhash = "0.2"
1718
once_cell = "1.18"
1819
parking_lot = "0.12"
1920
proc-macro2 = "1"
@@ -41,6 +42,7 @@ testing = []
4142

4243
[dependencies]
4344
comemo-macros = { workspace = true, optional = true }
45+
fxhash = { workspace = true }
4446
parking_lot = { workspace = true }
4547
siphasher = { workspace = true }
4648

src/accelerate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::collections::HashMap;
21
use std::sync::atomic::{AtomicUsize, Ordering};
32

3+
use fxhash::FxHashMap;
44
use parking_lot::{MappedRwLockReadGuard, Mutex, RwLock, RwLockReadGuard};
55

66
/// The global list of currently alive accelerators.
@@ -12,7 +12,7 @@ static ID: AtomicUsize = AtomicUsize::new(0);
1212
/// The type of each individual accelerator.
1313
///
1414
/// Maps from call hashes to return hashes.
15-
type Accelerator = Mutex<HashMap<u128, u128>>;
15+
type Accelerator = Mutex<FxHashMap<u128, u128>>;
1616

1717
/// Generate a new accelerator.
1818
pub fn id() -> usize {
@@ -58,6 +58,6 @@ pub fn get(id: usize) -> Option<MappedRwLockReadGuard<'static, Accelerator>> {
5858
fn resize(len: usize) {
5959
let mut pair = ACCELERATORS.write();
6060
if len > pair.1.len() {
61-
pair.1.resize_with(len, || Mutex::new(HashMap::new()));
61+
pair.1.resize_with(len, || Mutex::new(FxHashMap::default()));
6262
}
6363
}

src/cache.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::collections::HashMap;
21
use std::sync::LazyLock;
32
use std::sync::atomic::{AtomicUsize, Ordering};
43

4+
use fxhash::FxHashMap;
55
use parking_lot::RwLock;
66
use siphasher::sip128::{Hasher128, SipHasher13};
77

@@ -132,7 +132,7 @@ impl<C: 'static, Out: 'static> Cache<C, Out> {
132132
/// The internal data for a cache.
133133
pub struct CacheData<C, Out> {
134134
/// Maps from hashes to memoized results.
135-
entries: HashMap<u128, Vec<CacheEntry<C, Out>>>,
135+
entries: FxHashMap<u128, Vec<CacheEntry<C, Out>>>,
136136
}
137137

138138
impl<C, Out: 'static> CacheData<C, Out> {
@@ -174,7 +174,7 @@ impl<C, Out: 'static> CacheData<C, Out> {
174174

175175
impl<C, Out> Default for CacheData<C, Out> {
176176
fn default() -> Self {
177-
Self { entries: HashMap::new() }
177+
Self { entries: FxHashMap::default() }
178178
}
179179
}
180180

src/constraint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::borrow::Cow;
2-
use std::collections::HashMap;
32
use std::collections::hash_map::Entry;
43
use std::hash::Hash;
54

5+
use fxhash::FxHashMap;
66
use parking_lot::RwLock;
77

88
use crate::accelerate;
@@ -156,7 +156,7 @@ impl<T: Call> Default for MutableConstraint<T> {
156156

157157
/// A map of calls.
158158
#[derive(Clone)]
159-
struct EntryMap<T: Call>(HashMap<u128, ConstraintEntry<T>>);
159+
struct EntryMap<T: Call>(FxHashMap<u128, ConstraintEntry<T>>);
160160

161161
impl<T: Call> EntryMap<T> {
162162
/// Enter a constraint for a call to a function.
@@ -176,7 +176,7 @@ impl<T: Call> EntryMap<T> {
176176

177177
impl<T: Call> Default for EntryMap<T> {
178178
fn default() -> Self {
179-
Self(HashMap::new())
179+
Self(FxHashMap::default())
180180
}
181181
}
182182

0 commit comments

Comments
 (0)