Skip to content

Commit

Permalink
Object serialization micro-optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Jan 14, 2025
1 parent 8c5c106 commit 86979cc
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 57 deletions.
12 changes: 1 addition & 11 deletions src/Variant/AbstractPiece.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Chess\Variant\Classical\PGN\AN\Castle;
use Chess\Variant\Classical\PGN\AN\Color;
use Chess\Variant\Classical\PGN\AN\Piece;
use Chess\Variant\Classical\PGN\AN\Square;
use Chess\Variant\Classical\Rule\CastlingRule;

abstract class AbstractPiece
Expand All @@ -29,13 +28,6 @@ abstract class AbstractPiece
*/
public string $sq;

/**
* Square.
*
* @var \Chess\Variant\Classical\PGN\AN\Square
*/
public Square $square;

/**
* Identifier.
*
Expand Down Expand Up @@ -67,14 +59,12 @@ abstract class AbstractPiece
/**
* @param string $color
* @param string $sq
* @param \Chess\Variant\Classical\PGN\AN\Square $square
* @param string $id
*/
public function __construct(string $color, string $sq, Square $square, string $id)
public function __construct(string $color, string $sq, string $id)
{
$this->color = $color;
$this->sq = $sq;
$this->square = $square;
$this->id = $id;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Variant/Capablanca/Piece/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class A extends AbstractLinePiece

public function __construct(string $color, string $sq, Square $square)
{
parent::__construct($color, $sq, $square, Piece::A);
parent::__construct($color, $sq, Piece::A);

$this->mobility = [
...(new B($color, $sq, $square))->mobility,
Expand Down
2 changes: 1 addition & 1 deletion src/Variant/Capablanca/Piece/C.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class C extends AbstractLinePiece

public function __construct(string $color, string $sq, Square $square)
{
parent::__construct($color, $sq, $square, Piece::C);
parent::__construct($color, $sq, Piece::C);

$this->mobility = [
...(new R($color, $sq, $square, RType::R))->mobility,
Expand Down
8 changes: 4 additions & 4 deletions src/Variant/Classical/Piece/B.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class B extends AbstractLinePiece
{
public function __construct(string $color, string $sq, Square $square)
{
parent::__construct($color, $sq, $square, Piece::B);
parent::__construct($color, $sq, Piece::B);

$this->mobility = [
0 => [],
Expand All @@ -21,15 +21,15 @@ public function __construct(string $color, string $sq, Square $square)

$file = ord($this->file()) - 1;
$rank = $this->rank() + 1;
while ($file >= 97 && $rank <= $this->square::SIZE['ranks']) {
while ($file >= 97 && $rank <= $square::SIZE['ranks']) {
$this->mobility[0][] = chr($file) . $rank;
$file -= 1;
$rank += 1;
}

$file = ord($this->file()) + 1;
$rank = $this->rank() + 1;
while ($file <= 97 + $this->square::SIZE['files'] - 1 && $rank <= $this->square::SIZE['ranks']) {
while ($file <= 97 + $square::SIZE['files'] - 1 && $rank <= $square::SIZE['ranks']) {
$this->mobility[1][] = chr($file) . $rank;
$file += 1;
$rank += 1;
Expand All @@ -45,7 +45,7 @@ public function __construct(string $color, string $sq, Square $square)

$file = ord($this->file()) + 1;
$rank = $this->rank() - 1;
while ($file <= 97 + $this->square::SIZE['files'] - 1 && $rank >= 1) {
while ($file <= 97 + $square::SIZE['files'] - 1 && $rank >= 1) {
$this->mobility[3][] = chr($file) . $rank;
$file += 1;
$rank -= 1;
Expand Down
16 changes: 8 additions & 8 deletions src/Variant/Classical/Piece/K.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class K extends AbstractPiece
{
public function __construct(string $color, string $sq, Square $square)
{
parent::__construct($color, $sq, $square, Piece::K);
parent::__construct($color, $sq, Piece::K);

$this->mobility = [];

$rank = $this->rank() + 1;
if ($rank <= $this->square::SIZE['ranks']) {
if ($rank <= $square::SIZE['ranks']) {
$this->mobility[] = $this->file() . $rank;
}

Expand All @@ -33,19 +33,19 @@ public function __construct(string $color, string $sq, Square $square)
}

$file = ord($this->file()) + 1;
if ($file <= 97 + $this->square::SIZE['files'] - 1) {
if ($file <= 97 + $square::SIZE['files'] - 1) {
$this->mobility[] = chr($file) . $this->rank();
}

$file = ord($this->file()) - 1;
$rank = $this->rank() + 1;
if ($file >= 97 && $rank <= $this->square::SIZE['ranks']) {
if ($file >= 97 && $rank <= $square::SIZE['ranks']) {
$this->mobility[] = chr($file) . $rank;
}

$file = ord($this->file()) + 1;
$rank = $this->rank() + 1;
if ($file <= 97 + $this->square::SIZE['files'] - 1 && $rank <= $this->square::SIZE['ranks']) {
if ($file <= 97 + $square::SIZE['files'] - 1 && $rank <= $square::SIZE['ranks']) {
$this->mobility[] = chr($file) . $rank;
}

Expand All @@ -57,7 +57,7 @@ public function __construct(string $color, string $sq, Square $square)

$file = ord($this->file()) + 1;
$rank = $this->rank() - 1;
if ($file <= 97 + $this->square::SIZE['files'] - 1 && $rank >= 1) {
if ($file <= 97 + $square::SIZE['files'] - 1 && $rank >= 1) {
$this->mobility[] = chr($file) . $rank;
}
}
Expand Down Expand Up @@ -169,15 +169,15 @@ public function castle(string $rookType): bool
new K(
$this->color,
$this->board->castlingRule->rule[$this->color][Piece::K][rtrim($this->move['pgn'], '+')]['to'],
$this->square
$this->board->square
)
);
$this->board->detach($rook);
$this->board->attach(
new R(
$rook->color,
$this->board->castlingRule->rule[$this->color][Piece::R][rtrim($this->move['pgn'], '+')]['to'],
$this->square,
$this->board->square,
$rook->type
)
);
Expand Down
18 changes: 9 additions & 9 deletions src/Variant/Classical/Piece/N.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class N extends AbstractPiece
{
public function __construct(string $color, string $sq, Square $square)
{
parent::__construct($color, $sq, $square, Piece::N);
parent::__construct($color, $sq, Piece::N);

$this->mobility = [];

try {
$file = chr(ord($this->file()) - 1);
$rank = $this->rank() + 2;
if ($this->square->validate($file . $rank)) {
if ($square->validate($file . $rank)) {
$this->mobility[] = $file . $rank;
}
} catch (UnknownNotationException $e) {
Expand All @@ -27,7 +27,7 @@ public function __construct(string $color, string $sq, Square $square)
try {
$file = chr(ord($this->file()) - 2);
$rank = $this->rank() + 1;
if ($this->square->validate($file . $rank)) {
if ($square->validate($file . $rank)) {
$this->mobility[] = $file . $rank;
}
} catch (UnknownNotationException $e) {
Expand All @@ -36,7 +36,7 @@ public function __construct(string $color, string $sq, Square $square)
try {
$file = chr(ord($this->file()) - 2);
$rank = $this->rank() - 1;
if ($this->square->validate($file . $rank)) {
if ($square->validate($file . $rank)) {
$this->mobility[] = $file . $rank;
}
} catch (UnknownNotationException $e) {
Expand All @@ -45,7 +45,7 @@ public function __construct(string $color, string $sq, Square $square)
try {
$file = chr(ord($this->file()) - 1);
$rank = $this->rank() - 2;
if ($this->square->validate($file . $rank)) {
if ($square->validate($file . $rank)) {
$this->mobility[] = $file . $rank;
}
} catch (UnknownNotationException $e) {
Expand All @@ -54,7 +54,7 @@ public function __construct(string $color, string $sq, Square $square)
try {
$file = chr(ord($this->file()) + 1);
$rank = $this->rank() - 2;
if ($this->square->validate($file . $rank)) {
if ($square->validate($file . $rank)) {
$this->mobility[] = $file . $rank;
}
} catch (UnknownNotationException $e) {
Expand All @@ -63,7 +63,7 @@ public function __construct(string $color, string $sq, Square $square)
try {
$file = chr(ord($this->file()) + 2);
$rank = $this->rank() - 1;
if ($this->square->validate($file . $rank)) {
if ($square->validate($file . $rank)) {
$this->mobility[] = $file . $rank;
}
} catch (UnknownNotationException $e) {
Expand All @@ -72,7 +72,7 @@ public function __construct(string $color, string $sq, Square $square)
try {
$file = chr(ord($this->file()) + 2);
$rank = $this->rank() + 1;
if ($this->square->validate($file . $rank)) {
if ($square->validate($file . $rank)) {
$this->mobility[] = $file . $rank;
}
} catch (UnknownNotationException $e) {
Expand All @@ -81,7 +81,7 @@ public function __construct(string $color, string $sq, Square $square)
try {
$file = chr(ord($this->file()) + 1);
$rank = $this->rank() + 2;
if ($this->square->validate($file . $rank)) {
if ($square->validate($file . $rank)) {
$this->mobility[] = $file . $rank;
}
} catch (UnknownNotationException $e) {
Expand Down
20 changes: 10 additions & 10 deletions src/Variant/Classical/Piece/P.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ class P extends AbstractPiece

public function __construct(string $color, string $sq, Square $square)
{
parent::__construct($color, $sq, $square, Piece::P);
parent::__construct($color, $sq, Piece::P);

$this->ranks = $this->color === Color::W
? [
'start' => 2,
'next' => $this->rank() + 1,
'end' => $this->square::SIZE['ranks'],
'end' => $square::SIZE['ranks'],
]
: [
'start' => $this->square::SIZE['ranks'] - 1,
'start' => $square::SIZE['ranks'] - 1,
'next' => $this->rank() - 1,
'end' => 1,
];
Expand All @@ -36,29 +36,29 @@ public function __construct(string $color, string $sq, Square $square)
$this->mobility = [];

// next rank
if ($this->ranks['next'] <= $this->square::SIZE['ranks']) {
if ($this->ranks['next'] <= $square::SIZE['ranks']) {
$this->mobility[] = $this->file() . $this->ranks['next'];
}

// two square advance
if ($this->rank() === 2 && $this->ranks['start'] == 2) {
$this->mobility[] = $this->file() . ($this->ranks['start'] + 2);
} elseif ($this->rank() === $this->square::SIZE['ranks'] - 1 &&
$this->ranks['start'] == $this->square::SIZE['ranks'] - 1
} elseif ($this->rank() === $square::SIZE['ranks'] - 1 &&
$this->ranks['start'] == $square::SIZE['ranks'] - 1
) {
$this->mobility[] = $this->file() . ($this->ranks['start'] - 2);
}

// capture square
$file = ord($this->file()) - 1;
if ($file >= 97 && $this->ranks['next'] <= $this->square::SIZE['ranks']) {
if ($file >= 97 && $this->ranks['next'] <= $square::SIZE['ranks']) {
$this->captureSqs[] = chr($file) . $this->ranks['next'];
}

// capture square
$file = ord($this->file()) + 1;
if ($file <= 97 + $this->square::SIZE['files'] - 1 &&
$this->ranks['next'] <= $this->square::SIZE['ranks']
if ($file <= 97 + $square::SIZE['files'] - 1 &&
$this->ranks['next'] <= $square::SIZE['ranks']
) {
$this->captureSqs[] = chr($file) . $this->ranks['next'];
}
Expand Down Expand Up @@ -89,7 +89,7 @@ public function moveSqs(): array
$end = end($history);
if ($end && $end['id'] === Piece::P && $end['color'] === $this->oppColor()) {
if ($this->color === Color::W) {
if ($this->rank() === $this->square::SIZE['ranks'] - 3) {
if ($this->rank() === $this->board->square::SIZE['ranks'] - 3) {
$captureSq = $end['to'][0] . ($this->rank() + 1);
if (in_array($captureSq, $this->captureSqs)) {
$this->enPassantSq = $captureSq;
Expand Down
2 changes: 1 addition & 1 deletion src/Variant/Classical/Piece/Q.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Q extends AbstractLinePiece
{
public function __construct(string $color, string $sq, Square $square)
{
parent::__construct($color, $sq, $square, Piece::Q);
parent::__construct($color, $sq, Piece::Q);

$this->mobility = [
...(new R($color, $sq, $square, RType::R))->mobility,
Expand Down
6 changes: 3 additions & 3 deletions src/Variant/Classical/Piece/R.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class R extends AbstractLinePiece

public function __construct(string $color, string $sq, Square $square, string $type)
{
parent::__construct($color, $sq, $square, Piece::R);
parent::__construct($color, $sq, Piece::R);

$this->type = $type;

Expand All @@ -23,7 +23,7 @@ public function __construct(string $color, string $sq, Square $square, string $t
3 => [],
];

for ($i = $this->rank() + 1; $i <= $this->square::SIZE['ranks']; $i++) {
for ($i = $this->rank() + 1; $i <= $square::SIZE['ranks']; $i++) {
$this->mobility[0][] = $this->file() . $i;
}

Expand All @@ -35,7 +35,7 @@ public function __construct(string $color, string $sq, Square $square, string $t
$this->mobility[2][] = chr($i) . $this->rank();
}

for ($i = ord($this->file()) + 1; $i <= 97 + $this->square::SIZE['files'] - 1; $i++) {
for ($i = ord($this->file()) + 1; $i <= 97 + $square::SIZE['files'] - 1; $i++) {
$this->mobility[3][] = chr($i) . $this->rank();
}
}
Expand Down
Loading

0 comments on commit 86979cc

Please sign in to comment.