overhaul chain system

This made the code so fast, that the `board` benchmark became completely useless.
So that benchmark was removed.

Overall, we're looking at a futher 20% performance increase in `future_moves`
This commit is contained in:
2025-03-04 13:18:17 -05:00
parent fa7ad34dcb
commit 204ba85202
10 changed files with 190 additions and 295 deletions

View File

@@ -1,25 +1,10 @@
use super::{
board::Board,
coords::{CoordPair, CoordPairInner},
CoordAxis,
};
use const_fn::const_fn;
use static_assertions::const_assert;
// quick explanation for the dual-nature of [`BitBoard`]
// There's both a `bitvec` impl (which is variable length)
// and a `native` impl which uses a u64 as the backing type
// the `native` impl is ~15-25% faster (in non-BitBoard specific benchmarks)
// `bitvec` is only really useful if you're using esoteric board sizes
#[cfg(feature = "bitvec")]
use bitvec::prelude::*;
#[cfg(feature = "bitvec")]
type BBBaseType = u64;
#[cfg(feature = "bitvec")]
pub type BitBoardInner = BitArr!(for Board::BOARD_AREA, in BBBaseType, Lsb0);
#[cfg(not(feature = "bitvec"))]
pub type BitBoardInner = u64;
@@ -30,55 +15,133 @@ pub struct BitBoard(BitBoardInner);
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= Board::BOARD_AREA as usize);
impl BitBoard {
#[cfg(feature = "bitvec")]
#[allow(clippy::new_without_default)]
pub const fn new() -> Self {
Self(bitarr!(BBBaseType, Lsb0; 0; Board::BOARD_AREA))
}
#[cfg(not(feature = "bitvec"))]
#[allow(clippy::new_without_default)]
pub const fn new() -> Self {
Self(0)
}
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn get(&self, coord: CoordPair) -> bool {
self.get_by_index(coord.0)
}
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn set(&mut self, coord: CoordPair, value: bool) {
self.set_by_index(coord.0, value);
}
#[cfg(not(feature = "bitvec"))]
const fn get_by_index(&self, index: CoordPairInner) -> bool {
((self.0 >> index) & 0b1) != 0b0
}
#[cfg(not(feature = "bitvec"))]
const fn set_by_index(&mut self, index: CoordPairInner, value: bool) {
// PERF! branchless setting of bit (~+3% perf bump)
self.0 &= !(0b1 << index); // clear bit
self.0 |= (value as BitBoardInner) << index; // set bit (if needed)
}
#[cfg(feature = "bitvec")]
pub fn get_by_index(&self, index: CoordPairInner) -> bool {
self.0[index as usize]
}
#[cfg(feature = "bitvec")]
pub fn set_by_index(&mut self, index: CoordPairInner, value: bool) {
self.0.set(index as usize, value);
}
// works on both `bitvec` and native (const on native)
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn count(&self) -> usize {
self.0.count_ones() as usize
}
// Directional shifts with edge masking (prevents wrapping)
pub const fn east(&self) -> Self {
let mask = !Self::col_mask(Board::BOARD_SIZE).0; // Mask to block column 7 bits
Self((self.0 & mask) << 1)
}
pub const fn west(&self) -> Self {
let mask = !Self::col_mask(0).0;
Self((self.0 & mask) >> 1)
}
pub const fn north(&self) -> Self {
Self(self.0 >> Board::BOARD_SIZE)
}
pub const fn south(&self) -> Self {
Self(self.0 << Board::BOARD_SIZE)
}
pub const fn northeast(&self) -> Self {
self.north().east()
}
pub const fn northwest(&self) -> Self {
self.north().west()
}
pub const fn southeast(&self) -> Self {
self.south().east()
}
pub const fn southwest(&self) -> Self {
self.south().west()
}
// Mask for a specific column (e.g., col_mask(7) = 0x8080808080808080)
const fn col_mask(col: CoordAxis) -> Self {
let mut mask = 0;
let mut i = 0;
while i < Board::BOARD_AREA {
mask |= 1 << (i + col);
i += Board::BOARD_SIZE;
}
Self(mask)
}
// Check if a BitBoard contains a coordinate
pub const fn contains(&self, coord: CoordPair) -> bool {
(self.0 & (1 << coord.0)) != 0
}
// Create a BitBoard from a single coordinate
pub const fn from_coord(coord: CoordPair) -> Self {
Self(1 << coord.0)
}
pub fn intersects(self, other: Self) -> bool {
(self & other).count() > 0
}
pub fn union(self, other: Self) -> Self {
self | other
}
}
impl std::ops::Not for BitBoard {
type Output = BitBoard;
fn not(self) -> Self::Output {
Self(!self.0)
}
}
impl std::ops::BitAnd for BitBoard {
type Output = BitBoard;
fn bitand(self, rhs: Self) -> Self::Output {
Self(self.0 & rhs.0)
}
}
impl std::ops::BitOr for BitBoard {
type Output = BitBoard;
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl std::ops::BitAndAssign for BitBoard {
fn bitand_assign(&mut self, rhs: Self) {
*self = *self & rhs;
}
}
impl std::ops::BitOrAssign for BitBoard {
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs;
}
}
#[cfg(test)]