Skip to content

Improve perf of fixation removal #3

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

Merged
merged 1 commit into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/fwdpp_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ fn remove_fixations_from_extant_genomes(
genomes: &mut [HaploidGenome],
) -> bool {
if mutation_counts.iter().any(|c| *c == twon) {
genomes
.iter_mut()
.filter(|g| g.count > 0)
.for_each(|g| g.mutations.retain(|k| mutation_counts[*k as usize] < twon));
genomes.iter_mut().filter(|g| g.count > 0).for_each(|g| {
// SAFETY: see comments in genome_array.rs
g.mutations
.retain(|k| *unsafe { mutation_counts.get_unchecked(*k as usize) } < twon)
});
true
} else {
false
Expand Down
22 changes: 21 additions & 1 deletion src/genome_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,30 @@ impl DiploidPopulation {
#[inline(never)]
fn fixation_removal_check(mutation_counts: &[u32], twon: u32, output: &mut HaploidGenomes) -> bool {
if mutation_counts.iter().any(|m| *m == twon) {
// NOTE: using this as an assert
// eliminates all performance
// benefits of the unsafe code below
debug_assert!(output
.mutations
.iter()
.all(|m| (*m as usize) < mutation_counts.len()));
let x = output.mutations.len();
// SAFETY: None in general. :(
// In the specific case of this function not being public,
// genomes only contain mutation keys generated
// as they are added to the pop's mutation array.
// As the same time, we make sure that mutation_counts'
// length is also correct.
//
// This is a sticky issue:
// 1. The perf gain of unsafe here is quite big.
// 2. But it is gonna be hard to encapsulate things
// to the point where we feel generally good about saftety
// 3. Therefore, it seems that the function itself should
// be marked unsafe in the absence of such encapsulation.
output
.mutations
.retain(|m| mutation_counts[*m as usize] < twon);
.retain(|m| *unsafe { mutation_counts.get_unchecked(*m as usize) } < twon);
let delta = x - output.mutations.len();
assert_eq!(delta % output.genome_spans.len(), 0);

Expand Down
3 changes: 2 additions & 1 deletion src/genome_array_counted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ impl DiploidPopulation {
fn fixation_removal_check(mutation_counts: &[u32], twon: u32, output: &mut HaploidGenomes) -> bool {
if mutation_counts.iter().any(|m| *m == twon) {
let x = output.mutations.len();
// SAFETY: see comments in genome_array.rs
output
.mutations
.retain(|m| mutation_counts[*m as usize] < twon);
.retain(|m| *unsafe { mutation_counts.get_unchecked(*m as usize) } < twon);
let delta = x - output.mutations.len();
assert_eq!(delta % output.genome_spans.len(), 0);

Expand Down