-
Notifications
You must be signed in to change notification settings - Fork 6
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
perf: Reduce TASO hashtable size #133
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
60bf9e3
perf: Reduce TASO hashtable size
lmondada d1862d8
contains -> insert
lmondada 5f0c35c
Do not pollute hash table with expensive cost
lmondada a6255ef
Update src/optimiser/taso/hugr_pchannel.rs
lmondada a3d31b0
Address comments
lmondada 7e3050f
Merge branch 'main' into fix/reduce-hash-memory
lmondada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
use std::collections::VecDeque; | ||
|
||
use fxhash::FxHashSet; | ||
|
||
/// A datastructure storing Hugr hashes. | ||
/// | ||
/// Stores hashes in buckets based on a cost function, to allow clearing | ||
/// the set of hashes that are no longer needed. | ||
pub(super) struct HugrHashSet { | ||
buckets: VecDeque<FxHashSet<u64>>, | ||
/// The cost at the front of the queue. | ||
min_cost: Option<usize>, | ||
} | ||
|
||
impl HugrHashSet { | ||
/// Create a new empty set. | ||
pub(super) fn new() -> Self { | ||
Self { | ||
buckets: VecDeque::new(), | ||
min_cost: None, | ||
} | ||
} | ||
|
||
/// Create a new set with a single hash and cost. | ||
pub(super) fn singleton(hash: u64, cost: usize) -> Self { | ||
let mut set = Self::new(); | ||
set.insert(hash, cost); | ||
set | ||
} | ||
|
||
/// Insert circuit with given hash and cost. | ||
/// | ||
/// Returns whether the insertion was successful, i.e. the negation | ||
/// of whether it was already present. | ||
pub(super) fn insert(&mut self, hash: u64, cost: usize) -> bool { | ||
let Some(min_cost) = self.min_cost.as_mut() else { | ||
self.min_cost = Some(cost); | ||
self.buckets.push_front([hash].into_iter().collect()); | ||
return true; | ||
}; | ||
self.buckets.reserve(min_cost.saturating_sub(cost)); | ||
while cost < *min_cost { | ||
self.buckets.push_front(FxHashSet::default()); | ||
*min_cost -= 1; | ||
} | ||
let bucket_index = cost - *min_cost; | ||
if bucket_index >= self.buckets.len() { | ||
self.buckets | ||
.resize_with(bucket_index + 1, FxHashSet::default); | ||
} | ||
self.buckets[bucket_index].insert(hash) | ||
} | ||
|
||
/// Returns whether the given hash is present in the set. | ||
#[allow(dead_code)] | ||
pub(super) fn contains(&self, hash: u64, cost: usize) -> bool { | ||
let Some(min_cost) = self.min_cost else { | ||
return false; | ||
}; | ||
let Some(index) = cost.checked_sub(min_cost) else { | ||
return false; | ||
}; | ||
let Some(b) = self.buckets.get(index) else { | ||
return false; | ||
}; | ||
b.contains(&hash) | ||
} | ||
|
||
fn max_cost(&self) -> Option<usize> { | ||
Some(self.min_cost? + self.buckets.len() - 1) | ||
} | ||
|
||
/// Remove all hashes with cost strictly greater than the given cost. | ||
pub(super) fn clear_over(&mut self, cost: usize) { | ||
while self.max_cost().is_some() && self.max_cost() > Some(cost) { | ||
self.buckets.pop_back(); | ||
if self.buckets.is_empty() { | ||
self.min_cost = None; | ||
} | ||
} | ||
} | ||
|
||
/// The number of hashes in the set | ||
pub(super) fn len(&self) -> usize { | ||
self.buckets.iter().map(|b| b.len()).sum() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::HugrHashSet; | ||
|
||
#[test] | ||
fn insert_elements() { | ||
// For simplicity, we use as cost: hash % 10 | ||
let mut set = HugrHashSet::new(); | ||
|
||
assert!(!set.contains(0, 0)); | ||
assert!(!set.contains(2, 0)); | ||
assert!(!set.contains(2, 3)); | ||
|
||
assert!(set.insert(20, 2)); | ||
assert!(!set.contains(0, 0)); | ||
assert!(!set.insert(20, 2)); | ||
assert!(set.insert(22, 2)); | ||
assert!(set.insert(23, 2)); | ||
|
||
assert!(set.contains(22, 2)); | ||
|
||
assert!(set.insert(33, 3)); | ||
assert_eq!(set.min_cost, Some(2)); | ||
assert_eq!(set.max_cost(), Some(3)); | ||
assert_eq!( | ||
set.buckets, | ||
[ | ||
[20, 22, 23].into_iter().collect(), | ||
[33].into_iter().collect() | ||
] | ||
); | ||
|
||
assert!(set.insert(3, 0)); | ||
assert!(set.insert(1, 0)); | ||
assert!(!set.insert(22, 2)); | ||
assert!(set.contains(33, 3)); | ||
assert!(set.contains(3, 0)); | ||
assert!(!set.contains(3, 2)); | ||
assert_eq!(set.min_cost, Some(0)); | ||
assert_eq!(set.max_cost(), Some(3)); | ||
|
||
assert_eq!(set.min_cost, Some(0)); | ||
assert_eq!( | ||
set.buckets, | ||
[ | ||
[1, 3].into_iter().collect(), | ||
[].into_iter().collect(), | ||
[20, 22, 23].into_iter().collect(), | ||
[33].into_iter().collect(), | ||
] | ||
); | ||
} | ||
|
||
#[test] | ||
fn remove_empty() { | ||
let mut set = HugrHashSet::new(); | ||
assert!(set.insert(20, 2)); | ||
assert!(set.insert(30, 3)); | ||
|
||
assert_eq!(set.len(), 2); | ||
set.clear_over(2); | ||
assert_eq!(set.len(), 1); | ||
set.clear_over(0); | ||
assert_eq!(set.len(), 0); | ||
assert!(set.min_cost.is_none()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.