Skip to content

Commit

Permalink
avoid binary search if there is nothing to find (#470)
Browse files Browse the repository at this point in the history
Even though failing is cheap, it's not free and can be done a million
times.
  • Loading branch information
Byron committed Sep 20, 2022
1 parent b8f2f8b commit ffd4f0f
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions gitoxide-core/src/hours.rs
Expand Up @@ -361,15 +361,19 @@ fn estimate_hours(commits: &[(u32, actor::SignatureRef<'static>)], stats: &[(u32
email: author.email,
hours: FIRST_COMMIT_ADDITION_IN_MINUTES / 60.0 + hours_for_commits,
num_commits: commits.len() as u32,
stats: commits.iter().map(|t| &t.0).fold(Stats::default(), |mut acc, id| {
match stats.binary_search_by(|t| t.0.cmp(id)) {
Ok(idx) => {
acc.add(&stats[idx].1);
acc
}
Err(_) => acc,
}
}),
stats: (!stats.is_empty())
.then(|| {
commits.iter().map(|t| &t.0).fold(Stats::default(), |mut acc, id| {
match stats.binary_search_by(|t| t.0.cmp(id)) {
Ok(idx) => {
acc.add(&stats[idx].1);
acc
}
Err(_) => acc,
}
})
})
.unwrap_or_default(),
}
}

Expand Down

0 comments on commit ffd4f0f

Please sign in to comment.