Skip to content

Commit

Permalink
improving perfs by using parking_lot crate
Browse files Browse the repository at this point in the history
  • Loading branch information
bananasmoothii committed Oct 13, 2023
1 parent 32c0050 commit 10d6ee8
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 28 deletions.
128 changes: 120 additions & 8 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ rayon = "1.8"
rand = "0.8"
console = "0.15"
thousands = "0.2"
parking_lot = "0.12"

[profile.release]
debug = true
lto = 'thin'
panic = 'abort'
codegen-units = 1
17 changes: 6 additions & 11 deletions src/game/connect4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ impl ConnectFour {
self.play(player, NonZeroUsize::new(column).unwrap())
}

pub fn get_winner_coords(
&self,
) -> Option<[<Self as Game>::Coordinate; 4]> {
pub fn get_winner_coords(&self) -> Option<[<Self as Game>::Coordinate; 4]> {
if self.last_played_coords.is_none() {
return None;
}
Expand Down Expand Up @@ -304,11 +302,8 @@ impl ConnectFour {
self.winner = Some(counting_player);
return;
}
let count_opposite = self.count_in_direction(
last_coords,
count_direction.opposite(),
3 - count,
);
let count_opposite =
self.count_in_direction(last_coords, count_direction.opposite(), 3 - count);
if count + count_opposite == 3 {
self.winner = Some(counting_player);
return;
Expand Down Expand Up @@ -337,7 +332,7 @@ impl ConnectFour {
}
}

const RANDOMIZE_POSSIBLE_PLAYS: bool = false;
const RANDOMIZE_POSSIBLE_PLAYS: bool = true;
}

impl Game for ConnectFour {
Expand Down Expand Up @@ -460,8 +455,8 @@ impl Game for ConnectFour {
let order: [usize; 7] = if Self::RANDOMIZE_POSSIBLE_PLAYS {
match rand::thread_rng().gen_range(0..=2) {
0 => [4, 3, 5, 2, 6, 1, 7],
1 => [3, 5, 4, 2, 6, 1, 7],
_ => [2, 6, 4, 3, 5, 1, 7],
1 => [3, 5, 4, 6, 2, 1, 7],
_ => [6, 2, 4, 5, 3, 1, 7],
}
} else {
[4, 3, 5, 2, 6, 1, 7]
Expand Down
21 changes: 12 additions & 9 deletions src/min_max.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::sync::atomic::Ordering::Relaxed;
use std::sync::atomic::{AtomicBool, AtomicI32};
use std::sync::{Arc, Mutex};
use std::sync::Arc;

use parking_lot::Mutex;
use rayon::iter::*;

use crate::game::player::Player;
Expand Down Expand Up @@ -58,7 +59,9 @@ impl<G: Game> GameNode<G> {
}
}

const FORK_DEPTH: u32 = 4;
const FIRST_FORK: i32 = 3;

const FORK_INTERVAL: i32 = 4;

const USE_GAME_SCORE: bool = true;

Expand All @@ -85,7 +88,7 @@ impl<G: Game> GameNode<G> {
worst_sibling_score: Arc<Mutex<G::Score>>,
#[cfg(debug_assertions)] call_count: Arc<AtomicI32>,
) -> G::Score {
assert!(self.depth() >= real_plays, "Negative exploration");
debug_assert!(self.depth() >= real_plays, "Negative exploration");

#[cfg(debug_assertions)]
call_count.fetch_add(1, Relaxed);
Expand Down Expand Up @@ -125,7 +128,7 @@ impl<G: Game> GameNode<G> {
#[cfg(debug_assertions)]
call_count.clone(),
);
let mut worst_child_score = worst_child_score.lock().unwrap();
let mut worst_child_score = worst_child_score.lock();
// println!("maximize: {maximize}, child: {child_score} worst child: {worst_child_score}");

if (maximize && child_score > *worst_child_score)
Expand All @@ -134,7 +137,7 @@ impl<G: Game> GameNode<G> {
*worst_child_score = child_score;
}
let parent_maximize = !maximize;
let worst_sibling_score = worst_sibling_score.lock().unwrap();
let worst_sibling_score = worst_sibling_score.lock();

// if the parent will not choose us
if (parent_maximize && *worst_child_score < *worst_sibling_score)
Expand All @@ -158,14 +161,14 @@ impl<G: Game> GameNode<G> {
//print!("F");
maybe_explore_children(child)
});
let weight = (*worst_child_score.lock().unwrap()).into();
let weight = (*worst_child_score.lock()).into();
self.set_weight(weight);
weight.unwrap()
} else {
self.children
.iter_mut()
.try_for_each(|(_, child)| maybe_explore_children(child));
let weight = (*worst_child_score.lock().unwrap()).into();
let weight = (*worst_child_score.lock()).into();
self.set_weight(weight);
weight.unwrap()
};
Expand All @@ -178,8 +181,8 @@ impl<G: Game> GameNode<G> {
}

fn is_parallelize_depth(&self, real_plays: u32) -> bool {
// no need to check whether it overflows as there won't be u32::MAX plays
self.depth().overflowing_sub(real_plays).0 == Self::FORK_DEPTH && Self::MULTI_THREADING
let depth = self.depth() as i32 - real_plays as i32;
(depth - Self::FIRST_FORK) % Self::FORK_INTERVAL == 0 && Self::MULTI_THREADING
}

/// Returns true if childrens should be checked for win or draw, false if they were already checked
Expand Down

0 comments on commit 10d6ee8

Please sign in to comment.