diff --git a/src/Nethermind/Nethermind.State/PartialStorageProviderBase.cs b/src/Nethermind/Nethermind.State/PartialStorageProviderBase.cs index b47e0d36c96..1ca8de11af6 100644 --- a/src/Nethermind/Nethermind.State/PartialStorageProviderBase.cs +++ b/src/Nethermind/Nethermind.State/PartialStorageProviderBase.cs @@ -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 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}"); @@ -112,7 +113,7 @@ 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}"); @@ -120,7 +121,7 @@ public void Restore(int snapshot) _changes[_currentPosition - i] = null; - if (_intraBlockCache[change.StorageCell].Count == 0) + if (stack.Count == 0) { _intraBlockCache.Remove(change.StorageCell); } @@ -243,9 +244,9 @@ protected bool TryGetCachedValue(in StorageCell storageCell, out byte[]? bytes) /// Value to set private void PushUpdate(in StorageCell cell, byte[] value) { - SetupRegistry(cell); + StackList stack = SetupRegistry(cell); IncrementChangePosition(); - _intraBlockCache[cell].Push(_currentPosition); + stack.Push(_currentPosition); _changes[_currentPosition] = new Change(ChangeType.Update, cell, value); } @@ -261,12 +262,15 @@ protected void IncrementChangePosition() /// Initialize the StackList at the storage cell position if needed /// /// - protected void SetupRegistry(in StorageCell cell) + protected StackList SetupRegistry(in StorageCell cell) { - if (!_intraBlockCache.ContainsKey(cell)) + ref StackList? value = ref _intraBlockCache.GetValueRefOrAddDefault(cell, out bool exists); + if (!exists) { - _intraBlockCache[cell] = new StackList(); + value = new StackList(); } + + return value; } /// diff --git a/src/Nethermind/Nethermind.State/PersistentStorageProvider.cs b/src/Nethermind/Nethermind.State/PersistentStorageProvider.cs index 723bdbf105f..f4180447dc7 100644 --- a/src/Nethermind/Nethermind.State/PersistentStorageProvider.cs +++ b/src/Nethermind/Nethermind.State/PersistentStorageProvider.cs @@ -252,9 +252,9 @@ private byte[] LoadFromTree(in StorageCell storageCell) private void PushToRegistryOnly(in StorageCell cell, byte[] value) { - SetupRegistry(cell); + StackList stack = SetupRegistry(cell); IncrementChangePosition(); - _intraBlockCache[cell].Push(_currentPosition); + stack.Push(_currentPosition); _originalValues[cell] = value; _changes[_currentPosition] = new Change(ChangeType.JustCache, cell, value); } diff --git a/src/Nethermind/Nethermind.State/StateProvider.cs b/src/Nethermind/Nethermind.State/StateProvider.cs index 2e7ebf9d8d2..588865e6eff 100644 --- a/src/Nethermind/Nethermind.State/StateProvider.cs +++ b/src/Nethermind/Nethermind.State/StateProvider.cs @@ -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; @@ -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 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}"); @@ -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); } @@ -510,7 +512,8 @@ public void Commit(IReleaseSpec releaseSpec, IWorldStateTracer stateTracer, bool continue; } - int forAssertion = _intraBlockCache[change.Address].Pop(); + Stack 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}"); @@ -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) { @@ -736,23 +739,23 @@ private void PushDelete(Address address) private void Push(ChangeType changeType, Address address, Account? touchedAccount) { - SetupCache(address); + Stack 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 stack = SetupCache(address); IncrementChangePosition(); - _intraBlockCache[address].Push(_currentPosition); + stack.Push(_currentPosition); _changes[_currentPosition] = new Change(ChangeType.New, address, account); } @@ -761,12 +764,15 @@ private void IncrementChangePosition() Resettable.IncrementPosition(ref _changes, ref _capacity, ref _currentPosition); } - private void SetupCache(Address address) + private Stack SetupCache(Address address) { - if (!_intraBlockCache.ContainsKey(address)) + ref Stack? value = ref _intraBlockCache.GetValueRefOrAddDefault(address, out bool exists); + if (!exists) { - _intraBlockCache[address] = new Stack(); + value = new Stack(); } + + return value; } private enum ChangeType @@ -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;