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

Less Prewarmer work per tx (share scope for thread) #8172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -24,7 +24,7 @@ namespace Nethermind.Consensus.Processing;

public sealed class BlockCachePreWarmer(ReadOnlyTxProcessingEnvFactory envFactory, ISpecProvider specProvider, ILogManager logManager, PreBlockCaches? preBlockCaches = null) : IBlockCachePreWarmer
{
private readonly ObjectPool<IReadOnlyTxProcessorSource> _envPool = new DefaultObjectPool<IReadOnlyTxProcessorSource>(new ReadOnlyTxProcessingEnvPooledObjectPolicy(envFactory), Environment.ProcessorCount * 4);
private readonly ObjectPool<IReadOnlyTxProcessorSource> _envPool = new DefaultObjectPool<IReadOnlyTxProcessorSource>(new ReadOnlyTxProcessingEnvPooledObjectPolicy(envFactory), Environment.ProcessorCount * 2);
private readonly ILogger _logger = logManager.GetClassLogger<BlockCachePreWarmer>();

public Task PreWarmCaches(Block suggestedBlock, Hash256? parentStateRoot, IReleaseSpec spec, CancellationToken cancellationToken = default, params ReadOnlySpan<IHasAccessList> systemAccessLists)
Expand Down Expand Up @@ -135,22 +135,27 @@ private void WarmupTransactions(ParallelOptions parallelOptions, IReleaseSpec sp

try
{
ParallelUnbalancedWork.For<BlockState>(0, block.Transactions.Length, parallelOptions, new(this, block, stateRoot, spec), static (i, state) =>
BlockState blockState = new(this, block, stateRoot, spec);
ParallelUnbalancedWork.For(
0,
block.Transactions.Length,
parallelOptions,
blockState.InitThreadState,
static (i, state) =>
{
IReadOnlyTxProcessorSource env = state.PreWarmer._envPool.Get();
Transaction? tx = null;
try
{
// If the transaction has already been processed or being processed, exit early
if (state.Block.TransactionProcessed > i) return state;

tx = state.Block.Transactions[i];
using IReadOnlyTxProcessingScope scope = env.Build(state.StateRoot);

Address senderAddress = tx.SenderAddress!;
if (!scope.WorldState.AccountExists(senderAddress))
IWorldState worldState = state.Scope.WorldState;
if (!worldState.AccountExists(senderAddress))
{
scope.WorldState.CreateAccountIfNotExists(senderAddress, UInt256.Zero);
worldState.CreateAccountIfNotExists(senderAddress, UInt256.Zero);
}

UInt256 nonceDelta = UInt256.Zero;
Expand All @@ -164,14 +169,14 @@ private void WarmupTransactions(ParallelOptions parallelOptions, IReleaseSpec sp

if (!nonceDelta.IsZero)
{
scope.WorldState.IncrementNonce(senderAddress, nonceDelta);
worldState.IncrementNonce(senderAddress, nonceDelta);
}

if (state.Spec.UseTxAccessLists)
{
scope.WorldState.WarmUp(tx.AccessList); // eip-2930
worldState.WarmUp(tx.AccessList); // eip-2930
}
TransactionResult result = scope.TransactionProcessor.Warmup(tx, new BlockExecutionContext(state.BlockHeader, state.Spec), NullTxTracer.Instance);
TransactionResult result = state.Scope.TransactionProcessor.Warmup(tx, new BlockExecutionContext(state.BlockHeader, state.Spec), NullTxTracer.Instance);
if (state.PreWarmer._logger.IsTrace) state.PreWarmer._logger.Trace($"Finished pre-warming cache for tx[{i}] {tx.Hash} with {result}");
}
catch (Exception ex) when (ex is EvmException or OverflowException)
Expand All @@ -182,13 +187,10 @@ private void WarmupTransactions(ParallelOptions parallelOptions, IReleaseSpec sp
{
if (state.PreWarmer._logger.IsDebug) state.PreWarmer._logger.Error($"DEBUG/ERROR Error pre-warming cache {tx?.Hash}", ex);
}
finally
{
state.PreWarmer._envPool.Return(env);
}

return state;
});
},
BlockState.FinallyAction);
}
catch (OperationCanceledException)
{
Expand Down Expand Up @@ -345,13 +347,32 @@ private class ReadOnlyTxProcessingEnvPooledObjectPolicy(ReadOnlyTxProcessingEnvF
public bool Return(IReadOnlyTxProcessorSource obj) => true;
}

private readonly struct BlockState(BlockCachePreWarmer preWarmer, Block block, Hash256 stateRoot, IReleaseSpec spec)
private readonly struct BlockState(BlockCachePreWarmer preWarmer, Block block, Hash256 stateRoot, IReleaseSpec spec, IReadOnlyTxProcessorSource env = null, IReadOnlyTxProcessingScope scope = null)
{
public static Action<BlockState> FinallyAction { get; } = DisposeThreadState;

public readonly BlockCachePreWarmer PreWarmer = preWarmer;
public readonly Block Block = block;
public readonly Hash256 StateRoot = stateRoot;
public readonly IReleaseSpec Spec = spec;
public readonly BlockHeader BlockHeader => Block.Header;
public readonly IReadOnlyTxProcessorSource Env = env;
public readonly IReadOnlyTxProcessingScope Scope = scope;

public BlockState InitThreadState()
{
IReadOnlyTxProcessorSource env = PreWarmer._envPool.Get();
IReadOnlyTxProcessingScope scope = env.Build(StateRoot);
return new(PreWarmer, Block, StateRoot, Spec, env, scope);
}

public void Dispose()
{
Scope.Dispose();
PreWarmer._envPool.Return(Env);
}

private static void DisposeThreadState(BlockState state) => state.Dispose();
}
}

Loading