Skip to content

Commit

Permalink
RavenDB-22986 - use Vector256 for finding the first set bit
Browse files Browse the repository at this point in the history
  • Loading branch information
grisha-kotler committed Oct 14, 2024
1 parent f670a54 commit c40998c
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Voron/Impl/FreeSpace/StreamBitArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
using Sparrow.Server;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace Voron.Impl.FreeSpace
{
Expand Down Expand Up @@ -116,6 +118,24 @@ public bool Get(int index)

private int? FirstSetBit()
{
if (Avx2.IsSupported)
{
for (int i = 0; i < _inner.Length; i += Vector256<uint>.Count)
{
var a = Vector256.LoadUnsafe(ref _inner[i]).AsUInt32();
var gt = Vector256.GreaterThan(a, Vector256<uint>.Zero);
if (gt == Vector256<uint>.Zero)
continue;

var mask = gt.ExtractMostSignificantBits();
var idx = BitOperations.TrailingZeroCount(mask) + i;
var item = _inner[idx];
return idx * BitsInWord + BitOperations.TrailingZeroCount(item);
}

return null;
}

// finding a single set bit
for (var i = 0; i < _inner.Length; i++)
{
Expand Down

0 comments on commit c40998c

Please sign in to comment.