Skip to content

Commit

Permalink
RavenDB-22986 - improve the performance of getting the free pages count
Browse files Browse the repository at this point in the history
  • Loading branch information
grisha-kotler committed Oct 10, 2024
1 parent e66049b commit 35351ea
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Voron/Impl/FreeSpace/FreeSpaceHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,31 @@ public List<long> AllPages(LowLevelTransaction tx)
}
}

public int GetFreePagesCount(LowLevelTransaction tx)
{
var freeSpaceTree = GetFreeSpaceTree(tx);
if (freeSpaceTree.NumberOfEntries == 0)
return 0;

using (var it = freeSpaceTree.Iterate())
{
if (it.Seek(0) == false)
return 0;

var count = 0;

do
{
var stream = it.CreateReaderForCurrent();
var current = new StreamBitArray(stream);
count += current.GetNumberOfSetBits();

} while (it.MoveNext());

return count;
}
}

public void FreePage(LowLevelTransaction tx, long pageNumber)
{
if (_guard.IsProcessingFixedSizeTree)
Expand Down
1 change: 1 addition & 0 deletions src/Voron/Impl/FreeSpace/IFreeSpaceHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface IFreeSpaceHandling
{
long? TryAllocateFromFreeSpace(LowLevelTransaction tx, int num);
List<long> AllPages(LowLevelTransaction tx);
int GetFreePagesCount(LowLevelTransaction txLowLevelTransaction);
void FreePage(LowLevelTransaction tx, long pageNumber);
long GetFreePagesOverhead(LowLevelTransaction tx);
IEnumerable<long> GetFreePagesOverheadPages(LowLevelTransaction tx);
Expand Down
5 changes: 5 additions & 0 deletions src/Voron/Impl/FreeSpace/NoFreeSpaceHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public List<long> AllPages(LowLevelTransaction tx)
return new List<long>();
}

public int GetFreePagesCount(LowLevelTransaction txLowLevelTransaction)
{
return 0;
}

public void FreePage(LowLevelTransaction tx, long pageNumber)
{

Expand Down
13 changes: 13 additions & 0 deletions src/Voron/Impl/FreeSpace/StreamBitArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Numerics;
using Sparrow.Server;

namespace Voron.Impl.FreeSpace
Expand Down Expand Up @@ -110,6 +111,18 @@ public bool Get(int index)
return (_inner[index >> 5] & (1 << (index & 31))) != 0;
}

public int GetNumberOfSetBits()
{
var count = 0;

for (int i = 0; i < _inner.Length; i++)
{
count += BitOperations.PopCount((uint)_inner[i]);
}

return count;
}

public void Set(int index, bool value)
{
if (value)
Expand Down

0 comments on commit 35351ea

Please sign in to comment.