Skip to content

Commit

Permalink
Merge branch 'main' into importer
Browse files Browse the repository at this point in the history
  • Loading branch information
Scooletz committed Jun 24, 2024
2 parents 49ab586 + ff631b3 commit f5359ac
Show file tree
Hide file tree
Showing 17 changed files with 949 additions and 95 deletions.
82 changes: 82 additions & 0 deletions src/Paprika.Benchmarks/BitMapFilterBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using Paprika.Data;
using Paprika.Store;

namespace Paprika.Benchmarks;

[DisassemblyDiagnoser]
[MemoryDiagnoser]
public class BitMapFilterBenchmarks
{
private readonly Page[] _pages1A = AlignedAlloc(1);
private readonly Page[] _pages1B = AlignedAlloc(1);

private readonly Page[] _pages2A = AlignedAlloc(2);
private readonly Page[] _pages2B = AlignedAlloc(2);

private readonly Page[] _pages16A = AlignedAlloc(128);
private readonly Page[] _pages16B = AlignedAlloc(128);

[Benchmark(OperationsPerInvoke = 4)]
public void Or_BitMapFilter_Of1()
{
var a = new BitMapFilter<BitMapFilter.Of1>(new BitMapFilter.Of1(_pages1A[0]));
var b = new BitMapFilter<BitMapFilter.Of1>(new BitMapFilter.Of1(_pages1B[0]));

a.OrWith(b);
a.OrWith(b);
a.OrWith(b);
a.OrWith(b);
}

[Benchmark(OperationsPerInvoke = 4)]
public void Or_BitMapFilter_Of2()
{
var a = new BitMapFilter<BitMapFilter.Of2>(new BitMapFilter.Of2(_pages2A[0], _pages2A[1]));
var b = new BitMapFilter<BitMapFilter.Of2>(new BitMapFilter.Of2(_pages2B[0], _pages2B[1]));

a.OrWith(b);
a.OrWith(b);
a.OrWith(b);
a.OrWith(b);
}

[Benchmark(OperationsPerInvoke = 4)]
public void Or_BitMapFilter_OfN_128()
{
var a = new BitMapFilter<BitMapFilter.OfN>(new BitMapFilter.OfN(_pages16A));
var b = new BitMapFilter<BitMapFilter.OfN>(new BitMapFilter.OfN(_pages16B));

a.OrWith(b);
a.OrWith(b);
a.OrWith(b);
a.OrWith(b);
}

[Benchmark(OperationsPerInvoke = 4)]
public int MayContainAny_BitMapFilter_OfN_128()
{
var a = new BitMapFilter<BitMapFilter.OfN>(new BitMapFilter.OfN(_pages16A));

return (a.MayContainAny(13, 17) ? 1 : 0) +
(a.MayContainAny(2342, 2345) ? 1 : 0) +
(a.MayContainAny(3453453, 8789345) ? 1 : 0) +
(a.MayContainAny(2346345, 432509) ? 1 : 0);
}

private static unsafe Page[] AlignedAlloc(int pageCount)
{
var pages = new Page[pageCount];

for (int i = 0; i < pageCount; i++)
{
pages[i] = new Page((byte*)NativeMemory.AlignedAlloc(Page.PageSize, Page.PageSize));

// make data more interesting
pages[i].Span.Fill((byte)(1 << (i & 7)));
}

return pages;
}
}
75 changes: 75 additions & 0 deletions src/Paprika.Tests/Data/BitMapFilterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using FluentAssertions;
using Paprika.Chain;
using Paprika.Data;

namespace Paprika.Tests.Data;

public abstract class BitMapFilterTests<TAccessor> : IDisposable
where TAccessor : struct, BitMapFilter.IAccessor<TAccessor>
{
private readonly BufferPool _pool = new(32);

protected abstract BitMapFilter<TAccessor> Build(BufferPool pool);

[Test]
public void Non_colliding_set()
{
var filter = Build(_pool);

var count = (ulong)filter.BucketCount;

for (ulong i = 0; i < count; i++)
{
filter[i].Should().BeFalse();
filter[i] = true;
filter[i].Should().BeTrue();
}

filter.Return(_pool);
}

[Test]
public void Or_with()
{
var filter1 = Build(_pool);
var filter2 = Build(_pool);

const ulong v1 = 1;
const ulong v2 = 4213798855897314219;

filter1.Add(v1);
filter2.Add(v2);

filter1.OrWith(filter2);

filter1.MayContain(v1).Should().BeTrue();
filter1.MayContain(v2).Should().BeTrue();
filter1.MayContainAny(v1, v2).Should().BeTrue();

filter1.Return(_pool);
filter2.Return(_pool);
}

public void Dispose() => _pool.Dispose();
}

[TestFixture]
public class BitMapFilterTestsOf1 : BitMapFilterTests<BitMapFilter.Of1>
{
protected override BitMapFilter<BitMapFilter.Of1> Build(BufferPool pool) => BitMapFilter.CreateOf1(pool);
}

[TestFixture]
public class BitMapFilterTestsOf2 : BitMapFilterTests<BitMapFilter.Of2>
{
protected override BitMapFilter<BitMapFilter.Of2> Build(BufferPool pool) => BitMapFilter.CreateOf2(pool);
}

[TestFixture]
public class BitMapFilterTestsOf4 : BitMapFilterTests<BitMapFilter.OfN>
{
protected override BitMapFilter<BitMapFilter.OfN> Build(BufferPool pool) => BitMapFilter.CreateOfN(pool, 4);
}



27 changes: 27 additions & 0 deletions src/Paprika.Tests/Data/PageExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Runtime.InteropServices;
using FluentAssertions;
using Paprika.Data;
using Paprika.Store;

namespace Paprika.Tests.Data;

public class PageExtensionsTests
{
[Test]
public void Or_with()
{
var page0 = Page.DevOnlyNativeAlloc();
page0.Clear();

var page1 = Page.DevOnlyNativeAlloc();
const byte fill = 0xFF;
page1.Span.Fill(fill); // fill whole page with FFFFF

const int notFound = -1;
page0.Span.IndexOfAnyExcept((byte)0).Should().Be(notFound);

page0.OrWith(page1);

page0.Span.IndexOfAnyExcept(fill).Should().Be(notFound);
}
}
2 changes: 2 additions & 0 deletions src/Paprika.Tests/Store/AbandonedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ public async Task Work_proper_bookkeeping_when_lots_of_reads()

using var block = db.BeginNextBatch();
block.SetAccount(account, value);
block.VerifyDbPagesOnCommit();

await block.Commit(CommitOptions.FlushDataAndRoot);
}

Expand Down
41 changes: 41 additions & 0 deletions src/Paprika.Tests/Store/DbAddressSetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using FluentAssertions;
using Paprika.Store;

namespace Paprika.Tests.Store;

public class DbAddressSetTests
{
[Test]
public void Dummy()
{
const uint max = 1000;

var set = new DbAddressSet(DbAddress.Page(max));

for (uint i = 0; i < max; i++)
{
var addr = DbAddress.Page(i);
set[addr].Should().BeTrue();
set[addr] = false;
}

for (uint i = 0; i < max; i++)
{
var addr = DbAddress.Page(i);
set[addr].Should().BeFalse();
}
}

[Test]
public void Set_enumeration()
{
const uint max = 5;
var set = new DbAddressSet(DbAddress.Page(max));

set[DbAddress.Page(0)] = false;
set[DbAddress.Page(2)] = false;
set[DbAddress.Page(4)] = false;

set.EnumerateSet().ToArray().Should().BeEquivalentTo([DbAddress.Page(1), DbAddress.Page(3)]);
}
}
2 changes: 2 additions & 0 deletions src/Paprika.Tests/Store/DbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public async Task Readonly_transaction_block_till_they_are_released()

// write current
block.SetAccount(Key0, GetValue(i));

block.VerifyDbPagesOnCommit();
await block.Commit(CommitOptions.FlushDataOnly);

read.ShouldHaveAccount(Key0, GetValue(start));
Expand Down
Loading

0 comments on commit f5359ac

Please sign in to comment.