Skip to content

Commit

Permalink
feat(cell): split visited cells by high bits
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Nov 18, 2024
1 parent 799ca81 commit e9bcb92
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/cell/usage_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,36 @@ mod sync {

type VisitedCells = scc::HashSet<HashBytes, ahash::RandomState>;

const CELL_SHARDS: usize = 256;

pub struct UsageTreeState {
pub mode: UsageTreeMode,
pub visited: VisitedCells,
pub visited: [VisitedCells; CELL_SHARDS],
}

impl UsageTreeState {
pub fn new(mode: UsageTreeMode) -> SharedState {
Arc::new(Self {
mode,
visited: Default::default(),
visited: [(); CELL_SHARDS].map(|_| Default::default()),
})
}

pub fn with_mode_and_capacity(mode: UsageTreeMode, capacity: usize) -> SharedState {
const CAPACITY_K: usize = CELL_SHARDS >> 2;

Arc::new(Self {
mode,
visited: VisitedCells::with_capacity_and_hasher(capacity, Default::default()),
visited: [(); CELL_SHARDS].map(|_| {
VisitedCells::with_capacity_and_hasher(
if CAPACITY_K > 0 {
capacity / CAPACITY_K
} else {
1
},
Default::default(),
)
}),
})
}

Expand All @@ -343,22 +356,28 @@ mod sync {

#[inline]
pub fn insert(&self, cell: &Cell) {
_ = self.visited.insert(*cell.repr_hash());
let key = cell.repr_hash();
_ = self.shard(key).insert(*key);
}

#[inline]
pub fn contains(&self, repr_hash: &HashBytes) -> bool {
self.visited.contains(repr_hash)
self.shard(repr_hash).contains(repr_hash)
}

#[inline]
pub fn is_empty(&self) -> bool {
self.visited.is_empty()
self.visited.iter().all(VisitedCells::is_empty)
}

#[inline]
pub fn len(&self) -> usize {
self.visited.len()
self.visited.iter().map(VisitedCells::len).sum()
}

#[inline(always)]
fn shard(&self, key: &HashBytes) -> &VisitedCells {
&self.visited[key[0] as usize]
}
}

Expand Down

0 comments on commit e9bcb92

Please sign in to comment.