Skip to content

Commit a16900b

Browse files
committed
another small refactoring that does not affect search behavior but improves the speed, reduces LOC by 2 instructions and is easier to read
1 parent d4044a9 commit a16900b

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

MinimalChess/IterativeSearch.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private void StorePVinTT(Move[] pv, int depth)
102102

103103
//moves after the PV node are unlikely to raise alpha. skip those that appear clearly futile!
104104
int futilityMargin = (int)color * depth * MAX_GAIN_PER_PLY;
105-
if (expandedNodes > 1 && depth <= 4 && !isChecked && !window.Inside(child.Score + futilityMargin, color) && !child.IsChecked(child.SideToMove))
105+
if (expandedNodes > 1 && depth <= 4 && !isChecked && window.FailLow(child.Score + futilityMargin, color) && !child.IsChecked(child.SideToMove))
106106
continue;
107107

108108
//moves after the PV node are unlikely to raise alpha. searching with a null-sized window can save a lot of nodes
@@ -111,26 +111,26 @@ private void StorePVinTT(Move[] pv, int depth)
111111
//we can save a lot of nodes by searching with "null window" first, proving cheaply that the score is below alpha...
112112
SearchWindow nullWindow = window.GetLowerBound(color);
113113
var nullResult = EvalPositionTT(child, depth - 1, nullWindow);
114-
if (!nullWindow.Inside(nullResult.Score, color))
114+
if (nullWindow.FailLow(nullResult.Score, color))
115115
continue;
116116
}
117117

118118
//this node may raise alpha!
119119
var eval = EvalPositionTT(child, depth - 1, window);
120-
if (window.Inside(eval.Score, color))
120+
if (window.FailLow(eval.Score, color))
121+
continue;
122+
123+
Transpositions.Store(position.ZobristHash, depth, window, eval.Score, move);
124+
//store the PV beginning with move, followed by the PV of the childnode
125+
pv = Merge(move, eval.PV);
126+
//...and maybe get a beta cutoff
127+
if (window.Cut(eval.Score, color))
121128
{
122-
Transpositions.Store(position.ZobristHash, depth, window, eval.Score, move);
123-
//store the PV beginning with move, followed by the PV of the childnode
124-
pv = Merge(move, eval.PV);
125-
//...and maybe get a beta cutoff
126-
if (window.Cut(eval.Score, color))
127-
{
128-
//we remember killers like hat!
129-
if (position[move.ToSquare] == Piece.None)
130-
_killers.Add(move, depth);
131-
132-
return (window.GetScore(color), pv);
133-
}
129+
//we remember killers like hat!
130+
if (position[move.ToSquare] == Piece.None)
131+
_killers.Add(move, depth);
132+
133+
return (window.GetScore(color), pv);
134134
}
135135
}
136136

MinimalChess/SearchWindow.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public struct SearchWindow
66

77
public int Floor;//Alpha
88
public int Ceiling;//Beta
9-
public int Width => Ceiling - Floor - 1;
109

1110
public SearchWindow UpperBound => new SearchWindow(Ceiling - 1, Ceiling);
1211
public SearchWindow LowerBound => new SearchWindow(Floor, Floor + 1);
@@ -41,13 +40,9 @@ public bool Cut(int score, Color color)
4140
}
4241
}
4342

44-
public bool Inside(int score, Color color)
45-
{
46-
if (color == Color.White)
47-
return score > Floor;
48-
else
49-
return score < Ceiling;
50-
}
43+
public bool FailLow(int score, Color color) => color == Color.White ? (score <= Floor) : (score >= Ceiling);
44+
45+
//public bool FailHigh(int score, Color color) => color == Color.White ? (score >= Ceiling) : (score <= Floor);
5146

5247
public int GetScore(Color color) => color == Color.White ? Floor : Ceiling;
5348
}

0 commit comments

Comments
 (0)