From efd956d6143e7840b82782b358cb346f4a72007b Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 13 Mar 2025 10:02:54 -0400 Subject: [PATCH] alloc test --- Cargo.lock | 51 ++++++++++++++++++++++++++++++++++++--- Cargo.toml | 1 + src/allocs.rs | 44 +++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/logic/future_moves.rs | 7 ++++-- src/logic/move.rs | 3 ++- src/main.rs | 3 +++ src/repr/bitboard.rs | 3 ++- src/repr/board.rs | 5 ++-- src/repr/coords.rs | 3 ++- src/repr/piece.rs | 4 ++- 11 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 src/allocs.rs diff --git a/Cargo.lock b/Cargo.lock index d68e539..c7c08be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,27 @@ dependencies = [ "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]] name = "anes" version = "0.1.6" @@ -206,6 +227,16 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "either" version = "1.15.0" @@ -415,6 +446,7 @@ checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" name = "othello" version = "0.1.0" dependencies = [ + "allocative", "arrayvec", "console", "const_fn", @@ -608,7 +640,7 @@ checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.100", ] [[package]] @@ -635,6 +667,17 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "syn" version = "2.0.100" @@ -709,7 +752,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn", + "syn 2.0.100", "wasm-bindgen-shared", ] @@ -731,7 +774,7 @@ checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.100", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -873,5 +916,5 @@ checksum = "6352c01d0edd5db859a63e2605f4ea3183ddbd15e2c4a9e7d32184df75e4f154" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.100", ] diff --git a/Cargo.toml b/Cargo.toml index 1fbb168..5674cff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ inherits = "release" debug = true [dependencies] +allocative = "0.3.4" arrayvec = "0.7" console = "0.15" const_fn = "0.4" diff --git a/src/allocs.rs b/src/allocs.rs new file mode 100644 index 0000000..594a3a5 --- /dev/null +++ b/src/allocs.rs @@ -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()); +} diff --git a/src/lib.rs b/src/lib.rs index 2d62ee7..6b4006e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ mod agent; +mod allocs; mod complexagent; mod elo; mod game; diff --git a/src/logic/future_moves.rs b/src/logic/future_moves.rs index 79770db..b6ba15d 100644 --- a/src/logic/future_moves.rs +++ b/src/logic/future_moves.rs @@ -2,9 +2,11 @@ use crate::{ logic::r#move::Move, repr::{Board, CoordPair, Piece, Winner}, }; +use allocative::Allocative; use indicatif::{ProgressIterator, ProgressStyle}; use std::{collections::HashMap, hash::BuildHasherDefault, ops::ControlFlow}; +#[derive(Allocative)] pub struct FutureMoves { /// Arena containing all [`Move`] arena: Vec, @@ -23,7 +25,8 @@ pub struct FutureMoves { board: Board, } -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Allocative)] + pub struct FutureMoveConfig { /// Max depth of that we should try and traverse 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)] pub enum ChildrenEvalMethod { /// Best (by far) strat compared to Max or Min diff --git a/src/logic/move.rs b/src/logic/move.rs index f014948..257898c 100644 --- a/src/logic/move.rs +++ b/src/logic/move.rs @@ -1,8 +1,9 @@ use super::board_value::BoardValueMap; use crate::repr::{Board, CoordPair, Piece, Winner}; +use allocative::Allocative; use std::sync::LazyLock; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Allocative)] pub struct Move { /// Coordinates (i, j) of the move (if it exists) pub coord: Option, diff --git a/src/main.rs b/src/main.rs index 709efc8..7081fd3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use logic::{ChildrenEvalMethod, FutureMoveConfig}; use repr::Piece; mod agent; +mod allocs; mod complexagent; mod elo; mod game; @@ -12,6 +13,8 @@ pub mod repr; // TODO! make this agent configuration a config option via `clap-rs` fn main() { + allocs::run(); + return; // elo::run(); // return; let player1 = complexagent::ComplexAgent::new( diff --git a/src/repr/bitboard.rs b/src/repr/bitboard.rs index 969a5d3..3e48cbc 100644 --- a/src/repr/bitboard.rs +++ b/src/repr/bitboard.rs @@ -3,11 +3,12 @@ use super::{ coords::{CoordPair, CoordPairInner}, CoordAxis, }; +use allocative::Allocative; use static_assertions::const_assert; pub type BitBoardInner = u64; -#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone, PartialEq, Eq, Allocative)] pub struct BitBoard(BitBoardInner); // BitBoard should be big enough to fit all points on the board diff --git a/src/repr/board.rs b/src/repr/board.rs index ba9e76f..799a117 100644 --- a/src/repr/board.rs +++ b/src/repr/board.rs @@ -1,4 +1,5 @@ use super::{bitboard::BitBoard, piece::Piece, CoordAxis, CoordPair}; +use allocative::Allocative; use arrayvec::ArrayVec; use rand::seq::IteratorRandom; use std::{cmp::Ordering, fmt}; @@ -39,7 +40,7 @@ impl From> for PosMap { } } -#[derive(PartialEq, Eq, Copy, Clone, Debug)] +#[derive(PartialEq, Eq, Copy, Clone, Debug, Allocative)] pub enum Winner { Player(Piece), Tie, @@ -81,7 +82,7 @@ macro_rules! get_board { } /// Repersents a Othello game board at a certain space -#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone, PartialEq, Eq, Allocative)] pub struct Board { /// [`BitBoard`] containing all white pieces white_board: BitBoard, diff --git a/src/repr/coords.rs b/src/repr/coords.rs index f3bf238..063ca56 100644 --- a/src/repr/coords.rs +++ b/src/repr/coords.rs @@ -1,4 +1,5 @@ use super::Board; +use allocative::Allocative; use num::Integer; use static_assertions::const_assert; @@ -11,7 +12,7 @@ pub type CoordPairInner = u8; 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); impl CoordPair { diff --git a/src/repr/piece.rs b/src/repr/piece.rs index 9a299af..5c8bc20 100644 --- a/src/repr/piece.rs +++ b/src/repr/piece.rs @@ -1,4 +1,6 @@ -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +use allocative::Allocative; + +#[derive(Copy, Clone, PartialEq, Eq, Debug, Allocative)] pub enum Piece { Black, White,