diff options
| author | mat <git@matdoes.dev> | 2024-12-26 05:52:46 +0000 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2024-12-26 05:52:46 +0000 |
| commit | 3c83e5b24a622062c490f90c7e5bde043438d517 (patch) | |
| tree | 6f1efdf0f89233743576c149409041e0685eb38c | |
| parent | e11a902fba8c8c63be982afbc6085a49c6a19f12 (diff) | |
| download | azalea-drasl-3c83e5b24a622062c490f90c7e5bde043438d517.tar.xz | |
replace priority_queue crate with std BinaryHeap
| -rw-r--r-- | Cargo.lock | 12 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | azalea/Cargo.toml | 3 | ||||
| -rw-r--r-- | azalea/src/pathfinder/astar.rs | 34 |
4 files changed, 19 insertions, 32 deletions
@@ -236,7 +236,6 @@ dependencies = [ "num-format", "num-traits", "parking_lot", - "priority-queue", "rand", "rustc-hash 2.1.0", "serde", @@ -2299,17 +2298,6 @@ dependencies = [ ] [[package]] -name = "priority-queue" -version = "2.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "714c75db297bc88a63783ffc6ab9f830698a6705aa0201416931759ef4c8183d" -dependencies = [ - "autocfg", - "equivalent", - "indexmap", -] - -[[package]] name = "proc-macro2" version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -56,7 +56,6 @@ nohash-hasher = "0.2.0" num-bigint = "0.4.6" num-traits = "0.2.19" parking_lot = "0.12.3" -priority-queue = "2.1.1" proc-macro2 = "1.0.92" quote = "1.0.37" rand = "0.8.5" @@ -79,6 +78,7 @@ tracing = "0.1.41" tracing-subscriber = "0.3.19" hickory-resolver = { version = "0.24.2", default-features = false } uuid = "1.11.0" +num-format = "0.4.4" # --- Profile Settings --- diff --git a/azalea/Cargo.toml b/azalea/Cargo.toml index 3e5aa275..739eb5f7 100644 --- a/azalea/Cargo.toml +++ b/azalea/Cargo.toml @@ -35,10 +35,9 @@ derive_more = { workspace = true, features = ["deref", "deref_mut"] } futures = { workspace = true } futures-lite = { workspace = true } nohash-hasher = { workspace = true } -num-format = "0.4.4" +num-format = { workspace = true } num-traits = { workspace = true } parking_lot = { workspace = true } -priority-queue = { workspace = true } rustc-hash = { workspace = true } serde = { workspace = true, optional = true } thiserror = { workspace = true } diff --git a/azalea/src/pathfinder/astar.rs b/azalea/src/pathfinder/astar.rs index 03dca1dd..9948d315 100644 --- a/azalea/src/pathfinder/astar.rs +++ b/azalea/src/pathfinder/astar.rs @@ -1,12 +1,12 @@ use std::{ - cmp::Reverse, + cmp::{self}, + collections::BinaryHeap, fmt::Debug, hash::Hash, time::{Duration, Instant}, }; use num_format::ToFormattedString; -use priority_queue::PriorityQueue; use rustc_hash::FxHashMap; use tracing::{debug, trace, warn}; @@ -52,8 +52,8 @@ where { let start_time = Instant::now(); - let mut open_set = PriorityQueue::new(); - open_set.push(start, Reverse(Weight(0.))); + let mut open_set = BinaryHeap::<WeightedNode<P>>::new(); + open_set.push(WeightedNode(start, 0.)); let mut nodes: FxHashMap<P, Node<P, M>> = FxHashMap::default(); nodes.insert( start, @@ -71,7 +71,7 @@ where let mut num_nodes = 0; - while let Some((current_node, _)) = open_set.pop() { + while let Some(WeightedNode(current_node, _)) = open_set.pop() { num_nodes += 1; if success(current_node) { debug!("Nodes considered: {num_nodes}"); @@ -106,7 +106,7 @@ where f_score, }, ); - open_set.push(neighbor.movement.target, Reverse(Weight(f_score))); + open_set.push(WeightedNode(neighbor.movement.target, f_score)); for (coefficient_i, &coefficient) in COEFFICIENTS.iter().enumerate() { let node_score = heuristic + tentative_g_score / coefficient; @@ -118,8 +118,8 @@ where } } - // check for timeout every ~1ms - if num_nodes % 1000 == 0 { + // check for timeout every ~20ms + if num_nodes % 10000 == 0 { let timed_out = match timeout { PathfinderTimeout::Time(max_duration) => start_time.elapsed() > max_duration, PathfinderTimeout::Nodes(max_nodes) => num_nodes > max_nodes, @@ -218,17 +218,17 @@ impl<P: Hash + Copy + Clone, M: Clone> Clone for Movement<P, M> { } #[derive(PartialEq)] -pub struct Weight(f32); -impl Ord for Weight { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.0 - .partial_cmp(&other.0) - .unwrap_or(std::cmp::Ordering::Equal) +pub struct WeightedNode<P: PartialEq>(P, f32); + +impl<P: PartialEq> Ord for WeightedNode<P> { + fn cmp(&self, other: &Self) -> cmp::Ordering { + // intentionally inverted to make the BinaryHeap a min-heap + other.1.partial_cmp(&self.1).unwrap_or(cmp::Ordering::Equal) } } -impl Eq for Weight {} -impl PartialOrd for Weight { - fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { +impl<P: PartialEq> Eq for WeightedNode<P> {} +impl<P: PartialEq> PartialOrd for WeightedNode<P> { + fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { Some(self.cmp(other)) } } |
