Skip to content

Commit

Permalink
reduce binary size
Browse files Browse the repository at this point in the history
  • Loading branch information
brunocodutra committed Nov 2, 2024
1 parent 37e644d commit 8c64fdd
Show file tree
Hide file tree
Showing 45 changed files with 454 additions and 445 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ opt-level = 's'

[profile.release]
codegen-units = 1
lto = true
panic = "abort"
strip = "symbols"
lto = true
strip = true

[profile.dev]
opt-level = 3
Expand Down
30 changes: 10 additions & 20 deletions bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
use futures::executor::{block_on, block_on_stream};
use futures::{channel::mpsc, prelude::*};
use futures::executor::block_on;
use futures::{channel::mpsc::channel as bounded, prelude::*, sink::unfold};
use lib::uci::Uci;
use std::io::{prelude::*, stdin, stdout, LineWriter};
use std::thread;
use std::io::{prelude::*, stdin, stdout};
use std::{future::ready, thread};

fn main() {
let (mut tx, input) = mpsc::channel(32);
let (output, rx) = mpsc::channel(32);
let (mut tx, rx) = bounded(0);

Check warning on line 8 in bin/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/main.rs#L8

Added line #L8 was not covered by tests

thread::spawn(move || {
for item in stdin().lock().lines() {
match item {
Err(error) => return eprint!("{error}"),
Ok(line) => {
if let Err(error) = block_on(tx.send(line)) {
if error.is_disconnected() {
break;
}
if block_on(tx.send(line)).is_err() {
break;

Check warning on line 16 in bin/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/main.rs#L15-L16

Added lines #L15 - L16 were not covered by tests
}
}
}
}
});

thread::spawn(move || {
let mut stdout = LineWriter::new(stdout().lock());
for line in block_on_stream(rx) {
if let Err(error) = writeln!(stdout, "{line}") {
return eprint!("{error}");
}
}
});

block_on(Uci::new(input, output).run()).ok();
let mut stdout = stdout().lock();
let output = unfold((), |_, line: String| ready(writeln!(stdout, "{line}")));
block_on(Uci::new(rx, output).run()).unwrap();

Check warning on line 25 in bin/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/main.rs#L23-L25

Added lines #L23 - L25 were not covered by tests
}
58 changes: 29 additions & 29 deletions lib/chess/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::{cell::SyncUnsafeCell, mem::MaybeUninit};
#[repr(transparent)]
pub struct Bitboard(u64);

impl fmt::Debug for Bitboard {
impl Debug for Bitboard {
#[coverage(off)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_char('\n')?;
Expand All @@ -44,31 +44,31 @@ impl fmt::Debug for Bitboard {

impl Bitboard {
/// An empty board.
#[inline(always)]
#[inline]
pub fn empty() -> Self {
Bitboard(0)
}

/// A full board.
#[inline(always)]
#[inline]
pub fn full() -> Self {
Bitboard(0xFFFFFFFFFFFFFFFF)
}

/// Border squares.
#[inline(always)]
#[inline]
pub fn border() -> Self {
Bitboard(0xFF818181818181FF)
}

/// Light squares.
#[inline(always)]
#[inline]
pub fn light() -> Self {
Bitboard(0x55AA55AA55AA55AA)
}

/// Dark squares.
#[inline(always)]
#[inline]
pub fn dark() -> Self {
Bitboard(0xAA55AA55AA55AA55)
}
Expand All @@ -86,7 +86,7 @@ impl Bitboard {
/// vec![Square::F1, Square::E2, Square::D4, Square::C6]
/// );
/// ```
#[inline(always)]
#[inline]
pub fn fill(sq: Square, steps: &[(i8, i8)], occupied: Bitboard) -> Self {
let mut bitboard = sq.bitboard();

Expand Down Expand Up @@ -117,7 +117,7 @@ impl Bitboard {
/// vec![Square::E1, Square::D2, Square::C3, Square::B4, Square::A5]
/// );
/// ```
#[inline(always)]
#[inline]
pub fn line(whence: Square, whither: Square) -> Self {
static LINES: SyncUnsafeCell<[[Bitboard; 64]; 64]> =
unsafe { MaybeUninit::zeroed().assume_init() };
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Bitboard {
/// vec![Square::D2, Square::C3]
/// );
/// ```
#[inline(always)]
#[inline]
pub fn segment(whence: Square, whither: Square) -> Self {
static SEGMENTS: SyncUnsafeCell<[[Bitboard; 64]; 64]> =
unsafe { MaybeUninit::zeroed().assume_init() };
Expand Down Expand Up @@ -190,90 +190,90 @@ impl Bitboard {
}

/// The number of [`Square`]s in the set.
#[inline(always)]
#[inline]
pub fn len(&self) -> usize {
self.0.count_ones() as _
}

/// Whether the board is empty.
#[inline(always)]
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Whether this [`Square`] is in the set.
#[inline(always)]
#[inline]
pub fn contains(&self, sq: Square) -> bool {
!sq.bitboard().intersection(*self).is_empty()
}

/// Adds a [`Square`] to this bitboard.
#[inline(always)]
#[inline]
pub fn with(&self, sq: Square) -> Self {
sq.bitboard().union(*self)
}

/// Removes a [`Square`]s from this bitboard.
#[inline(always)]
#[inline]
pub fn without(&self, sq: Square) -> Self {
sq.bitboard().inverse().intersection(*self)
}

/// The set of [`Square`]s not in this bitboard.
#[inline(always)]
#[inline]
pub fn inverse(&self) -> Self {
Bitboard(!self.0)
}

/// The set of [`Square`]s in both bitboards.
#[inline(always)]
#[inline]
pub fn intersection(&self, bb: Bitboard) -> Self {
Bitboard(self.0 & bb.0)
}

/// The set of [`Square`]s in either bitboard.
#[inline(always)]
#[inline]
pub fn union(&self, bb: Bitboard) -> Self {
Bitboard(self.0 | bb.0)
}

/// An iterator over the [`Square`]s in this bitboard.
#[inline(always)]
#[inline]
pub fn iter(&self) -> Squares {
Squares::new(*self)
}

/// An iterator over the subsets of this bitboard.
#[inline(always)]
#[inline]
pub fn subsets(&self) -> Subsets {
Subsets::new(*self)
}
}

impl Perspective for Bitboard {
/// Flips all squares in the set.
#[inline(always)]
#[inline]
fn flip(&self) -> Self {
Self(self.0.swap_bytes())
}
}

impl From<File> for Bitboard {
#[inline(always)]
#[inline]
fn from(f: File) -> Self {
f.bitboard()
}
}

impl From<Rank> for Bitboard {
#[inline(always)]
#[inline]
fn from(r: Rank) -> Self {
r.bitboard()
}
}

impl From<Square> for Bitboard {
#[inline(always)]
#[inline]
fn from(sq: Square) -> Self {
sq.bitboard()
}
Expand All @@ -283,7 +283,7 @@ impl IntoIterator for Bitboard {
type Item = Square;
type IntoIter = Squares;

#[inline(always)]
#[inline]
fn into_iter(self) -> Self::IntoIter {
Squares::new(self)
}
Expand All @@ -296,7 +296,7 @@ pub struct Squares(Bitboard);
impl Iterator for Squares {
type Item = Square;

#[inline(always)]
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
None
Expand All @@ -307,14 +307,14 @@ impl Iterator for Squares {
}
}

#[inline(always)]
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
}

impl ExactSizeIterator for Squares {
#[inline(always)]
#[inline]
fn len(&self) -> usize {
self.0.len()
}
Expand All @@ -325,7 +325,7 @@ impl ExactSizeIterator for Squares {
pub struct Subsets(u64, Option<u64>);

impl Subsets {
#[inline(always)]
#[inline]
pub fn new(bb: Bitboard) -> Self {
Self(bb.0, Some(0))
}
Expand All @@ -334,7 +334,7 @@ impl Subsets {
impl Iterator for Subsets {
type Item = Bitboard;

#[inline(always)]
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let bits = self.1?;
self.1 = match bits.wrapping_sub(self.0) & self.0 {
Expand Down
Loading

0 comments on commit 8c64fdd

Please sign in to comment.