Skip to content
Open
Show file tree
Hide file tree
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 @@ -175,7 +175,9 @@ private void Reset()
/// <returns>True if value has been set</returns>
protected bool TryGetCachedValue(in StorageCell storageCell, out byte[]? bytes)
{
if (_intraBlockCache.TryGetValue(storageCell, out StackList<int> stack))
// If the cache is completely empty (no writes or reads yet this transaction),
// skip hashing the 52-byte cell — TryGetValue would miss anyway.
if (_intraBlockCache.Count != 0 && _intraBlockCache.TryGetValue(storageCell, out StackList<int> stack))
Comment thread
svlachakis marked this conversation as resolved.
{
int lastChangeIndex = stack.Peek();
{
Expand Down
24 changes: 23 additions & 1 deletion src/Nethermind/Nethermind.State/PersistentStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public override void Reset(bool resetBlockChanges = true)
if (resetBlockChanges)
{
_storages.ResetAndClear();
_lastStorageAddress = null;
_lastStorage = null;
_toUpdateRoots.Clear();
}
}
Expand Down Expand Up @@ -260,19 +262,39 @@ private static void ReportMetrics(int writes, int skipped)
Db.Metrics.IncrementStorageTreeWrites(writes);
}

public void ClearStorageMap() => _storages.Clear();
public void ClearStorageMap()
{
_storages.Clear();
_lastStorageAddress = null;
_lastStorage = null;
}

// Consecutive SLOADs overwhelmingly hit the same contract; the one-entry memo removes a
// dictionary lookup from the per-SLOAD hot path. Cleared wherever _storages is cleared
// (the pooled PerContractState is returned there and must not be reachable).
private Address? _lastStorageAddress;
private PerContractState? _lastStorage;

private PerContractState GetOrCreateStorage(Address address)
{
if (_lastStorageAddress == address)
{
return _lastStorage!;
}

ref PerContractState? value = ref CollectionsMarshal.GetValueRefOrAddDefault(_storages, address, out bool exists);
if (!exists) value = PerContractState.Rent(address, this);
_lastStorageAddress = address;
_lastStorage = value;
return value;
}

public void WarmUp(in StorageCell storageCell, bool isEmpty)
{
if (!isEmpty)
{
LoadFromTree(in storageCell);
}
}

private ReadOnlySpan<byte> LoadFromTree(in StorageCell storageCell) =>
Expand Down
Loading