alloc test

This commit is contained in:
Simon Gardling 2025-03-13 10:02:54 -04:00
parent ed5ad738e3
commit efd956d614
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
11 changed files with 113 additions and 12 deletions

51
Cargo.lock generated
View File

@ -11,6 +11,27 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "allocative"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fac2ce611db8b8cee9b2aa886ca03c924e9da5e5295d0dbd0526e5d0b0710f7"
dependencies = [
"allocative_derive",
"ctor",
]
[[package]]
name = "allocative_derive"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe233a377643e0fc1a56421d7c90acdec45c291b30345eb9f08e8d0ddce5a4ab"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.100",
]
[[package]] [[package]]
name = "anes" name = "anes"
version = "0.1.6" version = "0.1.6"
@ -206,6 +227,16 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929" checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
[[package]]
name = "ctor"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096"
dependencies = [
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "either" name = "either"
version = "1.15.0" version = "1.15.0"
@ -415,6 +446,7 @@ checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
name = "othello" name = "othello"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"allocative",
"arrayvec", "arrayvec",
"console", "console",
"const_fn", "const_fn",
@ -608,7 +640,7 @@ checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.100",
] ]
[[package]] [[package]]
@ -635,6 +667,17 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.100" version = "2.0.100"
@ -709,7 +752,7 @@ dependencies = [
"log", "log",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.100",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -731,7 +774,7 @@ checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.100",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -873,5 +916,5 @@ checksum = "6352c01d0edd5db859a63e2605f4ea3183ddbd15e2c4a9e7d32184df75e4f154"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.100",
] ]

View File

@ -22,6 +22,7 @@ inherits = "release"
debug = true debug = true
[dependencies] [dependencies]
allocative = "0.3.4"
arrayvec = "0.7" arrayvec = "0.7"
console = "0.15" console = "0.15"
const_fn = "0.4" const_fn = "0.4"

44
src/allocs.rs Normal file
View File

@ -0,0 +1,44 @@
use crate::{
logic::{ChildrenEvalMethod, FutureMoveConfig, FutureMoves},
repr::{Board, Piece, Winner},
};
use allocative::FlameGraphBuilder;
use rand::seq::IteratorRandom;
pub fn run() {
let mut flamegraph = FlameGraphBuilder::default();
let mut fut = FutureMoves::new(
Piece::Black,
FutureMoveConfig {
max_depth: 20,
min_arena_depth: 14,
top_k_children: 2,
up_to_minus: 10,
max_arena_size: 100_000_000,
do_prune: true,
print: true,
children_eval_method: ChildrenEvalMethod::AverageDivDepth,
},
);
let mut board = Board::new().starting_pos();
let mut rng = rand::rng();
let mut i = 0;
while board.game_winner() == Winner::None && i < 2 {
fut.update_from_board(&board);
flamegraph.visit_root(&fut);
fut.generate();
flamegraph.visit_root(&fut);
if let Some(m) = fut.best_move().expect("FutureMoves has no move?") {
board.place(m, Piece::Black).expect("failed move");
}
if let Some(m) = board.possible_moves(Piece::White).choose(&mut rng) {
board.place(m, Piece::White).expect("enemy failed move");
}
i += 1;
}
println!("{}", flamegraph.finish_and_write_flame_graph());
}

View File

@ -1,4 +1,5 @@
mod agent; mod agent;
mod allocs;
mod complexagent; mod complexagent;
mod elo; mod elo;
mod game; mod game;

View File

@ -2,9 +2,11 @@ use crate::{
logic::r#move::Move, logic::r#move::Move,
repr::{Board, CoordPair, Piece, Winner}, repr::{Board, CoordPair, Piece, Winner},
}; };
use allocative::Allocative;
use indicatif::{ProgressIterator, ProgressStyle}; use indicatif::{ProgressIterator, ProgressStyle};
use std::{collections::HashMap, hash::BuildHasherDefault, ops::ControlFlow}; use std::{collections::HashMap, hash::BuildHasherDefault, ops::ControlFlow};
#[derive(Allocative)]
pub struct FutureMoves { pub struct FutureMoves {
/// Arena containing all [`Move`] /// Arena containing all [`Move`]
arena: Vec<Move>, arena: Vec<Move>,
@ -23,7 +25,8 @@ pub struct FutureMoves {
board: Board, board: Board,
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone, Allocative)]
pub struct FutureMoveConfig { pub struct FutureMoveConfig {
/// Max depth of that we should try and traverse /// Max depth of that we should try and traverse
pub max_depth: usize, pub max_depth: usize,
@ -73,7 +76,7 @@ impl std::fmt::Display for FutureMoveConfig {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy, Allocative)]
#[allow(dead_code)] #[allow(dead_code)]
pub enum ChildrenEvalMethod { pub enum ChildrenEvalMethod {
/// Best (by far) strat compared to Max or Min /// Best (by far) strat compared to Max or Min

View File

@ -1,8 +1,9 @@
use super::board_value::BoardValueMap; use super::board_value::BoardValueMap;
use crate::repr::{Board, CoordPair, Piece, Winner}; use crate::repr::{Board, CoordPair, Piece, Winner};
use allocative::Allocative;
use std::sync::LazyLock; use std::sync::LazyLock;
#[derive(Clone, Debug)] #[derive(Clone, Debug, Allocative)]
pub struct Move { pub struct Move {
/// Coordinates (i, j) of the move (if it exists) /// Coordinates (i, j) of the move (if it exists)
pub coord: Option<CoordPair>, pub coord: Option<CoordPair>,

View File

@ -3,6 +3,7 @@ use logic::{ChildrenEvalMethod, FutureMoveConfig};
use repr::Piece; use repr::Piece;
mod agent; mod agent;
mod allocs;
mod complexagent; mod complexagent;
mod elo; mod elo;
mod game; mod game;
@ -12,6 +13,8 @@ pub mod repr;
// TODO! make this agent configuration a config option via `clap-rs` // TODO! make this agent configuration a config option via `clap-rs`
fn main() { fn main() {
allocs::run();
return;
// elo::run(); // elo::run();
// return; // return;
let player1 = complexagent::ComplexAgent::new( let player1 = complexagent::ComplexAgent::new(

View File

@ -3,11 +3,12 @@ use super::{
coords::{CoordPair, CoordPairInner}, coords::{CoordPair, CoordPairInner},
CoordAxis, CoordAxis,
}; };
use allocative::Allocative;
use static_assertions::const_assert; use static_assertions::const_assert;
pub type BitBoardInner = u64; pub type BitBoardInner = u64;
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq, Allocative)]
pub struct BitBoard(BitBoardInner); pub struct BitBoard(BitBoardInner);
// BitBoard should be big enough to fit all points on the board // BitBoard should be big enough to fit all points on the board

View File

@ -1,4 +1,5 @@
use super::{bitboard::BitBoard, piece::Piece, CoordAxis, CoordPair}; use super::{bitboard::BitBoard, piece::Piece, CoordAxis, CoordPair};
use allocative::Allocative;
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use rand::seq::IteratorRandom; use rand::seq::IteratorRandom;
use std::{cmp::Ordering, fmt}; use std::{cmp::Ordering, fmt};
@ -39,7 +40,7 @@ impl<T: Default + Copy> From<PosMapOrig<T>> for PosMap<T> {
} }
} }
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug, Allocative)]
pub enum Winner { pub enum Winner {
Player(Piece), Player(Piece),
Tie, Tie,
@ -81,7 +82,7 @@ macro_rules! get_board {
} }
/// Repersents a Othello game board at a certain space /// Repersents a Othello game board at a certain space
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq, Allocative)]
pub struct Board { pub struct Board {
/// [`BitBoard`] containing all white pieces /// [`BitBoard`] containing all white pieces
white_board: BitBoard, white_board: BitBoard,

View File

@ -1,4 +1,5 @@
use super::Board; use super::Board;
use allocative::Allocative;
use num::Integer; use num::Integer;
use static_assertions::const_assert; use static_assertions::const_assert;
@ -11,7 +12,7 @@ pub type CoordPairInner = u8;
const_assert!(CoordPairInner::MAX as usize >= Board::AREA.0 as usize); const_assert!(CoordPairInner::MAX as usize >= Board::AREA.0 as usize);
#[derive(PartialEq, Eq, Copy, Clone, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Hash, Allocative)]
pub struct CoordPair(pub CoordPairInner); pub struct CoordPair(pub CoordPairInner);
impl CoordPair { impl CoordPair {

View File

@ -1,4 +1,6 @@
#[derive(Copy, Clone, PartialEq, Eq, Debug)] use allocative::Allocative;
#[derive(Copy, Clone, PartialEq, Eq, Debug, Allocative)]
pub enum Piece { pub enum Piece {
Black, Black,
White, White,