diff --git a/src/Voron/Impl/FreeSpace/StreamBitArray.cs b/src/Voron/Impl/FreeSpace/StreamBitArray.cs index 67fe48304018..060c8fe9c3d4 100644 --- a/src/Voron/Impl/FreeSpace/StreamBitArray.cs +++ b/src/Voron/Impl/FreeSpace/StreamBitArray.cs @@ -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 { @@ -116,6 +118,24 @@ public bool Get(int index) private int? FirstSetBit() { + if (Avx2.IsSupported) + { + for (int i = 0; i < _inner.Length; i += Vector256.Count) + { + var a = Vector256.LoadUnsafe(ref _inner[i]).AsUInt32(); + var gt = Vector256.GreaterThan(a, Vector256.Zero); + if (gt == Vector256.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++) {