Skip to content

Commit

Permalink
Use the empty account cache as cache (#6452)
Browse files Browse the repository at this point in the history
* Use the empty account cache as cache

* Rename
  • Loading branch information
benaadams authored Jan 4, 2024
1 parent a7cbe27 commit 61245df
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/Nethermind/Nethermind.State/StateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal class StateProvider
private const int StartCapacity = Resettable.StartCapacity;
private readonly ResettableDictionary<Address, Stack<int>> _intraBlockCache = new();
private readonly ResettableHashSet<Address> _committedThisRound = new();
private readonly HashSet<Address> _nullAccountReads = new();
// Only guarding against hot duplicates so filter doesn't need to be too big
// Note:
// False negatives are fine as they will just result in a overwrite set
Expand Down Expand Up @@ -508,7 +509,7 @@ public void Commit(IReleaseSpec releaseSpec, IWorldStateTracer stateTracer, bool
// because it was not committed yet it means that the just cache is the only state (so it was read only)
if (isTracing && change.ChangeType == ChangeType.JustCache)
{
_readsForTracing.Add(change.Address);
_nullAccountReads.Add(change.Address);
continue;
}

Expand Down Expand Up @@ -597,7 +598,7 @@ public void Commit(IReleaseSpec releaseSpec, IWorldStateTracer stateTracer, bool

if (isTracing)
{
foreach (Address nullRead in _readsForTracing)
foreach (Address nullRead in _nullAccountReads)
{
// // this may be enough, let us write tests
stateTracer.ReportAccountRead(nullRead);
Expand All @@ -606,7 +607,7 @@ public void Commit(IReleaseSpec releaseSpec, IWorldStateTracer stateTracer, bool

Resettable<Change>.Reset(ref _changes, ref _capacity, ref _currentPosition, StartCapacity);
_committedThisRound.Reset();
_readsForTracing.Clear();
_nullAccountReads.Clear();
_intraBlockCache.Reset();

if (isTracing)
Expand Down Expand Up @@ -687,10 +688,10 @@ private void SetState(Address address, Account? account)
_tree.Set(address, account);
}

private readonly HashSet<Address> _readsForTracing = new();

private Account? GetAndAddToCache(Address address)
{
if (_nullAccountReads.Contains(address)) return null;

Account? account = GetState(address);
if (account is not null)
{
Expand All @@ -699,7 +700,7 @@ private void SetState(Address address, Account? account)
else
{
// just for tracing - potential perf hit, maybe a better solution?
_readsForTracing.Add(address);
_nullAccountReads.Add(address);
}

return account;
Expand Down Expand Up @@ -803,7 +804,7 @@ public void Reset()
if (_logger.IsTrace) _logger.Trace("Clearing state provider caches");
_intraBlockCache.Reset();
_committedThisRound.Reset();
_readsForTracing.Clear();
_nullAccountReads.Clear();
_currentPosition = Resettable.EmptyPosition;
Array.Clear(_changes, 0, _changes.Length);
_needsStateRootUpdate = false;
Expand Down

0 comments on commit 61245df

Please sign in to comment.