Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/directed/astar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ where
FH: FnMut(&N) -> C,
FS: FnMut(&N) -> bool,
{
let mut to_see = BinaryHeap::new();
// Pre-allocate heap with a reasonable default capacity to reduce reallocations
let mut to_see = BinaryHeap::with_capacity(64);
to_see.push(SmallestCostHolder {
estimated_cost: Zero::zero(),
cost: Zero::zero(),
Expand Down Expand Up @@ -184,7 +185,8 @@ where
FH: FnMut(&N) -> C,
FS: FnMut(&N) -> bool,
{
let mut to_see = BinaryHeap::new();
// Pre-allocate heap with a reasonable default capacity to reduce reallocations
let mut to_see = BinaryHeap::with_capacity(64);
let mut min_cost = None;
let mut sinks = HashSet::new();
to_see.push(SmallestCostHolder {
Expand Down
11 changes: 8 additions & 3 deletions src/undirected/connected_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::marker::PhantomData;

use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};

/// A connected component implementation for various generic types.
///
Expand Down Expand Up @@ -60,7 +60,9 @@ where
#[must_use]
pub fn separate_components(groups: &[It]) -> (HashMap<&N, usize>, Vec<usize>) {
let mut table = (0..groups.len()).collect::<Vec<_>>();
let mut indices = HashMap::new();
// Pre-size the hash map to reduce reallocations
let estimated_capacity = groups.iter().map(|g| g.into_iter().count()).sum();
let mut indices = HashMap::with_capacity(estimated_capacity);
for (mut group_index, group) in groups.iter().enumerate() {
let mut is_empty = true;
for element in group {
Expand Down Expand Up @@ -103,7 +105,10 @@ where
#[must_use]
pub fn components(groups: &[It]) -> C2 {
let (_, gindices) = Self::separate_components(groups);
let mut gb: FxHashMap<usize, FxHashSet<N>> = FxHashMap::default();
// Pre-size the hash map to reduce reallocations
let estimated_capacity = gindices.iter().filter(|&&n| n != usize::MAX).count();
let mut gb: FxHashMap<usize, FxHashSet<N>> =
FxHashMap::with_capacity_and_hasher(estimated_capacity, FxBuildHasher);
for (i, n) in gindices
.into_iter()
.enumerate()
Expand Down
Loading