Skip to content

Commit

Permalink
Reduce ResettableDictionary lookups (#6373)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Dec 14, 2023
1 parent f62468e commit 36c1171
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
24 changes: 14 additions & 10 deletions src/Nethermind/Nethermind.State/PartialStorageProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ public void Restore(int snapshot)
for (int i = 0; i < _currentPosition - snapshot; i++)
{
Change change = _changes[_currentPosition - i];
if (_intraBlockCache[change!.StorageCell].Count == 1)
StackList<int> stack = _intraBlockCache[change!.StorageCell];
if (stack.Count == 1)
{
if (_changes[_intraBlockCache[change.StorageCell].Peek()]!.ChangeType == ChangeType.JustCache)
if (_changes[stack.Peek()]!.ChangeType == ChangeType.JustCache)
{
int actualPosition = _intraBlockCache[change.StorageCell].Pop();
int actualPosition = stack.Pop();
if (actualPosition != _currentPosition - i)
{
throw new InvalidOperationException($"Expected actual position {actualPosition} to be equal to {_currentPosition} - {i}");
Expand All @@ -112,15 +113,15 @@ public void Restore(int snapshot)
}
}

int forAssertion = _intraBlockCache[change.StorageCell].Pop();
int forAssertion = stack.Pop();
if (forAssertion != _currentPosition - i)
{
throw new InvalidOperationException($"Expected checked value {forAssertion} to be equal to {_currentPosition} - {i}");
}

_changes[_currentPosition - i] = null;

if (_intraBlockCache[change.StorageCell].Count == 0)
if (stack.Count == 0)
{
_intraBlockCache.Remove(change.StorageCell);
}
Expand Down Expand Up @@ -243,9 +244,9 @@ protected bool TryGetCachedValue(in StorageCell storageCell, out byte[]? bytes)
/// <param name="value">Value to set</param>
private void PushUpdate(in StorageCell cell, byte[] value)
{
SetupRegistry(cell);
StackList<int> stack = SetupRegistry(cell);
IncrementChangePosition();
_intraBlockCache[cell].Push(_currentPosition);
stack.Push(_currentPosition);
_changes[_currentPosition] = new Change(ChangeType.Update, cell, value);
}

Expand All @@ -261,12 +262,15 @@ protected void IncrementChangePosition()
/// Initialize the StackList at the storage cell position if needed
/// </summary>
/// <param name="cell"></param>
protected void SetupRegistry(in StorageCell cell)
protected StackList<int> SetupRegistry(in StorageCell cell)
{
if (!_intraBlockCache.ContainsKey(cell))
ref StackList<int>? value = ref _intraBlockCache.GetValueRefOrAddDefault(cell, out bool exists);
if (!exists)
{
_intraBlockCache[cell] = new StackList<int>();
value = new StackList<int>();
}

return value;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.State/PersistentStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ private byte[] LoadFromTree(in StorageCell storageCell)

private void PushToRegistryOnly(in StorageCell cell, byte[] value)
{
SetupRegistry(cell);
StackList<int> stack = SetupRegistry(cell);
IncrementChangePosition();
_intraBlockCache[cell].Push(_currentPosition);
stack.Push(_currentPosition);
_originalValues[cell] = value;
_changes[_currentPosition] = new Change(ChangeType.JustCache, cell, value);
}
Expand Down
38 changes: 22 additions & 16 deletions src/Nethermind/Nethermind.State/StateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.InteropServices;
using Nethermind.Core;
using Nethermind.Core.Caching;
using Nethermind.Core.Collections;
using Nethermind.Core.Crypto;
using Nethermind.Core.Resettables;
using Nethermind.Core.Specs;
Expand Down Expand Up @@ -370,11 +371,12 @@ public void Restore(int snapshot)
for (int i = 0; i < _currentPosition - snapshot; i++)
{
Change change = _changes[_currentPosition - i];
if (_intraBlockCache[change!.Address].Count == 1)
Stack<int> stack = _intraBlockCache[change!.Address];
if (stack.Count == 1)
{
if (change.ChangeType == ChangeType.JustCache)
{
int actualPosition = _intraBlockCache[change.Address].Pop();
int actualPosition = stack.Pop();
if (actualPosition != _currentPosition - i)
{
throw new InvalidOperationException($"Expected actual position {actualPosition} to be equal to {_currentPosition} - {i}");
Expand All @@ -387,13 +389,13 @@ public void Restore(int snapshot)
}

_changes[_currentPosition - i] = null; // TODO: temp, ???
int forChecking = _intraBlockCache[change.Address].Pop();
int forChecking = stack.Pop();
if (forChecking != _currentPosition - i)
{
throw new InvalidOperationException($"Expected checked value {forChecking} to be equal to {_currentPosition} - {i}");
}

if (_intraBlockCache[change.Address].Count == 0)
if (stack.Count == 0)
{
_intraBlockCache.Remove(change.Address);
}
Expand Down Expand Up @@ -510,7 +512,8 @@ public void Commit(IReleaseSpec releaseSpec, IWorldStateTracer stateTracer, bool
continue;
}

int forAssertion = _intraBlockCache[change.Address].Pop();
Stack<int> stack = _intraBlockCache[change.Address];
int forAssertion = stack.Pop();
if (forAssertion != _currentPosition - i)
{
throw new InvalidOperationException($"Expected checked value {forAssertion} to be equal to {_currentPosition} - {i}");
Expand Down Expand Up @@ -566,9 +569,9 @@ public void Commit(IReleaseSpec releaseSpec, IWorldStateTracer stateTracer, bool
{
if (_logger.IsTrace) _logger.Trace($" Commit remove {change.Address}");
bool wasItCreatedNow = false;
while (_intraBlockCache[change.Address].Count > 0)
while (stack.Count > 0)
{
int previousOne = _intraBlockCache[change.Address].Pop();
int previousOne = stack.Pop();
wasItCreatedNow |= _changes[previousOne].ChangeType == ChangeType.New;
if (wasItCreatedNow)
{
Expand Down Expand Up @@ -736,23 +739,23 @@ private void PushDelete(Address address)

private void Push(ChangeType changeType, Address address, Account? touchedAccount)
{
SetupCache(address);
Stack<int> stack = SetupCache(address);
if (changeType == ChangeType.Touch
&& _changes[_intraBlockCache[address].Peek()]!.ChangeType == ChangeType.Touch)
&& _changes[stack.Peek()]!.ChangeType == ChangeType.Touch)
{
return;
}

IncrementChangePosition();
_intraBlockCache[address].Push(_currentPosition);
stack.Push(_currentPosition);
_changes[_currentPosition] = new Change(changeType, address, touchedAccount);
}

private void PushNew(Address address, Account account)
{
SetupCache(address);
Stack<int> stack = SetupCache(address);
IncrementChangePosition();
_intraBlockCache[address].Push(_currentPosition);
stack.Push(_currentPosition);
_changes[_currentPosition] = new Change(ChangeType.New, address, account);
}

Expand All @@ -761,12 +764,15 @@ private void IncrementChangePosition()
Resettable<Change>.IncrementPosition(ref _changes, ref _capacity, ref _currentPosition);
}

private void SetupCache(Address address)
private Stack<int> SetupCache(Address address)
{
if (!_intraBlockCache.ContainsKey(address))
ref Stack<int>? value = ref _intraBlockCache.GetValueRefOrAddDefault(address, out bool exists);
if (!exists)
{
_intraBlockCache[address] = new Stack<int>();
value = new Stack<int>();
}

return value;
}

private enum ChangeType
Expand Down Expand Up @@ -823,7 +829,7 @@ public void CommitBranch()
// placeholder for the three level Commit->CommitBlock->CommitBranch
}

// used in EtheereumTests
// used in EthereumTests
internal void SetNonce(Address address, in UInt256 nonce)
{
_needsStateRootUpdate = true;
Expand Down

0 comments on commit 36c1171

Please sign in to comment.