Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Use Unsafe.Add for PSQT() #1153

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/Lynx/PSQT.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Lynx.Model;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static Lynx.TunableEvalParameters;

namespace Lynx;
Expand Down Expand Up @@ -99,16 +101,11 @@ static EvaluationPSQTs()
public static int PSQT(int friendEnemy, int bucket, int piece, int square)
{
var index = PSQTIndex(friendEnemy, bucket, piece, square);
Debug.Assert(index >= 0 && index < _packedPSQT.Length);

unsafe
{
Comment on lines 106 to 107

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notably the unsafe { } is unnecessary

// Since _tt is a pinned array
// This is no-op pinning as it does not influence the GC compaction
// https://tooslowexception.com/pinned-object-heap-in-net-5/
fixed (int* psqtPtr = &_packedPSQT[0])
{
return psqtPtr[index];
}
return Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_packedPSQT), index);
}
}

Expand Down
Loading