Skip to content

Commit

Permalink
Switch to faster hashing algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
terrarier2111 committed Jun 6, 2023
1 parent 8a212ea commit cf51cbe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ memoffset = "0.8.0"
thread-id = "4.0.0"
crossbeam-utils = "0.8.15"
smallvec = { version = "1.10.0", features = ["union", "const_generics"] }
rustc-hash = "1.1.0"

[dev-dependencies]
criterion = "0.4.0"
Expand Down
9 changes: 6 additions & 3 deletions src/thread_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use std::cell::Cell;
use crate::{BUCKETS, Entry, POINTER_WIDTH};
use once_cell::sync::Lazy;
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap};
use std::collections::BinaryHeap;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Mutex;
use std::mem;
use std::mem::transmute;
use std::ops::Deref;
use std::ptr::{NonNull, null};
use rustc_hash::{FxHasher, FxHashMap};

/// Thread ID manager which allocates thread IDs. It attempts to aggressively
/// reuse thread IDs where possible to avoid cases where a ThreadLocal grows
Expand Down Expand Up @@ -100,6 +101,7 @@ pub(crate) static SHARED_IDS: [PtrCell<AtomicUsize>; BUCKETS] = {
unsafe { transmute([null::<AtomicUsize>(); BUCKETS]) }
};

#[inline]
pub(crate) unsafe fn shared_id_ptr(id: usize) -> *const AtomicUsize {
let (bucket, _, index) = id_into_parts(id);
SHARED_IDS[bucket].get().offset(index as isize)
Expand Down Expand Up @@ -172,22 +174,23 @@ pub(crate) fn id_into_parts(id: usize) -> (usize, usize, usize) {
(bucket, bucket_size, index)
}

#[inline]
pub(crate) fn free_id(id: usize) {
THREAD_ID_MANAGER.lock().unwrap().free(id);
}

pub(crate) struct FreeList {
id: usize,
pub(crate) dropping: AtomicBool,
pub(crate) free_list: Mutex<HashMap<usize, EntryData>>,
pub(crate) free_list: Mutex<FxHashMap<usize, EntryData>>,
}

impl FreeList {
fn new(id: usize) -> Self {
Self {
id,
dropping: Default::default(),
free_list: Mutex::new(Default::default()),
free_list: Mutex::new(FxHashMap::default()),
}
}

Expand Down

0 comments on commit cf51cbe

Please sign in to comment.