Skip to content

Commit

Permalink
Use ClockCache rather than LurCache for blocks and headers (#7439)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Sep 16, 2024
1 parent 0e1220c commit 1718592
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class BlockStore : IBlockStore
private readonly BlockDecoder _blockDecoder = new();
public const int CacheSize = 128 + 32;

private readonly LruCache<ValueHash256, Block>
_blockCache = new(CacheSize, CacheSize, "blocks");
private readonly ClockCache<ValueHash256, Block>
_blockCache = new(CacheSize);
private readonly long? _maxSize;

public BlockStore(IDb blockDb, long? maxSize = null)
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Blockchain/Headers/HeaderStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class HeaderStore : IHeaderStore
private readonly IDb _headerDb;
private readonly IDb _blockNumberDb;
private readonly HeaderDecoder _headerDecoder = new();
private readonly LruCache<ValueHash256, BlockHeader> _headerCache =
new(CacheSize, CacheSize, "headers");
private readonly ClockCache<ValueHash256, BlockHeader> _headerCache =
new(CacheSize);

public HeaderStore(IDb headerDb, IDb blockNumberDb)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Nethermind/Nethermind.Evm/Tracing/NullBlockTracer.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Threading;
using Nethermind.Core;

namespace Nethermind.Evm.Tracing
{
public class NullBlockTracer : BlockTracer
public sealed class NullBlockTracer : BlockTracer
{
private static NullBlockTracer? _instance;
public static NullBlockTracer Instance => LazyInitializer.EnsureInitialized(ref _instance, () => new NullBlockTracer());
public static NullBlockTracer Instance { get; } = new NullBlockTracer();
public override ITxTracer StartNewTxTrace(Transaction? tx) => NullTxTracer.Instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ public static class KeyValueStoreRlpExtensions
{
[SkipLocalsInit]
public static TItem? Get<TItem>(this IReadOnlyKeyValueStore db, long blockNumber, ValueHash256 hash, IRlpStreamDecoder<TItem> decoder,
LruCache<ValueHash256, TItem> cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class
ClockCache<ValueHash256, TItem> cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class
{
Span<byte> dbKey = stackalloc byte[40];
KeyValueStoreExtensions.GetBlockNumPrefixedKey(blockNumber, hash, dbKey);
return Get(db, hash, dbKey, decoder, cache, rlpBehaviors, shouldCache);
}

public static TItem? Get<TItem>(this IReadOnlyKeyValueStore db, ValueHash256 key, IRlpStreamDecoder<TItem> decoder, LruCache<ValueHash256, TItem> cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class
public static TItem? Get<TItem>(this IReadOnlyKeyValueStore db, ValueHash256 key, IRlpStreamDecoder<TItem> decoder, ClockCache<ValueHash256, TItem> cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class
{
return Get(db, key, key.Bytes, decoder, cache, rlpBehaviors, shouldCache);
}

public static TItem? Get<TItem>(this IReadOnlyKeyValueStore db, long key, IRlpStreamDecoder<TItem>? decoder, LruCache<long, TItem>? cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class
public static TItem? Get<TItem>(this IReadOnlyKeyValueStore db, long key, IRlpStreamDecoder<TItem>? decoder, ClockCache<long, TItem>? cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class
{
ReadOnlySpan<byte> keyDb = key.ToBigEndianSpanWithoutLeadingZeros(out _);
return Get(db, key, keyDb, decoder, cache, rlpBehaviors, shouldCache);
Expand All @@ -38,10 +38,11 @@ public static class KeyValueStoreRlpExtensions
TCacheKey cacheKey,
ReadOnlySpan<byte> key,
IRlpStreamDecoder<TItem> decoder,
LruCache<TCacheKey, TItem> cache = null,
ClockCache<TCacheKey, TItem> cache = null,
RlpBehaviors rlpBehaviors = RlpBehaviors.None,
bool shouldCache = true
) where TItem : class
where TCacheKey : struct, IEquatable<TCacheKey>
{
TItem item = cache?.Get(cacheKey);
if (item is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ChainLevelInfoRepository : IChainLevelInfoRepository
private const int CacheSize = 64;

private readonly object _writeLock = new();
private readonly LruCache<long, ChainLevelInfo> _blockInfoCache = new LruCache<long, ChainLevelInfo>(CacheSize, CacheSize, "chain level infos");
private readonly ClockCache<long, ChainLevelInfo> _blockInfoCache = new(CacheSize);

private readonly IDb _blockInfoDb;

Expand Down

0 comments on commit 1718592

Please sign in to comment.