From ee5d46e292873191a80f7186e94f83ae79c498ab Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Sun, 9 Jul 2023 09:53:38 +0200 Subject: [PATCH 1/5] optimize Divider.mixedness --- src/main/scala/Divider.scala | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/src/main/scala/Divider.scala b/src/main/scala/Divider.scala index d8e151959..a4d1eb9bc 100644 --- a/src/main/scala/Divider.scala +++ b/src/main/scala/Divider.scala @@ -2,6 +2,7 @@ package chess import cats.syntax.all.* import bitboard.Bitboard +import bitboard.Bitboard.bb import scala.annotation.switch case class Division(middle: Option[Ply], end: Option[Ply], plies: Ply): @@ -50,7 +51,7 @@ object Divider: (Bitboard.firstRank & board.white).count < 4 || (Bitboard.lastRank & board.black).count < 4 - private def score(white: Int, black: Int, y: Int): Int = + private def score(y: Int)(white: Int, black: Int): Int = ((white, black): @switch) match case (0, 0) => 0 @@ -76,26 +77,11 @@ object Divider: case _ => 0 - private val mixednessRegions: List[List[Square]] = { + private def mixedness(board: Board): Int = { + val smallSquare = 0x0303L.bb for - y <- Rank.all.take(7) - x <- File.all.take(7) - yield { - for - dy <- 0 to 1 - dx <- 0 to 1 - file <- x.offset(dx) - rank <- y.offset(dy) - yield Square(file, rank) - }.toList - }.toList - - private def mixedness(board: Board): Int = - mixednessRegions.foldLeft(0): (mix, region) => - var white = 0 - var black = 0 - region.foreach: s => - board(s).foreach: v => - if v is White then white = white + 1 - else black = black + 1 - mix + score(white, black, region.head.rank.index + 1) + y <- 0 to 6 + x <- 0 to 6 + region = smallSquare << (x + 8 * y) + yield board.byColor.map(c => (c & region).count).reduce(score(y + 1)) + }.sum From 78f070419e1017f491955e3afc1540e1454a8254 Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Sun, 9 Jul 2023 11:10:40 +0200 Subject: [PATCH 2/5] precompute mixednessRegions again --- src/main/scala/Divider.scala | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/scala/Divider.scala b/src/main/scala/Divider.scala index a4d1eb9bc..b6a54ff4c 100644 --- a/src/main/scala/Divider.scala +++ b/src/main/scala/Divider.scala @@ -77,11 +77,16 @@ object Divider: case _ => 0 - private def mixedness(board: Board): Int = { + private val mixednessRegions: List[Bitboard] = { val smallSquare = 0x0303L.bb for y <- 0 to 6 x <- 0 to 6 region = smallSquare << (x + 8 * y) - yield board.byColor.map(c => (c & region).count).reduce(score(y + 1)) - }.sum + yield region + }.toList + + private def mixedness(board: Board): Int = + mixednessRegions.foldLeft(0): (acc, region) => + val y = region.first.get.rank.index + 1 + acc + board.byColor.map(c => (c & region).count).reduce(score(y)) From 62df6002da643f62cadf8904ce37e04eabbecf82 Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Sun, 9 Jul 2023 11:25:11 +0200 Subject: [PATCH 3/5] attach y to each mixednessRegion --- src/main/scala/Divider.scala | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/scala/Divider.scala b/src/main/scala/Divider.scala index b6a54ff4c..e53eca851 100644 --- a/src/main/scala/Divider.scala +++ b/src/main/scala/Divider.scala @@ -77,16 +77,15 @@ object Divider: case _ => 0 - private val mixednessRegions: List[Bitboard] = { + private val mixednessRegions: List[(Bitboard, Int)] = { val smallSquare = 0x0303L.bb for y <- 0 to 6 x <- 0 to 6 - region = smallSquare << (x + 8 * y) - yield region + yield (smallSquare << (x + 8 * y), y + 1) }.toList private def mixedness(board: Board): Int = - mixednessRegions.foldLeft(0): (acc, region) => - val y = region.first.get.rank.index + 1 - acc + board.byColor.map(c => (c & region).count).reduce(score(y)) + mixednessRegions.foldLeft(0): + case (acc, (region, y)) => + acc + board.byColor.map(c => (c & region).count).reduce(score(y)) From e21527aefccff5bc0e55a1f36860a1eff0d13b35 Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Sun, 9 Jul 2023 11:36:30 +0200 Subject: [PATCH 4/5] use ByColor.mapReduce --- src/main/scala/Divider.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/Divider.scala b/src/main/scala/Divider.scala index e53eca851..1608bcdd8 100644 --- a/src/main/scala/Divider.scala +++ b/src/main/scala/Divider.scala @@ -88,4 +88,4 @@ object Divider: private def mixedness(board: Board): Int = mixednessRegions.foldLeft(0): case (acc, (region, y)) => - acc + board.byColor.map(c => (c & region).count).reduce(score(y)) + acc + board.byColor.mapReduce(c => (c & region).count)(score(y)) From aef101c5e7f0b3a285ac27bae15128159827c26c Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Sun, 9 Jul 2023 11:45:42 +0200 Subject: [PATCH 5/5] directly use Long for region masks --- src/main/scala/Divider.scala | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/scala/Divider.scala b/src/main/scala/Divider.scala index 1608bcdd8..a107281d3 100644 --- a/src/main/scala/Divider.scala +++ b/src/main/scala/Divider.scala @@ -2,7 +2,6 @@ package chess import cats.syntax.all.* import bitboard.Bitboard -import bitboard.Bitboard.bb import scala.annotation.switch case class Division(middle: Option[Ply], end: Option[Ply], plies: Ply): @@ -77,8 +76,8 @@ object Divider: case _ => 0 - private val mixednessRegions: List[(Bitboard, Int)] = { - val smallSquare = 0x0303L.bb + private val mixednessRegions: List[(Long, Int)] = { + val smallSquare = 0x0303L for y <- 0 to 6 x <- 0 to 6