Skip to content

Commit

Permalink
Faster cache
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Feb 8, 2023
1 parent 46edb49 commit 502cfae
Show file tree
Hide file tree
Showing 24 changed files with 652 additions and 387 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public class TestTxPermissionsBlockchain : TestContractBlockchain
public PermissionBasedTxFilter PermissionBasedTxFilter { get; private set; }
public PermissionBasedTxFilter.Cache TxPermissionFilterCache { get; private set; }

public ICache<Keccak, UInt256> TransactionPermissionContractVersions { get; private set; }
public LruCache<Keccak, UInt256> TransactionPermissionContractVersions { get; private set; }

protected override BlockProcessor CreateBlockProcessor()
{
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.Blockchain/BlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public partial class BlockTree : IBlockTree

private const int CacheSize = 64;

private readonly ICache<Keccak, Block>
_blockCache = new LruCache<Keccak, Block>(CacheSize, CacheSize, "blocks");
private readonly LruCache<KeccakKey, Block>
_blockCache = new(CacheSize, CacheSize, "blocks");

private readonly ICache<Keccak, BlockHeader> _headerCache =
new LruCache<Keccak, BlockHeader>(CacheSize, CacheSize, "headers");
private readonly LruCache<KeccakKey, BlockHeader> _headerCache =
new(CacheSize, CacheSize, "headers");

private const int BestKnownSearchLimit = 256_000_000;

Expand All @@ -53,7 +53,7 @@ public partial class BlockTree : IBlockTree
private readonly IDb _blockInfoDb;
private readonly IDb _metadataDb;

private readonly ICache<Keccak, Block> _invalidBlocks =
private readonly LruCache<Keccak, Block> _invalidBlocks =
new LruCache<Keccak, Block>(128, 128, "invalid blocks");

private readonly BlockDecoder _blockDecoder = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PersistentReceiptStorage : IReceiptStorage
private static readonly ReceiptStorageDecoder StorageDecoder = ReceiptStorageDecoder.Instance;

private const int CacheSize = 64;
private readonly ICache<Keccak, TxReceipt[]> _receiptsCache = new LruCache<Keccak, TxReceipt[]>(CacheSize, CacheSize, "receipts");
private readonly LruCache<KeccakKey, TxReceipt[]> _receiptsCache = new (CacheSize, CacheSize, "receipts");

public PersistentReceiptStorage(IColumnsDb<ReceiptsColumns> receiptsDb, ISpecProvider specProvider, IReceiptsRecovery receiptsRecovery)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public class Cache
{
private const int MaxCacheSize = 10;

internal ICache<Keccak, long?> GasLimitCache { get; } = new LruCache<Keccak, long?>(MaxCacheSize, "BlockGasLimit");
internal LruCache<Keccak, long?> GasLimitCache { get; } = new LruCache<Keccak, long?>(MaxCacheSize, "BlockGasLimit");
}

public bool IsGasLimitValid(BlockHeader parentHeader, in long gasLimit, out long? expectedGasLimit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public abstract class VersionedContract<T> : IActivatedAtBlock where T : IVersio
private readonly IDictionary<UInt256, T> _versions;

private readonly IVersionedContract _versionSelectorContract;
private readonly ICache<Keccak, UInt256> _versionsCache;
private readonly LruCache<Keccak, UInt256> _versionsCache;
private readonly ILogger _logger;

protected VersionedContract(IDictionary<UInt256, T> versions, ICache<Keccak, UInt256> cache, long activation, ILogManager logManager)
protected VersionedContract(IDictionary<UInt256, T> versions, LruCache<Keccak, UInt256> cache, long activation, ILogManager logManager)
{
_versions = versions ?? throw new ArgumentNullException(nameof(versions));
_versionSelectorContract = versions.Values.Last();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class VersionedTransactionPermissionContract : VersionedContract<ITransac
Address contractAddress,
long activation,
IReadOnlyTxProcessorSource readOnlyTxProcessorSource,
ICache<Keccak, UInt256> cache,
LruCache<Keccak, UInt256> cache,
ILogManager logManager,
ISpecProvider specProvider)
: base(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AuRaNethermindApi : NethermindApi

public IValidatorStore? ValidatorStore { get; set; }

public ICache<Keccak, UInt256> TransactionPermissionContractVersions { get; }
public LruCache<Keccak, UInt256> TransactionPermissionContractVersions { get; }
= new LruCache<Keccak, UInt256>(
PermissionBasedTxFilter.Cache.MaxCacheSize,
nameof(TransactionPermissionContract));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public class Cache
{
public const int MaxCacheSize = 4096;

internal ICache<(Keccak ParentHash, Address Sender), (ITransactionPermissionContract.TxPermissions Permissions, bool ContractExists)> Permissions { get; } =
internal LruCache<(Keccak ParentHash, Address Sender), (ITransactionPermissionContract.TxPermissions Permissions, bool ContractExists)> Permissions { get; } =
new LruCache<(Keccak ParentHash, Address Sender), (ITransactionPermissionContract.TxPermissions Permissions, bool ContractExists)>(MaxCacheSize, "TxPermissions");
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Consensus.Clique/SnapshotManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ public class SnapshotManager : ISnapshotManager
private readonly IBlockTree _blockTree;
private readonly ICliqueConfig _cliqueConfig;
private readonly ILogger _logger;
private readonly ICache<Keccak, Address> _signatures;
private readonly LruCache<KeccakKey, Address> _signatures;
private readonly IEthereumEcdsa _ecdsa;
private IDb _blocksDb;
private ulong _lastSignersCount = 0;
private ICache<Keccak, Snapshot> _snapshotCache = new LruCache<Keccak, Snapshot>(Clique.InMemorySnapshots, "clique snapshots");
private LruCache<KeccakKey, Snapshot> _snapshotCache = new (Clique.InMemorySnapshots, "clique snapshots");

public SnapshotManager(ICliqueConfig cliqueConfig, IDb blocksDb, IBlockTree blockTree, IEthereumEcdsa ecdsa, ILogManager logManager)
{
_logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
_cliqueConfig = cliqueConfig ?? throw new ArgumentNullException(nameof(cliqueConfig));
_signatures = new LruCache<Keccak, Address>(Clique.InMemorySignatures, Clique.InMemorySignatures, "signatures");
_signatures = new (Clique.InMemorySignatures, Clique.InMemorySignatures, "signatures");
_ecdsa = ecdsa ?? throw new ArgumentNullException(nameof(ecdsa));
_blocksDb = blocksDb ?? throw new ArgumentNullException(nameof(blocksDb));
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class EthashSealValidator : ISealValidator
private readonly ITimestamper _timestamper;
private readonly ILogger _logger;

private readonly ICache<Keccak, bool> _sealCache = new LruCache<Keccak, bool>(2048, 2048, "ethash seals");
private readonly LruCache<KeccakKey, bool> _sealCache = new (2048, 2048, "ethash seals");
private const int SealValidationIntervalConstantComponent = 1024;
private const long AllowedFutureBlockTimeSeconds = 15;
private int _sealValidationInterval = SealValidationIntervalConstantComponent;
Expand Down
18 changes: 18 additions & 0 deletions src/Nethermind/Nethermind.Core/Caching/LinkedListNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;

namespace Nethermind.Core.Caching;

internal sealed class LinkedListNode<T>
{
internal LinkedListNode<T>? Next;
internal LinkedListNode<T>? Prev;
internal T Value;

public LinkedListNode(T value)
{
Value = value;
}
}
Loading

0 comments on commit 502cfae

Please sign in to comment.