Skip to content

Commit

Permalink
⚡ Avoid stackalloc local initialization when allocating it for move…
Browse files Browse the repository at this point in the history
…gen (#858)

Sprinkle `[SkipLocalsInit]` in the methods that stackalloc for movegen
  • Loading branch information
eduherminio authored Jul 21, 2024
1 parent 2cf3f1d commit 7fd04e9
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Lynx/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Lynx.UCI.Commands.GUI;
using NLog;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Channels;

Expand Down Expand Up @@ -151,6 +152,7 @@ public void NewGame()
#pragma warning restore S1215 // "GC.Collect" should not be called
}

[SkipLocalsInit]
public void AdjustPosition(ReadOnlySpan<char> rawPositionCommand)
{
Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
Expand Down
1 change: 1 addition & 0 deletions src/Lynx/Model/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ public static string ToEPDString(this Move move, Position position)
#pragma warning restore S3358 // Ternary operators should not be nested
}

[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string UCIString(this Move move)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Lynx/Perft.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Lynx.Model;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Lynx;

Expand Down Expand Up @@ -36,6 +37,7 @@ public static (long Nodes, double ElapsedMilliseconds) Divide(Position position,
/// <param name="depth"></param>
/// <param name="nodes"></param>
/// <returns></returns>
[SkipLocalsInit]
internal static long ResultsImpl(Position position, int depth, long nodes)
{
if (depth != 0)
Expand All @@ -58,6 +60,7 @@ internal static long ResultsImpl(Position position, int depth, long nodes)
return nodes + 1;
}

[SkipLocalsInit]
private static long DivideImpl(Position position, int depth, long nodes, Action<string> write)
{
if (depth != 0)
Expand Down
2 changes: 2 additions & 0 deletions src/Lynx/Search/IDDFS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public sealed partial class Engine
/// <param name="maxDepth"></param>
/// <param name="softLimitTimeBound"></param>
/// <returns>Not null <see cref="SearchResult"/>, although made nullable in order to match online tb probing signature</returns>
[SkipLocalsInit]
public SearchResult IDDFS(int maxDepth, int softLimitTimeBound)
{
// Cleanup
Expand Down Expand Up @@ -242,6 +243,7 @@ private bool StopSearchCondition(int depth, int maxDepth, int mate, int softLimi
return true;
}

[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool OnlyOneLegalMove(ref Move firstLegalMove, [NotNullWhen(true)] out SearchResult? result)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Lynx/Search/NegaMax.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lynx.Model;
using System.Runtime.CompilerServices;

namespace Lynx;

Expand All @@ -18,6 +19,7 @@ public sealed partial class Engine
/// Defaults to the worse possible score for Side to move's opponent, Int.MaxValue
/// </param>
/// <returns></returns>
[SkipLocalsInit]
private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullMove = false)
{
var position = Game.CurrentPosition;
Expand Down Expand Up @@ -505,6 +507,7 @@ private int NegaMax(int depth, int ply, int alpha, int beta, bool parentWasNullM
/// Defaults to the works possible score for Black, Int.MaxValue
/// </param>
/// <returns></returns>
[SkipLocalsInit]
public int QuiescenceSearch(int ply, int alpha, int beta)
{
var position = Game.CurrentPosition;
Expand Down

0 comments on commit 7fd04e9

Please sign in to comment.