Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix low object LruCaches #7160

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void At_capacity()
Cache cache = Create();
for (int i = 0; i < Capacity; i++)
{
cache.Set(_addresses[i], _accounts[i]);
cache.Set(_addresses[i], _accounts[i]).Should().BeTrue();
}

Account? account = cache.Get(_addresses[Capacity - 1]);
Expand All @@ -52,8 +52,8 @@ public void At_capacity()
public void Can_reset()
{
Cache cache = Create();
cache.Set(_addresses[0], _accounts[0]);
cache.Set(_addresses[0], _accounts[1]);
cache.Set(_addresses[0], _accounts[0]).Should().BeTrue();
cache.Set(_addresses[0], _accounts[1]).Should().BeFalse();
cache.Get(_addresses[0]).Should().Be(_accounts[1]);
}

Expand All @@ -68,10 +68,10 @@ public void Can_ask_before_first_set()
public void Can_clear()
{
Cache cache = Create();
cache.Set(_addresses[0], _accounts[0]);
cache.Set(_addresses[0], _accounts[0]).Should().BeTrue();
cache.Clear();
cache.Get(_addresses[0]).Should().BeNull();
cache.Set(_addresses[0], _accounts[1]);
cache.Set(_addresses[0], _accounts[1]).Should().BeTrue();
cache.Get(_addresses[0]).Should().Be(_accounts[1]);
}

Expand All @@ -81,13 +81,27 @@ public void Beyond_capacity()
Cache cache = Create();
for (int i = 0; i < Capacity * 2; i++)
{
cache.Set(_addresses[i], _accounts[i]);
cache.Set(_addresses[i], _accounts[i]).Should().BeTrue();
}

Account? account = cache.Get(_addresses[Capacity]);
account.Should().Be(_accounts[Capacity]);
}

[Test]
public void Beyond_capacity_lru()
{
Cache cache = Create();
for (int i = 0; i < Capacity * 2; i++)
{
for (int ii = 0; ii < Capacity / 2; ii++)
{
cache.Set(_addresses[i], _accounts[i]);
}
cache.Set(_addresses[i], _accounts[i]);
}
}

[Test]
public void Can_set_and_then_set_null()
{
Expand All @@ -114,7 +128,7 @@ public void Clear_should_free_all_capacity()
Cache cache = Create();
for (int i = 0; i < Capacity; i++)
{
cache.Set(_addresses[i], _accounts[i]);
cache.Set(_addresses[i], _accounts[i]).Should().BeTrue();
}

cache.Clear();
Expand All @@ -124,7 +138,7 @@ public void Clear_should_free_all_capacity()
// fill again
for (int i = 0; i < Capacity; i++)
{
cache.Set(_addresses[i], _accounts[MapForRefill(i)]);
cache.Set(_addresses[i], _accounts[MapForRefill(i)]).Should().BeTrue();
}

// validate
Expand All @@ -145,8 +159,10 @@ public void Delete_keeps_internal_structure()

for (int i = 0; i < iterations; i++)
{
cache.Set(i, i);
cache.Delete(i - itemsToKeep);
cache.Set(i, i).Should().BeTrue();
var remove = i - itemsToKeep;
if (remove >= 0)
cache.Delete(remove).Should().BeTrue();
}

int count = 0;
Expand Down
26 changes: 20 additions & 6 deletions src/Nethermind/Nethermind.Core.Test/Caching/LruCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void At_capacity()
ICache<Address, Account> cache = Create();
for (int i = 0; i < Capacity; i++)
{
cache.Set(_addresses[i], _accounts[i]);
cache.Set(_addresses[i], _accounts[i]).Should().BeTrue();
}

Account? account = cache.Get(_addresses[Capacity - 1]);
Expand All @@ -50,8 +50,8 @@ public void At_capacity()
public void Can_reset()
{
ICache<Address, Account> cache = Create();
cache.Set(_addresses[0], _accounts[0]);
cache.Set(_addresses[0], _accounts[1]);
cache.Set(_addresses[0], _accounts[0]).Should().BeTrue();
cache.Set(_addresses[0], _accounts[1]).Should().BeFalse();
cache.Get(_addresses[0]).Should().Be(_accounts[1]);
}

Expand All @@ -66,21 +66,35 @@ public void Can_ask_before_first_set()
public void Can_clear()
{
ICache<Address, Account> cache = Create();
cache.Set(_addresses[0], _accounts[0]);
cache.Set(_addresses[0], _accounts[0]).Should().BeTrue();
cache.Clear();
cache.Get(_addresses[0]).Should().BeNull();
cache.Set(_addresses[0], _accounts[1]);
cache.Set(_addresses[0], _accounts[1]).Should().BeTrue();
cache.Get(_addresses[0]).Should().Be(_accounts[1]);
}

[Test]
public void Beyond_capacity()
public void Beyond_capacity_lru()
{
ICache<Address, Account> cache = Create();
for (int i = 0; i < Capacity * 2; i++)
{
for (int ii = 0; ii < Capacity / 2; ii++)
{
cache.Set(_addresses[i], _accounts[i]);
}
cache.Set(_addresses[i], _accounts[i]);
}
}

[Test]
public void Beyond_capacity()
{
ICache<Address, Account> cache = Create();
for (int i = 0; i < Capacity * 2; i++)
{
cache.Set(_addresses[i], _accounts[i]).Should().BeTrue();
}

Account? account = cache.Get(_addresses[Capacity]);
account.Should().Be(_accounts[Capacity]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ public void Beyond_capacity()
cache.Get(_addresses[Capacity]).Should().BeTrue();
}

[Test]
public void Beyond_capacity_lru()
{
LruKeyCacheLowObject<AddressAsKey> cache = new(Capacity, "test");
for (int i = 0; i < Capacity * 2; i++)
{
for (int ii = 0; ii < Capacity / 2; ii++)
{
cache.Set(_addresses[i]);
}
cache.Set(_addresses[i]);
}
}

benaadams marked this conversation as resolved.
Show resolved Hide resolved
[Test]
public void Can_delete()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using FluentAssertions;
using Nethermind.Core.Caching;
using Nethermind.Core.Test.Builders;
using Nethermind.Int256;
using NUnit.Framework;

namespace Nethermind.Core.Test.Caching
{
[TestFixture]
public class LruKeyCacheNonConcurrentTests
{
private const int Capacity = 16;

private readonly Account[] _accounts = new Account[Capacity * 2];
private readonly Address[] _addresses = new Address[Capacity * 2];

[SetUp]
public void Setup()
{
for (int i = 0; i < Capacity * 2; i++)
{
_accounts[i] = Build.An.Account.WithBalance((UInt256)i).TestObject;
_addresses[i] = Build.An.Address.FromNumber(i).TestObject;
}
}

[Test]
public void At_capacity()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
for (int i = 0; i < Capacity; i++)
{
cache.Set(_addresses[i]).Should().BeTrue();
}

cache.Get(_addresses[Capacity - 1]).Should().BeTrue();
}

[Test]
public void Can_reset()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
cache.Set(_addresses[0]).Should().BeTrue();
cache.Set(_addresses[0]).Should().BeFalse();
cache.Get(_addresses[0]).Should().BeTrue();
}

[Test]
public void Can_ask_before_first_set()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
cache.Get(_addresses[0]).Should().BeFalse();
}

[Test]
public void Can_clear()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
cache.Set(_addresses[0]).Should().BeTrue();
cache.Clear();
cache.Get(_addresses[0]).Should().BeFalse();
cache.Set(_addresses[0]).Should().BeTrue();
cache.Get(_addresses[0]).Should().BeTrue();
}

[Test]
public void Beyond_capacity()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
for (int i = 0; i < Capacity * 2; i++)
{
cache.Set(_addresses[i]);
}

cache.Get(_addresses[Capacity]).Should().BeTrue();
}

[Test]
public void Beyond_capacity_lru()
{
LruKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity, "test");
for (int i = 0; i < Capacity * 2; i++)
{
for (int ii = 0; ii < Capacity / 2; ii++)
{
cache.Set(_addresses[i]);
}
cache.Set(_addresses[i]);
}
}

[Test]
public void Can_delete()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
cache.Set(_addresses[0]).Should().BeTrue();
cache.Delete(_addresses[0]);
cache.Get(_addresses[0]).Should().BeFalse();
}
}
}
20 changes: 17 additions & 3 deletions src/Nethermind/Nethermind.Core.Test/Caching/LruKeyCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void At_capacity()
LruKeyCache<Address> cache = new(Capacity, "test");
for (int i = 0; i < Capacity; i++)
{
cache.Set(_addresses[i]);
cache.Set(_addresses[i]).Should().BeTrue();
}

cache.Get(_addresses[Capacity - 1]).Should().BeTrue();
Expand Down Expand Up @@ -72,17 +72,31 @@ public void Beyond_capacity()
LruKeyCache<Address> cache = new(Capacity, "test");
for (int i = 0; i < Capacity * 2; i++)
{
cache.Set(_addresses[i]);
cache.Set(_addresses[i]).Should().BeTrue();
}

cache.Get(_addresses[Capacity]).Should().BeTrue();
}

[Test]
public void Beyond_capacity_lru()
{
LruKeyCache<AddressAsKey> cache = new(Capacity, "test");
for (int i = 0; i < Capacity * 2; i++)
{
for (int ii = 0; ii < Capacity / 2; ii++)
{
cache.Set(_addresses[i]);
}
cache.Set(_addresses[i]);
}
}

benaadams marked this conversation as resolved.
Show resolved Hide resolved
[Test]
public void Can_delete()
{
LruKeyCache<Address> cache = new(Capacity, "test");
cache.Set(_addresses[0]);
cache.Set(_addresses[0]).Should().BeTrue();
cache.Delete(_addresses[0]);
cache.Get(_addresses[0]).Should().BeFalse();
}
Expand Down
28 changes: 21 additions & 7 deletions src/Nethermind/Nethermind.Core.Test/Caching/SpanLruCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void At_capacity()
ISpanCache<byte, Account> cache = Create();
for (int i = 0; i < Capacity; i++)
{
cache.Set(_addresses[i].Bytes, _accounts[i]);
cache.Set(_addresses[i].Bytes, _accounts[i]).Should().BeTrue();
}

Account? account = cache.Get(_addresses[Capacity - 1].Bytes);
Expand All @@ -51,8 +51,8 @@ public void At_capacity()
public void Can_reset()
{
ISpanCache<byte, Account> cache = Create();
cache.Set(_addresses[0].Bytes, _accounts[0]);
cache.Set(_addresses[0].Bytes, _accounts[1]);
cache.Set(_addresses[0].Bytes, _accounts[0]).Should().BeTrue();
cache.Set(_addresses[0].Bytes, _accounts[1]).Should().BeFalse();
cache.Get(_addresses[0].Bytes).Should().Be(_accounts[1]);
}

Expand All @@ -67,10 +67,10 @@ public void Can_ask_before_first_set()
public void Can_clear()
{
ISpanCache<byte, Account> cache = Create();
cache.Set(_addresses[0].Bytes, _accounts[0]);
cache.Set(_addresses[0].Bytes, _accounts[0]).Should().BeTrue();
cache.Clear();
cache.Get(_addresses[0].Bytes).Should().BeNull();
cache.Set(_addresses[0].Bytes, _accounts[1]);
cache.Set(_addresses[0].Bytes, _accounts[1]).Should().BeTrue();
cache.Get(_addresses[0].Bytes).Should().Be(_accounts[1]);
}

Expand All @@ -80,13 +80,27 @@ public void Beyond_capacity()
ISpanCache<byte, Account> cache = Create();
for (int i = 0; i < Capacity * 2; i++)
{
cache.Set(_addresses[i].Bytes, _accounts[i]);
cache.Set(_addresses[i].Bytes, _accounts[i]).Should().BeTrue();
}

Account? account = cache.Get(_addresses[Capacity].Bytes);
account.Should().Be(_accounts[Capacity]);
}

[Test]
public void Beyond_capacity_lru()
{
ISpanCache<byte, Account> cache = Create();
for (int i = 0; i < Capacity * 2; i++)
{
for (int ii = 0; ii < Capacity / 2; ii++)
{
cache.Set(_addresses[i].Bytes, _accounts[i]);
}
cache.Set(_addresses[i].Bytes, _accounts[i]);
}
}

[Test]
public void Can_set_and_then_set_null()
{
Expand All @@ -101,7 +115,7 @@ public void Can_set_and_then_set_null()
public void Can_delete()
{
ISpanCache<byte, Account> cache = Create();
cache.Set(_addresses[0].Bytes, _accounts[0]);
cache.Set(_addresses[0].Bytes, _accounts[0]).Should().BeTrue();
cache.Delete(_addresses[0].Bytes).Should().BeTrue();
cache.Get(_addresses[0].Bytes).Should().Be(null);
cache.Delete(_addresses[0].Bytes).Should().BeFalse();
Expand Down
Loading