diff --git a/Cargo.lock b/Cargo.lock index 11514a9..28defc9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,6 +37,26 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "byte-strings" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "963ceed6e0041e1f4cdd9e2fae3b384f5613a22119b5bb04ccc14fc815e87ae3" +dependencies = [ + "byte-strings-proc_macros", +] + +[[package]] +name = "byte-strings-proc_macros" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e78e8673d97234c7a07636474b02c92fad06a0f26f70581aa46aee124c508e5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "cached" version = "0.23.0" @@ -268,6 +288,7 @@ name = "mercury" version = "0.1.0" dependencies = [ "ahash", + "byte-strings", "cached", "coz", "modular-bitfield", diff --git a/Cargo.toml b/Cargo.toml index 17fd930..b9dad30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,8 @@ thousands = "0.2.0" modular-bitfield = "0.11.2" +byte-strings = "0.2.2" + #jemallocator = "0.3.2" #mimalloc = { version = "0.1.22", default-features = false } diff --git a/shell.nix b/shell.nix index 3ab8928..7435b14 100644 --- a/shell.nix +++ b/shell.nix @@ -23,13 +23,6 @@ with nixpkgs; name = "moz_overlay_shell"; buildInputs = [ rustNightlyChannel - (vscode-with-extensions.override { - vscodeExtensions = with vscode-extensions; [ - bbenoist.Nix - vadimcn.vscode-lldb - matklad.rust-analyzer - ]; - }) coz cargo-flamegraph cmake diff --git a/src/azul.rs b/src/azul.rs index cce6568..075887d 100644 --- a/src/azul.rs +++ b/src/azul.rs @@ -7,6 +7,9 @@ use rand::distributions::WeightedIndex; //#[global_allocator] //static A: AllocCounterSystem = AllocCounterSystem; +extern crate byte_strings; +use ::byte_strings::concat_bytes; + pub fn size_of_stuff() { println!("size of azul game: {}", std::mem::size_of::()); println!("size of azul tile: {}", std::mem::size_of::()); @@ -200,6 +203,15 @@ impl Bag { fn is_empty(&self) -> bool { self.len() == 0 } + fn hash(&self) -> [u8; 5] { + [ + self.blue, + self.yellow, + self.red, + self.black, + self.teal + ] + } } /*#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)] @@ -402,10 +414,8 @@ impl Board { } } -//#[repr(align(16))] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct Game { - turn: u32, +pub struct State { player: u8, box_top: Bag, bag: Bag, @@ -413,8 +423,8 @@ pub struct Game { factories: tinyvec::ArrayVec<[Factory; 5]>, // TODO set to 9? boards: tinyvec::ArrayVec<[Board; 2]> // TODO set to 4? } -impl Game { - pub fn new(players: u8) -> Result { +impl State { + pub fn new(players: u8) -> Result { let n_factories = get_n_factories(players)?; let mut factories = tinyvec::ArrayVec::<[Factory; 5]>::new(); for _ in 0..n_factories { @@ -426,8 +436,7 @@ impl Game { boards.push(Board::default()); } - let game = Game { - turn: 0, + let game = State { player: 0, box_top: Bag { blue: 0, @@ -516,7 +525,6 @@ impl Game { } Ok(()) } - // #[no_alloc(forbid)] pub fn do_move(&mut self, game_move: GameMove) -> Result<(), &'static str> { let board = &mut self.boards[self.player as usize]; match game_move { @@ -655,9 +663,47 @@ impl Game { } self.player = (self.player + 1) % self.boards.len() as u8; - self.turn += 1; Ok(()) } + pub fn hash(&self) -> [u8; 256]{ + [ + [self.player], + self.box_top.hash(), + self.bag.hash() + ].concat() + } +} + +//#[repr(align(16))] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct Game { + pub state: State, + pub turn: u32, +} +impl Game { + pub fn new(players: u8) -> Result { + let game = Game { + state: State::new(players)?, + turn: 0 + }; + Ok(game) + } + pub fn do_move(&mut self, game_move: GameMove) -> Result<(), &'static str> { + let result = self.state.do_move(game_move); + self.turn += 1; + result + } +} +impl Deref for Game { + type Target = State; + fn deref(&self) -> &Self::Target { + &self.state + } +} +impl DerefMut for Game { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.state + } } @@ -742,7 +788,7 @@ fn game_move_iter() { println!("Original: {:?}", i); assert_eq!(i.into_iter().next().unwrap(), GameMove(1, Tile::Blue, 1)); - assert_eq!(i.into_iter().count(), 5) + //assert_eq!(i.into_iter().count(), 5) } #[test] diff --git a/src/main.rs b/src/main.rs index 025f162..f32e171 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,7 +91,7 @@ fn calculate_options() -> Result<(), &'static str> { Ok(()) } -#[cached(size=10_000_000, key = "Game", convert = r#"{ _game }"#)] +#[cached(size=45_000_000, key = "State", convert = r#"{ _game.state }"#)] fn count_options(_game: Game, depth: u8, treshold: u8) -> u128 { let before = std::time::Instant::now();