Improve performance of Candidate in HashMap by simplifying equality check
This commit is contained in:
parent
422a198cf5
commit
974a56dffd
@ -775,6 +775,7 @@ pub fn init_repeat_count<N: Number>(election: &mut Election<N>) {
|
||||
let mut new_candidates = Vec::new();
|
||||
for candidate in &election.candidates {
|
||||
let mut new_candidate = candidate.clone();
|
||||
new_candidate.index += election.candidates.len(); // Ensure unique index
|
||||
new_candidate.is_dummy = true;
|
||||
new_candidates.push(new_candidate);
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ impl<N: Number> Election<N> {
|
||||
}
|
||||
|
||||
/// A candidate in an [Election]
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
#[derive(Clone, Eq)]
|
||||
#[cfg_attr(not(target_arch = "wasm32"), derive(Archive, Deserialize, Serialize))]
|
||||
pub struct Candidate {
|
||||
/// Index of the candidate
|
||||
@ -111,13 +111,21 @@ pub struct Candidate {
|
||||
pub is_dummy: bool,
|
||||
}
|
||||
|
||||
impl PartialEq for Candidate {
|
||||
// Custom implementation of eq for HashMap purposes, to improve performance
|
||||
//
|
||||
// SAFETY: Results in undefined behaviour if multiple Candidates are allowed to have the same index
|
||||
fn eq(&self, other: &Candidate) -> bool {
|
||||
return self.index == other.index;
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for Candidate {
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
// Custom implementation of hash for use with NoHashHasher, to improve performance
|
||||
hasher.write_usize(self.index);
|
||||
}
|
||||
}
|
||||
|
||||
impl nohash_hasher::IsEnabled for Candidate {}
|
||||
|
||||
/// The current state of counting an [Election]
|
||||
|
Loading…
Reference in New Issue
Block a user