Skip to content

Commit

Permalink
replace a linear search with a binary search . . .
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Oct 5, 2023
1 parent e4e0433 commit 177864b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions azalea-core/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl Hash for ChunkPos {
impl nohash_hasher::IsEnabled for ChunkPos {}

/// The coordinates of a chunk section in the world.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct ChunkSectionPos {
pub x: i32,
pub y: i32,
Expand Down Expand Up @@ -413,9 +413,9 @@ impl From<BlockPos> for ChunkSectionBlockPos {
#[inline]
fn from(pos: BlockPos) -> Self {
ChunkSectionBlockPos {
x: pos.x as u8 & 0xF,
y: pos.y as u8 & 0xF,
z: pos.z as u8 & 0xF,
x: (pos.x & 0xF) as u8,
y: (pos.y & 0xF) as u8,
z: (pos.z & 0xF) as u8,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions azalea/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ uuid = "1.4.1"
bevy_log = "0.11.3"
azalea-entity = { version = "0.8.0", path = "../azalea-entity" }
bevy_time = "0.11.3"
rustc-hash = "1.1.0"

[dev-dependencies]
criterion = "0.5.1"
Expand Down
6 changes: 3 additions & 3 deletions azalea/src/pathfinder/astar.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{
cmp::Reverse,
collections::HashMap,
fmt::Debug,
hash::Hash,
time::{Duration, Instant},
};

use log::{debug, trace, warn};
use priority_queue::PriorityQueue;
use rustc_hash::FxHashMap;

pub struct Path<P, M>
where
Expand Down Expand Up @@ -40,7 +40,7 @@ where

let mut open_set = PriorityQueue::new();
open_set.push(start, Reverse(Weight(0.)));
let mut nodes: HashMap<P, Node<P, M>> = HashMap::new();
let mut nodes: FxHashMap<P, Node<P, M>> = FxHashMap::default();
nodes.insert(
start,
Node {
Expand Down Expand Up @@ -134,7 +134,7 @@ where
best_paths[0]
}

fn reconstruct_path<P, M>(mut nodes: HashMap<P, Node<P, M>>, current: P) -> Vec<Movement<P, M>>
fn reconstruct_path<P, M>(mut nodes: FxHashMap<P, Node<P, M>>, current: P) -> Vec<Movement<P, M>>
where
P: Eq + Hash + Copy + Debug,
{
Expand Down
11 changes: 9 additions & 2 deletions azalea/src/pathfinder/moves/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub struct PathfinderCtx {
#[derive(Default)]
pub struct CachedSections {
pub last_index: usize,
pub second_last_index: usize,
pub sections: Vec<CachedSection>,
}

Expand All @@ -67,15 +68,20 @@ impl CachedSections {
if let Some(last_item) = self.sections.get(self.last_index) {
if last_item.pos == pos {
return Some(&mut self.sections[self.last_index]);
} else if let Some(second_last_item) = self.sections.get(self.second_last_index) {
if second_last_item.pos == pos {
return Some(&mut self.sections[self.second_last_index]);
}
}
}

let index = self
.sections
.iter_mut()
.position(|section| section.pos == pos);
.binary_search_by(|section| section.pos.cmp(&pos))
.ok();

if let Some(index) = index {
self.second_last_index = self.last_index;
self.last_index = index;
return Some(&mut self.sections[index]);
}
Expand All @@ -85,6 +91,7 @@ impl CachedSections {
#[inline]
pub fn insert(&mut self, section: CachedSection) {
self.sections.push(section);
self.sections.sort_unstable_by(|a, b| a.pos.cmp(&b.pos));
}
}

Expand Down

0 comments on commit 177864b

Please sign in to comment.