From 1718592af9e5ced5d27673ecda692b4079060aaf Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Mon, 16 Sep 2024 09:15:39 +0100 Subject: [PATCH] Use ClockCache rather than LurCache for blocks and headers (#7439) --- .../Nethermind.Blockchain/Blocks/BlockStore.cs | 4 ++-- .../Nethermind.Blockchain/Headers/HeaderStore.cs | 4 ++-- src/Nethermind/Nethermind.Evm/Tracing/NullBlockTracer.cs | 6 ++---- .../KeyValueStoreRlpExtensions.cs | 9 +++++---- .../Repositories/ChainLevelInfoRepository.cs | 2 +- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs b/src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs index 20aee80ea04..43b8c6533f1 100644 --- a/src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs +++ b/src/Nethermind/Nethermind.Blockchain/Blocks/BlockStore.cs @@ -20,8 +20,8 @@ public class BlockStore : IBlockStore private readonly BlockDecoder _blockDecoder = new(); public const int CacheSize = 128 + 32; - private readonly LruCache - _blockCache = new(CacheSize, CacheSize, "blocks"); + private readonly ClockCache + _blockCache = new(CacheSize); private readonly long? _maxSize; public BlockStore(IDb blockDb, long? maxSize = null) diff --git a/src/Nethermind/Nethermind.Blockchain/Headers/HeaderStore.cs b/src/Nethermind/Nethermind.Blockchain/Headers/HeaderStore.cs index 7001ee14e51..bdbd049356a 100644 --- a/src/Nethermind/Nethermind.Blockchain/Headers/HeaderStore.cs +++ b/src/Nethermind/Nethermind.Blockchain/Headers/HeaderStore.cs @@ -21,8 +21,8 @@ public class HeaderStore : IHeaderStore private readonly IDb _headerDb; private readonly IDb _blockNumberDb; private readonly HeaderDecoder _headerDecoder = new(); - private readonly LruCache _headerCache = - new(CacheSize, CacheSize, "headers"); + private readonly ClockCache _headerCache = + new(CacheSize); public HeaderStore(IDb headerDb, IDb blockNumberDb) { diff --git a/src/Nethermind/Nethermind.Evm/Tracing/NullBlockTracer.cs b/src/Nethermind/Nethermind.Evm/Tracing/NullBlockTracer.cs index f3c270572fb..5ebe1869118 100644 --- a/src/Nethermind/Nethermind.Evm/Tracing/NullBlockTracer.cs +++ b/src/Nethermind/Nethermind.Evm/Tracing/NullBlockTracer.cs @@ -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; } } diff --git a/src/Nethermind/Nethermind.Serialization.Rlp/KeyValueStoreRlpExtensions.cs b/src/Nethermind/Nethermind.Serialization.Rlp/KeyValueStoreRlpExtensions.cs index 1042f94031a..844bf33acfb 100644 --- a/src/Nethermind/Nethermind.Serialization.Rlp/KeyValueStoreRlpExtensions.cs +++ b/src/Nethermind/Nethermind.Serialization.Rlp/KeyValueStoreRlpExtensions.cs @@ -15,19 +15,19 @@ public static class KeyValueStoreRlpExtensions { [SkipLocalsInit] public static TItem? Get(this IReadOnlyKeyValueStore db, long blockNumber, ValueHash256 hash, IRlpStreamDecoder decoder, - LruCache cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class + ClockCache cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class { Span dbKey = stackalloc byte[40]; KeyValueStoreExtensions.GetBlockNumPrefixedKey(blockNumber, hash, dbKey); return Get(db, hash, dbKey, decoder, cache, rlpBehaviors, shouldCache); } - public static TItem? Get(this IReadOnlyKeyValueStore db, ValueHash256 key, IRlpStreamDecoder decoder, LruCache cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class + public static TItem? Get(this IReadOnlyKeyValueStore db, ValueHash256 key, IRlpStreamDecoder decoder, ClockCache 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(this IReadOnlyKeyValueStore db, long key, IRlpStreamDecoder? decoder, LruCache? cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class + public static TItem? Get(this IReadOnlyKeyValueStore db, long key, IRlpStreamDecoder? decoder, ClockCache? cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true) where TItem : class { ReadOnlySpan keyDb = key.ToBigEndianSpanWithoutLeadingZeros(out _); return Get(db, key, keyDb, decoder, cache, rlpBehaviors, shouldCache); @@ -38,10 +38,11 @@ public static class KeyValueStoreRlpExtensions TCacheKey cacheKey, ReadOnlySpan key, IRlpStreamDecoder decoder, - LruCache cache = null, + ClockCache cache = null, RlpBehaviors rlpBehaviors = RlpBehaviors.None, bool shouldCache = true ) where TItem : class + where TCacheKey : struct, IEquatable { TItem item = cache?.Get(cacheKey); if (item is null) diff --git a/src/Nethermind/Nethermind.State/Repositories/ChainLevelInfoRepository.cs b/src/Nethermind/Nethermind.State/Repositories/ChainLevelInfoRepository.cs index d04e496152e..101cdd90215 100644 --- a/src/Nethermind/Nethermind.State/Repositories/ChainLevelInfoRepository.cs +++ b/src/Nethermind/Nethermind.State/Repositories/ChainLevelInfoRepository.cs @@ -14,7 +14,7 @@ public class ChainLevelInfoRepository : IChainLevelInfoRepository private const int CacheSize = 64; private readonly object _writeLock = new(); - private readonly LruCache _blockInfoCache = new LruCache(CacheSize, CacheSize, "chain level infos"); + private readonly ClockCache _blockInfoCache = new(CacheSize); private readonly IDb _blockInfoDb;