Skip to content

Commit

Permalink
Improve copy performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
onspli committed Sep 17, 2021
1 parent a424d80 commit 85724ee
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 50 deletions.
28 changes: 4 additions & 24 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
|--------|-------------|
| [**Board**](#Board) | |
| [Board::__construct](#Board__construct) | Load piece placement or setup initial position. |
| [Board::copy](#Boardcopy) | Creates deep copy of the board instance. |
| [Board::__toString](#Board__toString) | |
| [Board::export](#Boardexport) | Export piece placement string. |
| [Board::preview](#Boardpreview) | Preview of the board in ASCII graphics. |
Expand All @@ -24,7 +23,7 @@
| [Board::is_check](#Boardis_check) | Tells whether the king of color specified is in check. |
| [**FEN**](#FEN) | FEN is a standard notation for describing a particular board position of a chess game |
| [FEN::__construct](#FEN__construct) | Load FEN or setup starting position. |
| [FEN::copy](#FENcopy) | Creates deep copy of the FEN instance. |
| [FEN::__clone](#FEN__clone) | |
| [FEN::export](#FENexport) | Export whole FEN string. |
| [FEN::export_short](#FENexport_short) | Export FEN string without halfmoves count and fullmove number. |
| [FEN::__toString](#FEN__toString) | |
Expand Down Expand Up @@ -146,25 +145,6 @@ digits 1 through 8 (the number of empty squares), and "/" separates ranks.



---
### Board::copy

Creates deep copy of the board instance.

```php
Board::copy( ): mixed
```





**Return Value:**





---
### Board::__toString

Expand Down Expand Up @@ -582,12 +562,12 @@ FEN::__construct( string fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQ


---
### FEN::copy
### FEN::__clone


Creates deep copy of the FEN instance.

```php
FEN::copy( ): mixed
FEN::__clone( ): mixed
```


Expand Down
8 changes: 0 additions & 8 deletions src/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ function __construct(string $pieces = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKB
}
}

/**
* Creates deep copy of the board instance.
*/
public function copy()
{
return new self($this->export());
}

private function split_to_ranks(string $pieces) : array
{
$pieces = preg_replace('/\s+/', '', $pieces);
Expand Down
20 changes: 9 additions & 11 deletions src/FEN.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ function __construct(string $fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
$this->set_fullmove($parts[5] ?? 1);
}

/**
* Creates deep copy of the FEN instance.
*/
public function copy()
{
return new self($this->export());
}
public function __clone()
{
$this->board = clone $this->board;
$this->en_passant = clone $this->en_passant;
}

/**
* Export whole FEN string.
Expand Down Expand Up @@ -541,7 +539,7 @@ public function get_legal_moves() : array
*/
public function is_legal_move(string $move) : bool
{
$fen = $this->copy();
$fen = clone $this;
try {
$fen->move($move);
} catch (\Exception $e) {
Expand Down Expand Up @@ -575,7 +573,7 @@ private function castle_kingside(Move &$move) : void
throw new RulesException("Castling not available. King in check.");
}

$new_board = $this->board->copy();
$new_board = clone $this->board;
$new_board->set_square($origin, '');
$new_board->set_square($origin->get_relative_square(1, 0), $this->get_active_piece('K'));
if ($new_board->is_check($this->get_active_color())) {
Expand Down Expand Up @@ -615,7 +613,7 @@ private function castle_queenside(Move &$move) : void
throw new RulesException("Castling not available. King in check.");
}

$new_board = $this->board->copy();
$new_board = clone $this->board;
$new_board->set_square($origin, '');
$new_board->set_square($origin->get_relative_square(-1, 0), $this->get_active_piece('K'));
if ($new_board->is_check($this->get_active_color())) {
Expand Down Expand Up @@ -734,7 +732,7 @@ private function standard_move(Move &$move) : void

private function get_new_board(Move $move, Square $origin) : Board
{
$new_board = $this->board->copy();
$new_board = clone $this->board;
$piece = $this->get_active_piece($move->get_piece_type());
$target = $move->get_target(true);

Expand Down
8 changes: 4 additions & 4 deletions src/PGN.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function get_tags() : array
public function get_initial_fen(bool $as_object = false)
{
if ($as_object) {
return $this->initial_fen->copy();
return clone $this->initial_fen;
}
return $this->initial_fen->export();
}
Expand Down Expand Up @@ -196,7 +196,7 @@ public function get_fen_after_halfmove(int $halfmove_number, bool $as_object = f
}
$fen = $this->fens[$halfmove_index];
if ($as_object) {
return $fen->copy();
return clone $fen;
}
return $fen->export();
}
Expand All @@ -223,7 +223,7 @@ private function get_last_computed_fen() : FEN
if ($computed == 0) {
return $this->get_initial_fen(true);
}
return $this->fens[$computed - 1]->copy();
return clone $this->fens[$computed - 1];
}

private function compute_fens(int $max_halfmove_to_compute) : void
Expand All @@ -239,7 +239,7 @@ private function compute_fens(int $max_halfmove_to_compute) : void
} catch (\Exception $e) {
throw new \Exception('Move ' . ceil($halfmove_number / 2) . ($halfmove_number % 2 ? '. ' : '... ') . $halfmove . ' is invalid. FEN ' . $this->get_fen_after_halfmove($halfmove_number - 1), 0, $e);
}
$this->fens[] = $fen->copy();
$this->fens[] = clone $fen;
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/benchmark/BoardBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Onspli\Chess;

class BoardBench
{

/**
* @Revs(10)
*/
public function benchConstructor()
{
$board = new Board('rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR');
}

/**
* @Revs(10)
*/
public function benchCopy()
{
$board = new Board('rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR');
$board2 = clone $board;
}

}
2 changes: 1 addition & 1 deletion tests/benchmark/FENBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function benchConstructor()
public function benchCopy()
{
$fen = new FEN('rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR b KQq c6 1 2');
$fen->copy();
$fen2 = clone $fen;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/BoardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function testIsSquareVacant() : void
public function testCopy() : void
{
$board1 = new Board('rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR');
$board2 = $board1->copy();
$board2 = clone $board1;

$this->assertEquals($board1->export(), $board2->export());
$board2->set_square('a1', '');
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/FENTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public function testStringable() : void
public function testCopy() : void
{
$fen = new FEN;
$fen2 = $fen->copy();
$fen2 = clone $fen;
$fen->set_square('e4', 'Q');
$fen->set_fullmove(5);
$fen->set_en_passant('e3');
$this->assertEquals('', $fen2->get_square('e4'));
$this->assertEquals(1, $fen2->get_fullmove());
$this->assertEquals('-', $fen2->get_en_passant());
}

public function testSetFullmoveInvalid1() : void
Expand Down

0 comments on commit 85724ee

Please sign in to comment.