Benchmark cold storage prefetching via an SLOAD linked-list chain.
Deploy a contract with pre-populated linked-list storage where
each slot's value is the next key. Each execution transaction
performs back-to-back cold SLOADs with minimal compute between reads
— a worst-case prefetch scenario.
Keys are unpredictable without reading storage, so clients can
only prefetch via the BAL.
A shared pointer slot (_POINTER_SLOT) serializes
transactions: each tx reads its seed from the pointer and writes
back the final key, preventing parallel execution without a BAL.
The BAL makes all accessed slots prefetchable: the data slots
appear in storage_reads (SLOADs not also written per
EIP-7928), while _POINTER_SLOT appears in
storage_changes (both read and written).
Source code in tests/benchmark/compute/eip7928_block_level_access_lists/test_block_access_list.py
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593 | def test_prefetch_cold_storage(
benchmark_test: BenchmarkTestFiller,
pre: Alloc,
fork: Fork,
gas_benchmark_value: int,
tx_gas_limit: int,
) -> None:
"""
Benchmark cold storage prefetching via an SLOAD linked-list chain.
Deploy a contract with pre-populated linked-list storage where
each slot's value is the next key. Each execution transaction
performs back-to-back cold SLOADs with minimal compute between reads
— a worst-case prefetch scenario.
Keys are unpredictable without reading storage, so clients can
only prefetch via the BAL.
A shared pointer slot (``_POINTER_SLOT``) serializes
transactions: each tx reads its seed from the pointer and writes
back the final key, preventing parallel execution without a BAL.
The BAL makes all accessed slots prefetchable: the data slots
appear in ``storage_reads`` (SLOADs not also written per
EIP-7928), while ``_POINTER_SLOT`` appears in
``storage_changes`` (both read and written).
"""
intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()
intrinsic_gas = intrinsic_gas_calculator()
# Reconstruct body bytecode to extract gas components;
# _build_sload_chain_code only returns assembled code.
setup = Op.MSTORE(
0,
Op.SLOAD(_POINTER_SLOT, key_warm=False),
old_memory_size=0,
new_memory_size=32,
)
body = Op.MSTORE(
0,
Op.SLOAD(
Op.MLOAD(0),
key_warm=False,
),
old_memory_size=32,
new_memory_size=32,
)
cleanup = (
Op.SSTORE(
_POINTER_SLOT,
Op.MLOAD(0),
key_warm=True,
original_value=1,
current_value=1,
new_value=2,
)
+ Op.STOP
)
setup_gas = setup.gas_cost(fork)
cleanup_gas = cleanup.gas_cost(fork)
per_iter_gas, exit_overhead = _derive_loop_gas(body, fork)
reserve_gas = per_iter_gas + exit_overhead + cleanup_gas
runtime_code = _build_sload_chain_code(reserve_gas)
min_per_tx_gas = intrinsic_gas + setup_gas + reserve_gas
tx_gas_schedule = _derive_tx_schedule(
gas_benchmark_value, min_per_tx_gas, tx_gas_limit, TxDensity.GREEDY
)
num_exec_txs = len(tx_gas_schedule)
available_gas = tx_gas_schedule[0] - intrinsic_gas - setup_gas
estimated_slots_per_tx = max(1, available_gas // per_iter_gas)
# All txs share a single contract with disjoint slot ranges.
total_slots = (estimated_slots_per_tx + 1) * num_exec_txs
# Build linked-list storage: each tx continues where the
# previous left off via the shared pointer slot.
chain = _compute_hash_chain(1, total_slots)
storage = Storage()
storage[1] = chain[0]
for i in range(len(chain) - 1):
storage[chain[i]] = chain[i + 1]
storage[chain[-1]] = chain[-1]
# Nonzero initial value keeps the cleanup SSTORE a
# nonzero -> nonzero write (cheaper than 0 -> nonzero).
storage[_POINTER_SLOT] = 1
contract = pre.deploy_contract(
code=runtime_code,
storage=storage,
)
blocks: list[Block] = []
with TestPhaseManager.execution():
exec_txs = []
for gas_limit in tx_gas_schedule:
exec_txs.append(
Transaction(
to=contract,
gas_limit=gas_limit,
sender=pre.fund_eoa(),
)
)
blocks.append(Block(txs=exec_txs))
benchmark_test(blocks=blocks, skip_gas_used_validation=True)
|