aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/pathfinder/moves
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2023-10-05 01:40:25 -0500
committermat <git@matdoes.dev>2023-10-05 01:40:25 -0500
commit177864be60c08cb61fd577eb99ee5c99481fc03e (patch)
tree982fbe6430b1818fec7134e26378fd5f626a1ed4 /azalea/src/pathfinder/moves
parente4e0433853d1e2d9d95d4269700df032db9dd913 (diff)
downloadazalea-drasl-177864be60c08cb61fd577eb99ee5c99481fc03e.tar.xz
replace a linear search with a binary search . . .
Diffstat (limited to 'azalea/src/pathfinder/moves')
-rw-r--r--azalea/src/pathfinder/moves/mod.rs11
1 files changed, 9 insertions, 2 deletions
diff --git a/azalea/src/pathfinder/moves/mod.rs b/azalea/src/pathfinder/moves/mod.rs
index f65e556f..49615a3a 100644
--- a/azalea/src/pathfinder/moves/mod.rs
+++ b/azalea/src/pathfinder/moves/mod.rs
@@ -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>,
}
@@ -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]);
}
@@ -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));
}
}