Skip to content

Commit

Permalink
🐛 Fix index out of range exception (#708)
Browse files Browse the repository at this point in the history
* Add a buffer to array size for those ones accessed by depth or ply
  • Loading branch information
eduherminio authored Apr 5, 2024
1 parent b3d6934 commit 431d769
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/Lynx/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ public static class Constants

public const int AbsoluteMaxDepth = 255;

/// <summary>
/// To take into account array access by depth and depth extensions
/// </summary>
public const int ArrayDepthMargin = 16;

/// <summary>
/// From https://lichess.org/RViT3UWL
/// Failes input parsing fail if default input buffer size is used (see Lynx.Cli.Listener
Expand Down
6 changes: 3 additions & 3 deletions src/Lynx/EvaluationConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ public static class EvaluationConstants
/// <summary>
/// <see cref="Constants.AbsoluteMaxDepth"/> x <see cref="Constants.MaxNumberOfPossibleMovesInAPosition"/>
/// </summary>
public static readonly int[][] LMRReductions = new int[Configuration.EngineSettings.MaxDepth][];
public static readonly int[][] LMRReductions = new int[Configuration.EngineSettings.MaxDepth + Constants.ArrayDepthMargin][];

/// <summary>
/// [0, 4, 136, 276, 424, 580, 744, 916, 1096, 1284, 1480, 1684, 1896, 1896, 1896, 1896, ...]
/// </summary>
public static readonly int[] HistoryBonus = new int[Configuration.EngineSettings.MaxDepth];
public static readonly int[] HistoryBonus = new int[Configuration.EngineSettings.MaxDepth + Constants.ArrayDepthMargin];

static EvaluationConstants()
{
Expand Down Expand Up @@ -262,7 +262,7 @@ static EvaluationConstants()
}
}

for (int searchDepth = 1; searchDepth < Configuration.EngineSettings.MaxDepth; ++searchDepth) // Depth > 0 or we'd be in QSearch
for (int searchDepth = 1; searchDepth < Configuration.EngineSettings.MaxDepth + Constants.ArrayDepthMargin; ++searchDepth) // Depth > 0 or we'd be in QSearch
{
LMRReductions[searchDepth] = new int[Constants.MaxNumberOfPossibleMovesInAPosition];

Expand Down
4 changes: 2 additions & 2 deletions src/Lynx/Model/PVTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class PVTable

private static ImmutableArray<int> Initialize()
{
var indexes = new int[Configuration.EngineSettings.MaxDepth + 1];
var indexes = new int[Configuration.EngineSettings.MaxDepth + Constants.ArrayDepthMargin];
int previousPVIndex = 0;
indexes[0] = previousPVIndex;

Expand All @@ -18,6 +18,6 @@ private static ImmutableArray<int> Initialize()
previousPVIndex = indexes[depth + 1];
}

return indexes.ToImmutableArray();
return [.. indexes];
}
}
8 changes: 4 additions & 4 deletions src/Lynx/Search/IDDFS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public sealed partial class Engine
/// </summary>
private readonly int[][] _killerMoves =
[
new int[Configuration.EngineSettings.MaxDepth],
new int[Configuration.EngineSettings.MaxDepth],
new int[Configuration.EngineSettings.MaxDepth]
new int[Configuration.EngineSettings.MaxDepth + Constants.ArrayDepthMargin],
new int[Configuration.EngineSettings.MaxDepth + Constants.ArrayDepthMargin],
new int[Configuration.EngineSettings.MaxDepth + Constants.ArrayDepthMargin]
];

/// <summary>
Expand All @@ -31,7 +31,7 @@ public sealed partial class Engine
/// </summary>
private readonly int[][][] _captureHistory;

private readonly int[] _maxDepthReached = new int[Configuration.EngineSettings.MaxDepth];
private readonly int[] _maxDepthReached = new int[Configuration.EngineSettings.MaxDepth + Constants.ArrayDepthMargin];
private TranspositionTable _tt = [];
private int _ttMask;

Expand Down

0 comments on commit 431d769

Please sign in to comment.