Skip to content

Commit 8fe06d6

Browse files
committed
use rustc_hash::FxHasher instead of DefaultHasher for 2x-5x faster genes hashing
1 parent b0a1237 commit 8fe06d6

File tree

12 files changed

+28
-20
lines changed

12 files changed

+28
-20
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ log = "0.4.0"
2626
cardinality-estimator = "1.0.2"
2727
impl-trait-for-tuples = "0.2.2"
2828
fixedbitset = "0.5.7"
29+
rustc-hash = "2.1.0"
2930
bytemuck = { version = "1.21.0", features = ["derive"] }
3031

3132
[dev-dependencies]

src/genotype/binary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use itertools::Itertools;
66
use num::BigUint;
77
use rand::distributions::{Standard, Uniform};
88
use rand::prelude::*;
9-
use std::collections::hash_map::DefaultHasher;
9+
use rustc_hash::FxHasher;
1010
use std::fmt;
1111
use std::hash::{Hash, Hasher};
1212

@@ -84,7 +84,7 @@ impl Genotype for Binary {
8484
}
8585
fn calculate_genes_hash(&self, chromosome: &Self::Chromosome) -> Option<GenesHash> {
8686
if self.genes_hashing {
87-
let mut s = DefaultHasher::new();
87+
let mut s = FxHasher::default();
8888
chromosome.genes.hash(&mut s);
8989
Some(s.finish())
9090
} else {

src/genotype/bit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use itertools::Itertools;
77
use num::BigUint;
88
use rand::distributions::{Standard, Uniform};
99
use rand::prelude::*;
10-
use std::collections::hash_map::DefaultHasher;
10+
use rustc_hash::FxHasher;
1111
use std::fmt;
1212
use std::hash::{Hash, Hasher};
1313

@@ -146,7 +146,7 @@ impl Genotype for Bit {
146146
}
147147
fn calculate_genes_hash(&self, chromosome: &Self::Chromosome) -> Option<GenesHash> {
148148
if self.genes_hashing {
149-
let mut s = DefaultHasher::new();
149+
let mut s = FxHasher::default();
150150
chromosome.genes.hash(&mut s);
151151
Some(s.finish())
152152
} else {

src/genotype/dynamic_matrix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use num::BigUint;
1212
use rand::distributions::uniform::SampleUniform;
1313
use rand::distributions::{Distribution, Uniform};
1414
use rand::prelude::*;
15+
use rustc_hash::FxHasher;
1516
use std::cmp::Ordering;
16-
use std::collections::hash_map::DefaultHasher;
1717
use std::fmt;
1818
use std::hash::{Hash, Hasher};
1919
use std::ops::{Bound, Range, RangeBounds, RangeInclusive};
@@ -327,7 +327,7 @@ where
327327
}
328328
fn calculate_genes_hash(&self, chromosome: &Self::Chromosome) -> Option<GenesHash> {
329329
if self.genes_hashing {
330-
let mut s = DefaultHasher::new();
330+
let mut s = FxHasher::default();
331331
let bytes: &[u8] = cast_slice(self.genes_slice(chromosome));
332332
bytes.hash(&mut s);
333333
Some(s.finish())

src/genotype/list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use itertools::Itertools;
77
use num::BigUint;
88
use rand::distributions::{Distribution, Uniform};
99
use rand::prelude::*;
10-
use std::collections::hash_map::DefaultHasher;
10+
use rustc_hash::FxHasher;
1111
use std::fmt;
1212
use std::hash::{Hash, Hasher};
1313

@@ -122,7 +122,7 @@ impl<T: Allele + PartialEq + Hash> Genotype for List<T> {
122122
}
123123
fn calculate_genes_hash(&self, chromosome: &Self::Chromosome) -> Option<GenesHash> {
124124
if self.genes_hashing {
125-
let mut s = DefaultHasher::new();
125+
let mut s = FxHasher::default();
126126
chromosome.genes.hash(&mut s);
127127
Some(s.finish())
128128
} else {

src/genotype/multi_list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use itertools::Itertools;
99
use num::BigUint;
1010
use rand::distributions::{Distribution, Uniform, WeightedIndex};
1111
use rand::prelude::*;
12-
use std::collections::hash_map::DefaultHasher;
12+
use rustc_hash::FxHasher;
1313
use std::fmt;
1414
use std::hash::{Hash, Hasher};
1515

@@ -164,7 +164,7 @@ impl<T: Allele + PartialEq + Hash> Genotype for MultiList<T> {
164164
}
165165
fn calculate_genes_hash(&self, chromosome: &Self::Chromosome) -> Option<GenesHash> {
166166
if self.genes_hashing {
167-
let mut s = DefaultHasher::new();
167+
let mut s = FxHasher::default();
168168
chromosome.genes.hash(&mut s);
169169
Some(s.finish())
170170
} else {

src/genotype/multi_range.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use num::BigUint;
1111
use rand::distributions::uniform::SampleUniform;
1212
use rand::distributions::{Distribution, Uniform, WeightedIndex};
1313
use rand::prelude::*;
14-
use std::collections::hash_map::DefaultHasher;
14+
use rustc_hash::FxHasher;
1515
use std::fmt;
1616
use std::hash::{Hash, Hasher};
1717
use std::ops::RangeInclusive;
@@ -261,7 +261,7 @@ where
261261
}
262262
fn calculate_genes_hash(&self, chromosome: &Self::Chromosome) -> Option<GenesHash> {
263263
if self.genes_hashing {
264-
let mut s = DefaultHasher::new();
264+
let mut s = FxHasher::default();
265265
let bytes: &[u8] = cast_slice(self.genes_slice(chromosome));
266266
bytes.hash(&mut s);
267267
Some(s.finish())

src/genotype/multi_unique.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use itertools::Itertools;
1010
use num::BigUint;
1111
use rand::distributions::{Distribution, Uniform, WeightedIndex};
1212
use rand::prelude::*;
13-
use std::collections::hash_map::DefaultHasher;
13+
use rustc_hash::FxHasher;
1414
use std::collections::HashMap;
1515
use std::fmt;
1616
use std::hash::{Hash, Hasher};
@@ -166,7 +166,7 @@ impl<T: Allele + Hash> Genotype for MultiUnique<T> {
166166
}
167167
fn calculate_genes_hash(&self, chromosome: &Self::Chromosome) -> Option<GenesHash> {
168168
if self.genes_hashing {
169-
let mut s = DefaultHasher::new();
169+
let mut s = FxHasher::default();
170170
chromosome.genes.hash(&mut s);
171171
Some(s.finish())
172172
} else {

src/genotype/range.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use num::BigUint;
99
use rand::distributions::uniform::SampleUniform;
1010
use rand::distributions::{Distribution, Uniform};
1111
use rand::prelude::*;
12-
use std::collections::hash_map::DefaultHasher;
12+
use rustc_hash::FxHasher;
1313
use std::fmt;
1414
use std::hash::{Hash, Hasher};
1515
use std::ops::RangeInclusive;
@@ -204,7 +204,7 @@ where
204204
}
205205
fn calculate_genes_hash(&self, chromosome: &Self::Chromosome) -> Option<GenesHash> {
206206
if self.genes_hashing {
207-
let mut s = DefaultHasher::new();
207+
let mut s = FxHasher::default();
208208
let bytes: &[u8] = cast_slice(self.genes_slice(chromosome));
209209
bytes.hash(&mut s);
210210
Some(s.finish())

0 commit comments

Comments
 (0)