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

Switch to lower contention ClockCache #7215

Merged
merged 7 commits into from
Jun 25, 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 @@ -12,7 +12,7 @@ public class PeerInfo : IUserOperationPoolPeer
{
private IUserOperationPoolPeer Peer { get; }

private LruKeyCacheLowObject<ValueHash256> NotifiedUserOperations { get; } = new(MemoryAllowance.MemPoolSize, "notifiedUserOperations");
private ClockKeyCache<ValueHash256> NotifiedUserOperations { get; } = new(MemoryAllowance.MemPoolSize);

public PeerInfo(IUserOperationPoolPeer peer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
using Nethermind.Int256;
using NUnit.Framework;

using Cache = Nethermind.Core.Caching.LruCacheLowObject<Nethermind.Core.AddressAsKey, Nethermind.Core.Account>;
using Cache = Nethermind.Core.Caching.ClockCache<Nethermind.Core.AddressAsKey, Nethermind.Core.Account>;

namespace Nethermind.Core.Test.Caching
{
[TestFixture]
public class LruCacheLowObjectTests
public class ClockCacheTests
{
private static Cache Create()
{
return new Cache(Capacity, "test")!;
return new Cache(Capacity)!;
}

private const int Capacity = 32;
Expand Down Expand Up @@ -183,7 +183,7 @@ public void Beyond_capacity_lru_check()
[Test]
public void Beyond_capacity_lru_parallel()
{
Cache cache = new(Capacity, "test");
Cache cache = new(Capacity);
Parallel.For(0, Environment.ProcessorCount * 8, (iter) =>
{
for (int ii = 0; ii < Capacity; ii++)
Expand Down Expand Up @@ -275,7 +275,7 @@ public void Delete_keeps_internal_structure()
int itemsToKeep = 10;
int iterations = 40;

LruCacheLowObject<int, int> cache = new(maxCapacity, "test");
ClockCache<int, int> cache = new(maxCapacity);

for (int i = 0; i < iterations; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Threading.Tasks;
using FluentAssertions;
using Nethermind.Core.Caching;
using Nethermind.Core.Test.Builders;
Expand All @@ -12,7 +11,7 @@
namespace Nethermind.Core.Test.Caching
{
[TestFixture]
public class LruKeyCacheNonConcurrentTests
public class ClockKeyCacheNonConcurrentTests
{
private const int Capacity = 16;

Expand All @@ -32,7 +31,7 @@ public void Setup()
[Test]
public void At_capacity()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
ClockKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity);
for (int i = 0; i < Capacity; i++)
{
cache.Set(_addresses[i]).Should().BeTrue();
Expand All @@ -44,7 +43,7 @@ public void At_capacity()
[Test]
public void Can_reset()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
ClockKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity);
cache.Set(_addresses[0]).Should().BeTrue();
cache.Set(_addresses[0]).Should().BeFalse();
cache.Get(_addresses[0]).Should().BeTrue();
Expand All @@ -53,14 +52,14 @@ public void Can_reset()
[Test]
public void Can_ask_before_first_set()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
ClockKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity);
cache.Get(_addresses[0]).Should().BeFalse();
}

[Test]
public void Can_clear()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
ClockKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity);
cache.Set(_addresses[0]).Should().BeTrue();
cache.Clear();
cache.Get(_addresses[0]).Should().BeFalse();
Expand All @@ -71,7 +70,7 @@ public void Can_clear()
[Test]
public void Beyond_capacity()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
ClockKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity);
for (int i = 0; i < Capacity * 2; i++)
{
cache.Set(_addresses[i]);
Expand All @@ -91,7 +90,7 @@ public void Beyond_capacity()
[Test]
public void Beyond_capacity_lru()
{
LruKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity);
for (int i = 0; i < Capacity * 2; i++)
{
for (int ii = 0; ii < Capacity / 2; ii++)
Expand All @@ -106,7 +105,7 @@ public void Beyond_capacity_lru()
public void Beyond_capacity_lru_check()
{
Random random = new();
LruKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity);
for (var iter = 0; iter < Capacity; iter++)
{
for (int ii = 0; ii < Capacity; ii++)
Expand Down Expand Up @@ -173,7 +172,7 @@ public void Beyond_capacity_lru_check()
[Test]
public void Can_delete()
{
LruKeyCacheNonConcurrent<Address> cache = new(Capacity, "test");
ClockKeyCacheNonConcurrent<AddressAsKey> cache = new(Capacity);
cache.Set(_addresses[0]).Should().BeTrue();
cache.Delete(_addresses[0]);
cache.Get(_addresses[0]).Should().BeFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Nethermind.Core.Test.Caching
{
[TestFixture]
public class LruKeyCacheLowObjectTests
public class ClockKeyCacheTests
{
private const int Capacity = 32;

Expand All @@ -32,7 +32,7 @@ public void Setup()
[Test]
public void At_capacity()
{
LruKeyCacheLowObject<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCache<AddressAsKey> cache = new(Capacity);
for (int i = 0; i < Capacity; i++)
{
cache.Set(_addresses[i]);
Expand All @@ -44,7 +44,7 @@ public void At_capacity()
[Test]
public void Can_reset()
{
LruKeyCacheLowObject<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCache<AddressAsKey> cache = new(Capacity);
cache.Set(_addresses[0]).Should().BeTrue();
cache.Set(_addresses[0]).Should().BeFalse();
cache.Get(_addresses[0]).Should().BeTrue();
Expand All @@ -53,14 +53,14 @@ public void Can_reset()
[Test]
public void Can_ask_before_first_set()
{
LruKeyCacheLowObject<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCache<AddressAsKey> cache = new(Capacity);
cache.Get(_addresses[0]).Should().BeFalse();
}

[Test]
public void Can_clear()
{
LruKeyCacheLowObject<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCache<AddressAsKey> cache = new(Capacity);
cache.Set(_addresses[0]).Should().BeTrue();
cache.Clear();
cache.Get(_addresses[0]).Should().BeFalse();
Expand All @@ -71,7 +71,7 @@ public void Can_clear()
[Test]
public void Beyond_capacity()
{
LruKeyCacheLowObject<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCache<AddressAsKey> cache = new(Capacity);
for (int i = 0; i < Capacity * 2; i++)
{
cache.Set(_addresses[i]);
Expand All @@ -91,7 +91,7 @@ public void Beyond_capacity()
[Test]
public void Beyond_capacity_lru()
{
LruKeyCacheLowObject<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCache<AddressAsKey> cache = new(Capacity);
for (int i = 0; i < Capacity * 2; i++)
{
for (int ii = 0; ii < Capacity / 2; ii++)
Expand All @@ -106,7 +106,7 @@ public void Beyond_capacity_lru()
public void Beyond_capacity_lru_check()
{
Random random = new();
LruKeyCacheLowObject<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCache<AddressAsKey> cache = new(Capacity);
for (var iter = 0; iter < Capacity; iter++)
{
for (int ii = 0; ii < Capacity; ii++)
Expand All @@ -116,25 +116,6 @@ public void Beyond_capacity_lru_check()

for (int i = 1; i < Capacity; i++)
{
for (int ii = i - 1; ii < i - 1 + Capacity; ii++)
{
// Fuzz the order of the addresses
var index = random.Next(i - 1, i - 1 + Capacity);
cache.Delete(_addresses[index]).Should().BeTrue();
cache.Set(_addresses[index]).Should().BeTrue();
}
for (int ii = i - 1; ii < i - 1 + Capacity; ii++)
{
// Fuzz the order of the addresses
var index = random.Next(i - 1, i - 1 + Capacity);
cache.Set(_addresses[index]).Should().BeFalse();
}
for (int ii = i - 1; ii < i - 1 + Capacity; ii++)
{
// Fuzz the order of the addresses
var index = random.Next(i - 1, i - 1 + Capacity);
cache.Get(_addresses[index]).Should().BeTrue();
}
for (int ii = i; ii < i + Capacity; ii++)
{
if (ii < i + Capacity - 1)
Expand Down Expand Up @@ -173,7 +154,7 @@ public void Beyond_capacity_lru_check()
[Test]
public void Beyond_capacity_lru_parallel()
{
LruKeyCacheLowObject<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCache<AddressAsKey> cache = new(Capacity);
Parallel.For(0, Environment.ProcessorCount * 8, (s) =>
{
for (int ii = 0; ii < Capacity; ii++)
Expand Down Expand Up @@ -208,7 +189,7 @@ public void Beyond_capacity_lru_parallel()
[Test]
public void Can_delete()
{
LruKeyCacheLowObject<AddressAsKey> cache = new(Capacity, "test");
ClockKeyCache<AddressAsKey> cache = new(Capacity);
cache.Set(_addresses[0]);
cache.Delete(_addresses[0]);
cache.Get(_addresses[0]).Should().BeFalse();
Expand Down
8 changes: 6 additions & 2 deletions src/Nethermind/Nethermind.Core/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,12 @@ public readonly struct AddressAsKey(Address key) : IEquatable<AddressAsKey>
public static implicit operator Address(AddressAsKey key) => key._key;
public static implicit operator AddressAsKey(Address key) => new(key);

public bool Equals(AddressAsKey other) => _key.Equals(other._key);
public override int GetHashCode() => _key.GetHashCode();
public bool Equals(AddressAsKey other) => _key == other._key;
public override int GetHashCode() => _key?.GetHashCode() ?? 0;
public override string ToString()
{
return _key?.ToString() ?? "<null>";
}
}

public ref struct AddressStructRef
Expand Down
Loading