Skip to content

Commit

Permalink
RavenDB-22986 - use constants
Browse files Browse the repository at this point in the history
  • Loading branch information
grisha-kotler committed Oct 7, 2024
1 parent 7ea2dd9 commit 7bb24e0
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/Voron/Impl/FreeSpace/StreamBitArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ namespace Voron.Impl.FreeSpace
{
public sealed class StreamBitArray
{
readonly int[] _inner = new int[64];
private const int CountOfItems = 64;
private const int BitsInItem = 32;
private const int TotalBits = CountOfItems * BitsInItem;

readonly int[] _inner = new int[CountOfItems];
public int SetCount { get; private set; }

public StreamBitArray()
Expand Down Expand Up @@ -124,15 +127,15 @@ public bool Get(int index)
case 0:
continue;
case -1:
return i * 32;
return i * BitsInItem;
default:
return i * 32 + BitOperations.TrailingZeroCount(_inner[i]);
return i * BitsInItem + BitOperations.TrailingZeroCount(_inner[i]);
}
}

return null;

case < 32:
case < BitsInItem:
// finding sequences up to 32 bits
for (var i = 0; i < _inner.Length; i++)
{
Expand All @@ -141,23 +144,23 @@ public bool Get(int index)
continue;

if (current == -1)
return i * 32;
return i * BitsInItem;

int firstSetBitPos = BitOperations.TrailingZeroCount((uint)current);

// Only proceed if there is a set bit and it's within range
int mask = (1 << num) - 1; // Create a mask of num 1s

if (firstSetBitPos <= 32 - num)
if (firstSetBitPos <= BitsInItem - num)
{
for (int bitPos = firstSetBitPos; bitPos <= 32 - num; bitPos++)
for (int bitPos = firstSetBitPos; bitPos <= BitsInItem - num; bitPos++)
{
var temp = mask << bitPos; // Shift the mask to the current position

// Check if the current block has the sequence of 1s
if ((current & temp) == temp)
{
return i * 32 + bitPos; // Found the sequence, return the position
return i * BitsInItem + bitPos; // Found the sequence, return the position
}
}
}
Expand All @@ -174,7 +177,7 @@ public bool Get(int index)
var numberOfSetBitsNext = BitOperations.TrailingZeroCount(~nextBlock);

if (numberOfSetBitsCurrent + numberOfSetBitsNext >= num)
return (i * 32) + (32 - numberOfSetBitsCurrent);
return (i * BitsInItem) + (BitsInItem - numberOfSetBitsCurrent);
}

return null;
Expand All @@ -199,10 +202,10 @@ public bool Get(int index)
{
if (start == -1)
{
start = i * 32;
start = i * BitsInItem;
}

count += 32;
count += BitsInItem;
if (count >= num)
return start;

Expand All @@ -216,7 +219,7 @@ public bool Get(int index)
}
else
{
if (count + (2048 - i * 32) < num)
if (count + (TotalBits - i * BitsInItem) < num)
{
// impossible to satisfy the continuous bit requirement
return null;
Expand Down Expand Up @@ -250,7 +253,7 @@ void CheckTrailingSequence()
else
{
// Calculate the starting bit position in the array
start = (i * 32) + (32 - numberOfSetBits);
start = (i * BitsInItem) + (BitsInItem - numberOfSetBits);
count = numberOfSetBits;
}
}
Expand Down

0 comments on commit 7bb24e0

Please sign in to comment.