Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Scooletz committed Jul 25, 2024
1 parent 0ef13aa commit 90c99b1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
33 changes: 32 additions & 1 deletion src/Paprika.Tests/Data/SlottedArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,36 @@ public void Breach_VectorSize_with_key_count()
}
}

[Test(Description = "Make a lot of requests to make breach the vector count")]
public void Set_Get_With_Specific_Lengths([Values(8, 16, 32, 64, 68, 72)] int count)
{
const int keyLength = 2;

Span<byte> keys = stackalloc byte[count * 2];
for (byte i = 0; i < count; i++)
{
keys[i * keyLength] = i;
keys[i * keyLength + 1] = i;
}

var map = new SlottedArray(new byte[Page.PageSize]);

for (var i = 0; i < count; i++)
{
map.SetAssert(GetKey(keys, i), GetValue(i));
}

for (var i = 0; i < count; i++)
{
map.GetAssert(GetKey(keys, i), GetValue(i));
}

return;

static NibblePath GetKey(Span<byte> keys, int i) => NibblePath.FromKey(keys.Slice(i * keyLength, keyLength));
static ReadOnlySpan<byte> GetValue(int i) => new byte[(byte)(i & 255)];
}

private static ReadOnlySpan<byte> Data(byte key) => new[] { key };

[Test]
Expand Down Expand Up @@ -669,7 +699,8 @@ public static void GetAssert(this SlottedArray map, in ReadOnlySpan<byte> key, R

public static void GetAssert(this SlottedArray map, in NibblePath key, ReadOnlySpan<byte> expected)
{
map.TryGet(key, out var actual).Should().BeTrue();
var retrieved = map.TryGet(key, out var actual);
retrieved.Should().BeTrue();
actual.SequenceEqual(expected).Should().BeTrue("Actual data should equal expected");
}

Expand Down
13 changes: 11 additions & 2 deletions src/Paprika.Tests/Store/DbTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Buffers.Binary;
using System.Diagnostics;
using FluentAssertions;
using Nethermind.Int256;
using NUnit.Framework;
Expand Down Expand Up @@ -197,7 +198,7 @@ public async Task Spin_large()
using var db = PagedDb.NativeMemoryDb(size);

const int batches = 25;
const int storageSlots = 10_000;
const int storageSlots = 5_000;
const int storageKeyLength = 32;

var value = new byte[32];
Expand All @@ -209,13 +210,21 @@ public async Task Spin_large()

var readBatches = new List<IReadOnlyBatch>();

for (var i = 0; i < batches; i++)
//for (var i = 0; i < batches; i++)
{
using var batch = db.BeginNextBatch();

for (var slot = 0; slot < storageSlots; slot++)
{
if (slot >= 4890)
Debugger.Break();

batch.SetStorage(account, GetStorageAddress(slot), value);

if (slot >= 4890)
{
batch.AssertStorageValue(account, GetStorageAddress(4890), value);
}
}

await batch.Commit(CommitOptions.FlushDataAndRoot);
Expand Down
21 changes: 14 additions & 7 deletions src/Paprika/Data/SlottedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public readonly ref struct SlottedArray

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int AlignToDoubleVectorSize(int count) => (count + (DoubleVectorSize - 1)) & -DoubleVectorSize;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int AlignToVectorSize(int count) => (count + (VectorSize - 1)) & -VectorSize;

Expand Down Expand Up @@ -491,14 +491,14 @@ public void Clear()
private int TryGetImpl(in NibblePath key, ushort hash, byte preamble, out Span<byte> data)
{
var count = _header.Low / Slot.TotalSize;
var jump = DoubleVectorSize / sizeof(ushort);
var aligned = AlignToDoubleVectorSize(_header.Low) / sizeof(ushort);

ref var d = ref Unsafe.As<byte, ushort>(ref MemoryMarshal.GetReference(_data));

if (Vector256.IsHardwareAccelerated)
{
var search = Vector256.Create(hash);
var jump = DoubleVectorSize / sizeof(ushort);

for (var i = 0; i < aligned; i += jump)
{
Expand All @@ -509,9 +509,13 @@ private int TryGetImpl(in NibblePath key, ushort hash, byte preamble, out Span<b

if (i + jump >= aligned)
{
// Undoing the multiplication done above to calculate aligned, to get the number of items.
var alignedCount = aligned / VectorsByBatch;
var toClear = alignedCount - count;

// This is the last in batch, masking is required to remove potential hits that are false positive
var shift = count & (VectorSize - 1);
var mask = (1U << shift) - 1;
var hashesPerVector = VectorSize / sizeof(ushort);
var mask = (1U << hashesPerVector - toClear) - 1;
matches &= mask;
}

Expand All @@ -529,7 +533,6 @@ private int TryGetImpl(in NibblePath key, ushort hash, byte preamble, out Span<b
else if (Vector128.IsHardwareAccelerated)
{
var search = Vector128.Create(hash);
var jump = DoubleVectorSize / sizeof(ushort);

for (var i = 0; i < aligned; i += jump)
{
Expand All @@ -540,9 +543,13 @@ private int TryGetImpl(in NibblePath key, ushort hash, byte preamble, out Span<b

if (i + jump >= aligned)
{
// Undoing the multiplication done above to calculate aligned, to get the number of items.
var alignedCount = aligned / VectorsByBatch;
var toClear = alignedCount - count;

// This is the last in batch, masking is required to remove potential hits that are false positive
var shift = count & (VectorSize - 1);
var mask = (1U << shift) - 1;
var hashesPerVector = VectorSize / sizeof(ushort);
var mask = (1U << hashesPerVector - toClear) - 1;
matches &= mask;
}

Expand Down

0 comments on commit 90c99b1

Please sign in to comment.