From c40998c352c465b05179689ece9cc199b7c65dd3 Mon Sep 17 00:00:00 2001 From: Grisha Kotler Date: Mon, 14 Oct 2024 23:05:04 +0300 Subject: [PATCH] RavenDB-22986 - use Vector256 for finding the first set bit --- src/Voron/Impl/FreeSpace/StreamBitArray.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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++) {