-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a
KPatternStack
to reduce allocations during IDFS.
Performance impact is small but seems to be roughly a 3% for 3x3x3: git checkout f16af8e70cc4470d49d0b380249f7b41f6e43c00 git checkout c4720e54bff7563b32d213f622f9c986d0b8807f cargo build cp ./target/release/twsearch /tmp/twsearch-with-stack git checkout f16af8e70cc4470d49d0b380249f7b41f6e43c00 cargo build cp ./target/release/twsearch /tmp/twsearch-without-stack hyperfine '/tmp/twsearch-with-stack search --generator-moves F,R,D,B,L --scramble-alg U "samples/json/3x3x3/3x3x3-Reid.def.json"' '/tmp/twsearch-without-stack search --generator-moves F,R,D,B,L --scramble-alg U "samples/json/3x3x3/3x3x3-Reid.def.json"'
- Loading branch information
Showing
3 changed files
with
56 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use cubing::kpuzzle::{KPattern, KTransformation}; | ||
|
||
// Grows from the top | ||
pub(crate) struct KPatternStack { | ||
stack: Vec<KPattern>, | ||
current_idx: usize, | ||
} | ||
|
||
impl KPatternStack { | ||
pub fn new(root_kpattern: KPattern) -> Self { | ||
Self { | ||
stack: vec![root_kpattern], | ||
current_idx: 0, | ||
} | ||
} | ||
|
||
pub fn push(&mut self, transformation: &KTransformation) { | ||
self.current_idx += 1; | ||
if self.current_idx >= self.stack.len() { | ||
self.stack | ||
.push(self.stack[self.current_idx - 1].apply_transformation(transformation)) | ||
} else { | ||
// We have to use `split_at_mut` so that we can borrow both the read and write entries at the same time: https://doc.rust-lang.org/nomicon/borrow-splitting.html | ||
let (left, right) = self.stack.split_at_mut(self.current_idx); | ||
|
||
left.last() | ||
.unwrap() | ||
.apply_transformation_into(transformation, right.first_mut().unwrap()); | ||
} | ||
} | ||
|
||
pub fn current_pattern(&self) -> &KPattern { | ||
&self.stack[self.current_idx] | ||
} | ||
|
||
pub fn pop(&mut self) { | ||
self.current_idx -= 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters