-
Notifications
You must be signed in to change notification settings - Fork 713
Expand file tree
/
Copy pathReadOnlySnapshotBundleBenchmark.cs
More file actions
429 lines (364 loc) · 16.5 KB
/
Copy pathReadOnlySnapshotBundleBenchmark.cs
File metadata and controls
429 lines (364 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// SPDX-FileCopyrightText: 2026 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Nethermind.Core;
using Nethermind.Core.Collections;
using Nethermind.Core.Crypto;
using Nethermind.Db;
using Nethermind.Evm.State;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.State.Flat;
using Nethermind.State.Flat.Persistence;
using Nethermind.State.Flat.PersistedSnapshots;
using Nethermind.State.Flat.ScopeProvider;
using Nethermind.Trie;
using FlatSnapshot = Nethermind.State.Flat.Snapshot;
namespace Nethermind.Benchmarks.State;
[MemoryDiagnoser]
[WarmupCount(3)]
[MinIterationCount(3)]
[MaxIterationCount(10)]
public class ReadOnlySnapshotBundleBenchmark
{
private ReadOnlySnapshotBundle _bundle = null!;
// Hit arrays — sampled from actually written data
private Address[] _hitAccounts = null!;
private (Address Address, UInt256 Slot)[] _hitSlots = null!;
private TreePath[] _hitShortPaths = null!;
private TreePath[] _hitLongPaths = null!;
private (Hash256 AddressHash, TreePath Path)[] _hitStorageNodes = null!;
// Same-account arrays — all slots/nodes from one address (hot-contract pattern)
private (Address Address, UInt256 Slot)[] _sameAccountSlots = null!;
private (Hash256 AddressHash, TreePath Path)[] _sameAccountStorageNodes = null!;
// Miss arrays — keys guaranteed absent from the snapshot
private Address[] _missAccounts = null!;
private (Address Address, UInt256 Slot)[] _missSlots = null!;
private TreePath[] _missShortPaths = null!;
private TreePath[] _missLongPaths = null!;
private (Hash256 AddressHash, TreePath Path)[] _missStorageNodes = null!;
private int _index;
private const int SnapshotCount = 8;
private const int ArraySize = 32;
[GlobalSetup]
public void Setup()
{
FlatDbConfig config = new();
ResourcePool resourcePool = new(config);
List<FlatSnapshot> allSnapshots = new(SnapshotCount);
StateId currentStateId = new(0, Keccak.EmptyTreeHash);
int totalAccountCount = 0;
int totalStorageAccountCount = 0;
int maxSlotsPerStorageAccount = 0;
// Track storage account ranges per snapshot for hit distribution
List<(int AddressStart, int StorageCount, int SlotsPerAccount)> storageRanges = [];
for (ulong block = 0; block < SnapshotCount; block++)
{
int multiplier = block < 6 ? 16 : 1;
int accountCount = 1000 * multiplier;
int storageAccountCount = 20 * multiplier;
int slotsPerStorageAccount = 100 * multiplier;
SnapshotPooledList prevSnapshots = new(allSnapshots.Count);
foreach (FlatSnapshot s in allSnapshots)
{
s.TryAcquire();
prevSnapshots.Add(s);
}
// Build ReadOnlySnapshotBundle from previously captured snapshots
ReadOnlySnapshotBundle readOnly = new(
prevSnapshots, new NoopPersistenceReader(), recordDetailedMetrics: false,
PersistedSnapshotStack.Empty());
NullTrieNodeCache cache = new();
SnapshotBundle bundle = new(
readOnly, cache, resourcePool, ResourcePool.Usage.MainBlockProcessing);
CapturingCommitTarget commitTarget = new();
FlatWorldStateScope scope = new(
currentStateId: currentStateId,
snapshotBundle: bundle,
codeDb: new NullCodeDb(),
commitTarget: commitTarget,
configuration: config,
trieCacheWarmer: new NoopTrieWarmer(),
logManager: NullLogManager.Instance);
int addressOffset = totalAccountCount;
// Pre-compute addresses in parallel (DeriveAddress involves Keccak)
Address[] addresses = new Address[accountCount];
int offset = addressOffset;
Parallel.For(0, accountCount, i =>
{
addresses[i] = DeriveAddress(offset + i + 1);
});
using (IWorldStateScopeProvider.IWorldStateWriteBatch batch =
scope.StartWriteBatch(accountCount))
{
// Phase 1 (sequential): set accounts and create storage write batches
IWorldStateScopeProvider.IStorageWriteBatch[] storageBatches =
new IWorldStateScopeProvider.IStorageWriteBatch[storageAccountCount];
for (int i = 0; i < accountCount; i++)
{
batch.Set(addresses[i], new Account(balance: (UInt256)(addressOffset + i + 1)));
if (i < storageAccountCount)
{
storageBatches[i] = batch.CreateStorageWriteBatch(addresses[i],
estimatedEntries: slotsPerStorageAccount);
}
}
// Phase 2 (parallel): fill storage slots — each FlatStorageTree is independent
int slots = slotsPerStorageAccount;
Parallel.For(0, storageAccountCount, i =>
{
IWorldStateScopeProvider.IStorageWriteBatch storageBatch = storageBatches[i];
for (int s = 0; s < slots; s++)
{
storageBatch.Set((UInt256)(ulong)(s + 1),
new byte[] { (byte)((s + 1) & 0xFF) });
}
storageBatch.Dispose();
});
}
scope.Commit(blockNumber: block + 1);
FlatSnapshot snapshot = commitTarget.LastSnapshot
?? throw new InvalidOperationException(
$"Block {block + 1}: Commit produced no snapshot");
snapshot.TryAcquire();
allSnapshots.Add(snapshot);
currentStateId = new StateId(block + 1, scope.RootHash);
storageRanges.Add((totalAccountCount + 1, storageAccountCount, slotsPerStorageAccount));
totalAccountCount += accountCount;
totalStorageAccountCount += storageAccountCount;
if (slotsPerStorageAccount > maxSlotsPerStorageAccount)
maxSlotsPerStorageAccount = slotsPerStorageAccount;
}
SnapshotPooledList finalSnapshots = new(allSnapshots.Count);
foreach (FlatSnapshot s in allSnapshots)
{
s.TryAcquire();
finalSnapshots.Add(s);
}
// Build final ReadOnlySnapshotBundle with all 8 snapshots
_bundle = new ReadOnlySnapshotBundle(
finalSnapshots, new NoopPersistenceReader(), recordDetailedMetrics: false,
PersistedSnapshotStack.Empty());
// --- Hit arrays ---
_hitAccounts = new Address[ArraySize];
int accountStep = Math.Max(1, totalAccountCount / ArraySize);
for (int i = 0; i < ArraySize; i++)
{
int accountIndex = (i * accountStep % totalAccountCount) + 1;
_hitAccounts[i] = DeriveAddress(accountIndex);
}
// Hit slots: spread across all snapshots so lookups hit different depth positions
_hitSlots = new (Address, UInt256)[ArraySize];
for (int i = 0; i < ArraySize; i++)
{
(int AddressStart, int StorageCount, int SlotsPerAccount) range = storageRanges[i % storageRanges.Count];
int storageAccountIndex = range.AddressStart + (i / storageRanges.Count % range.StorageCount);
UInt256 slot = (UInt256)(ulong)((i * 97 % range.SlotsPerAccount) + 1);
_hitSlots[i] = (DeriveAddress(storageAccountIndex), slot);
}
// Collect state/storage trie nodes from all snapshots
List<TreePath> shortPaths = new(ArraySize);
List<TreePath> longPaths = new(ArraySize);
List<(Hash256, TreePath)> storageNodesList = new(ArraySize);
foreach (FlatSnapshot snapshot in allSnapshots)
{
if (shortPaths.Count < ArraySize || longPaths.Count < ArraySize)
{
foreach (KeyValuePair<HashedKey<TreePath>, TrieNode> kv in snapshot.StateNodes)
{
if (shortPaths.Count < ArraySize && kv.Key.Key.Length <= 15)
shortPaths.Add(kv.Key.Key);
if (longPaths.Count < ArraySize && kv.Key.Key.Length > 15)
longPaths.Add(kv.Key.Key);
if (shortPaths.Count >= ArraySize && longPaths.Count >= ArraySize)
break;
}
}
if (storageNodesList.Count < ArraySize)
{
foreach (KeyValuePair<HashedKey<(Hash256, TreePath)>, TrieNode> kv in snapshot.StorageNodes)
{
storageNodesList.Add((kv.Key.Key.Item1, kv.Key.Key.Item2));
if (storageNodesList.Count >= ArraySize)
break;
}
}
}
_hitShortPaths = shortPaths.ToArray();
_hitLongPaths = longPaths.Count > 0 ? longPaths.ToArray() : shortPaths.ToArray();
_hitStorageNodes = storageNodesList.ToArray();
// --- Same-account arrays (hot-contract pattern) ---
Address sameAddr = DeriveAddress(1);
_sameAccountSlots = new (Address, UInt256)[ArraySize];
for (int i = 0; i < ArraySize; i++)
_sameAccountSlots[i] = (sameAddr, (UInt256)(ulong)(i + 1));
Hash256 sameAddrHash = Keccak.Compute(sameAddr.Bytes);
List<(Hash256, TreePath)> sameAccountNodesList = new(ArraySize);
foreach (FlatSnapshot snapshot in allSnapshots)
{
foreach (KeyValuePair<HashedKey<(Hash256, TreePath)>, TrieNode> kv in snapshot.StorageNodes)
{
if (kv.Key.Key.Item1 == sameAddrHash)
{
sameAccountNodesList.Add((kv.Key.Key.Item1, kv.Key.Key.Item2));
if (sameAccountNodesList.Count >= ArraySize)
break;
}
}
if (sameAccountNodesList.Count >= ArraySize) break;
}
_sameAccountStorageNodes = sameAccountNodesList.ToArray();
// --- Miss arrays ---
_missAccounts = new Address[ArraySize];
for (int i = 0; i < ArraySize; i++)
_missAccounts[i] = DeriveAddress(totalAccountCount + 200_001 + i);
_missSlots = new (Address, UInt256)[ArraySize];
for (int i = 0; i < ArraySize; i++)
{
Address storageAddr = DeriveAddress((i % 20) + 1);
UInt256 missSlot = (UInt256)(ulong)(maxSlotsPerStorageAccount + 100 + i);
_missSlots[i] = (storageAddr, missSlot);
}
_missShortPaths = new TreePath[ArraySize];
_missLongPaths = new TreePath[ArraySize];
for (int i = 0; i < ArraySize; i++)
{
Address nonExistent = DeriveAddress(totalAccountCount + 300_001 + i);
ValueHash256 addrHash = ValueKeccak.Compute(nonExistent.Bytes);
TreePath shortPath = TreePath.FromPath(addrHash.Bytes);
shortPath = shortPath.Truncate(15);
_missShortPaths[i] = shortPath;
_missLongPaths[i] = TreePath.FromPath(addrHash.Bytes);
}
_missStorageNodes = new (Hash256, TreePath)[ArraySize];
for (int i = 0; i < ArraySize; i++)
{
Address nonStorageAddr = DeriveAddress(totalAccountCount + 400_001 + i);
Hash256 addrHash = Keccak.Compute(nonStorageAddr.Bytes);
_missStorageNodes[i] = (addrHash, TreePath.Empty);
}
_index = 0;
// Verify hit arrays are populated
if (_hitAccounts.Length == 0)
throw new InvalidOperationException("Hit accounts array is empty");
if (_hitSlots.Length == 0)
throw new InvalidOperationException("Hit slots array is empty");
if (_hitShortPaths.Length == 0)
throw new InvalidOperationException("No short state trie paths found (Length <= 15)");
if (_hitStorageNodes.Length == 0)
throw new InvalidOperationException(
"No storage trie nodes found — storage tree commit may have failed");
if (_sameAccountStorageNodes.Length == 0)
throw new InvalidOperationException(
"No same-account storage trie nodes found for hot-contract pattern benchmark");
// Verify miss keys are actually absent
if (_bundle.GetAccount(_missAccounts[0]) is not null)
throw new InvalidOperationException(
"Miss account should not be found in snapshot bundle");
}
[Benchmark]
public Account GetAccount()
=> _bundle.GetAccount(_hitAccounts[_index++ % _hitAccounts.Length]);
[Benchmark]
public byte[] GetSlot()
{
(Address addr, UInt256 slot) = _hitSlots[_index++ % _hitSlots.Length];
return _bundle.GetSlot(addr, in slot, selfDestructStateIdx: -1);
}
[Benchmark]
public bool TryFindStateNodes_Short()
{
TreePath path = _hitShortPaths[_index++ % _hitShortPaths.Length];
return _bundle.TryFindStateNodes(in path, Keccak.Zero, out _);
}
[Benchmark]
public bool TryFindStateNodes_Long()
{
TreePath path = _hitLongPaths[_index++ % _hitLongPaths.Length];
return _bundle.TryFindStateNodes(in path, Keccak.Zero, out _);
}
[Benchmark]
public bool TryFindStorageNodes()
{
(Hash256 addrHash, TreePath path) = _hitStorageNodes[_index++ % _hitStorageNodes.Length];
return _bundle.TryFindStorageNodes(addrHash, in path, Keccak.Zero, out _);
}
[Benchmark]
public byte[] GetSlot_SameAccount()
{
(Address addr, UInt256 slot) = _sameAccountSlots[_index++ % _sameAccountSlots.Length];
return _bundle.GetSlot(addr, in slot, selfDestructStateIdx: -1);
}
[Benchmark]
public bool TryFindStorageNodes_SameAccount()
{
(Hash256 addrHash, TreePath path) =
_sameAccountStorageNodes[_index++ % _sameAccountStorageNodes.Length];
return _bundle.TryFindStorageNodes(addrHash, in path, Keccak.Zero, out _);
}
[Benchmark]
public Account GetAccount_Miss()
=> _bundle.GetAccount(_missAccounts[_index++ % _missAccounts.Length]);
[Benchmark]
public byte[] GetSlot_Miss()
{
(Address addr, UInt256 slot) = _missSlots[_index++ % _missSlots.Length];
return _bundle.GetSlot(addr, in slot, selfDestructStateIdx: -1);
}
[Benchmark]
public bool TryFindStateNodes_Short_Miss()
{
TreePath path = _missShortPaths[_index++ % _missShortPaths.Length];
return _bundle.TryFindStateNodes(in path, Keccak.Zero, out _);
}
[Benchmark]
public bool TryFindStateNodes_Long_Miss()
{
TreePath path = _missLongPaths[_index++ % _missLongPaths.Length];
return _bundle.TryFindStateNodes(in path, Keccak.Zero, out _);
}
[Benchmark]
public bool TryFindStorageNodes_Miss()
{
(Hash256 addrHash, TreePath path) =
_missStorageNodes[_index++ % _missStorageNodes.Length];
return _bundle.TryFindStorageNodes(addrHash, in path, Keccak.Zero, out _);
}
private static Address DeriveAddress(int index) =>
new(Keccak.Compute(Address.FromNumber((UInt256)(ulong)index).Bytes));
private sealed class NullTrieNodeCache : ITrieNodeCache
{
public bool TryGet(Hash256 address, in TreePath path, Hash256 hash, out TrieNode node)
{
node = null;
return false;
}
public void Add(TransientResource transientResource) { }
public void Clear() { }
}
private sealed class CapturingCommitTarget : IFlatCommitTarget
{
public FlatSnapshot LastSnapshot { get; private set; }
public TransientResource LastResource { get; private set; }
public void AddSnapshot(FlatSnapshot snapshot, TransientResource transientResource)
{
LastSnapshot = snapshot;
LastResource = transientResource;
}
}
private sealed class NullCodeDb : IWorldStateScopeProvider.ICodeDb
{
public byte[] GetCode(in ValueHash256 codeHash) => null;
public IWorldStateScopeProvider.ICodeSetter BeginCodeWrite()
=> NullCodeSetter.Instance;
private sealed class NullCodeSetter : IWorldStateScopeProvider.ICodeSetter
{
public static readonly NullCodeSetter Instance = new();
public void Set(in ValueHash256 codeHash, ReadOnlySpan<byte> code) { }
public void Dispose() { }
}
}
}