Skip to content

Commit 5297bba

Browse files
authored
chore: cache tips (#15996)
Caching the chain tips to not read from storage multiple times. Very small reductions, but 500 here and there also helps.
1 parent f8da478 commit 5297bba

File tree

5 files changed

+97
-89
lines changed

5 files changed

+97
-89
lines changed

l1-contracts/gas_benchmark.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@
1616

1717
| Function | Avg | Max |
1818
|----------------------|---------|---------|
19-
| propose | 180,182 | 195,380 |
20-
| setupEpoch | 40,785 | 103,864 |
21-
| submitEpochRootProof | 799,356 | 819,960 |
19+
| propose | 192,119 | 207,397 |
20+
| setupEpoch | 40,849 | 108,446 |
21+
| submitEpochRootProof | 813,442 | 834,067 |
2222

23-
**Avg Gas Cost per Second**: 3,572.3 gas/second
23+
**Avg Gas Cost per Second**: 3,781.1 gas/second
2424
*Epoch duration*: 0h 48m 0s
2525

2626
### Validators (IGNITION)
2727

2828
| Function | Avg | Max |
2929
|----------------------|---------|---------|
30-
| propose | 284,434 | 300,076 |
31-
| setupEpoch | 48,311 | 354,729 |
32-
| submitEpochRootProof | 799,356 | 819,960 |
33-
| aggregate3 | 331,654 | 351,870 |
30+
| propose | 250,072 | 265,515 |
31+
| setupEpoch | 48,233 | 354,577 |
32+
| submitEpochRootProof | 923,880 | 944,511 |
33+
| aggregate3 | 297,537 | 316,779 |
3434

35-
**Avg Gas Cost per Second**: 5,312.4 gas/second
35+
**Avg Gas Cost per Second**: 4,826.2 gas/second
3636
*Epoch duration*: 0h 48m 0s
3737

3838

@@ -52,22 +52,22 @@
5252

5353
| Function | Avg | Max |
5454
|----------------------|---------|---------|
55-
| propose | 219,022 | 235,012 |
56-
| setupEpoch | 41,912 | 106,381 |
57-
| submitEpochRootProof | 671,999 | 710,831 |
55+
| propose | 230,021 | 246,987 |
56+
| setupEpoch | 42,024 | 110,967 |
57+
| submitEpochRootProof | 686,752 | 725,612 |
5858

59-
**Avg Gas Cost per Second**: 7,287.0 gas/second
59+
**Avg Gas Cost per Second**: 7,618.2 gas/second
6060
*Epoch duration*: 0h 19m 12s
6161

6262
### Validators (Alpha)
6363

6464
| Function | Avg | Max |
6565
|----------------------|---------|---------|
66-
| propose | 419,406 | 435,627 |
67-
| setupEpoch | 61,658 | 600,014 |
68-
| submitEpochRootProof | 671,999 | 710,831 |
69-
| aggregate3 | 471,485 | 495,734 |
66+
| propose | 337,105 | 355,679 |
67+
| setupEpoch | 61,573 | 599,674 |
68+
| submitEpochRootProof | 894,446 | 932,423 |
69+
| aggregate3 | 389,549 | 411,347 |
7070

71-
**Avg Gas Cost per Second**: 12,870.4 gas/second
71+
**Avg Gas Cost per Second**: 10,970.3 gas/second
7272
*Epoch duration*: 0h 19m 12s
7373

l1-contracts/gas_benchmark_results.json

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,109 +3,109 @@
33
"no_validators": {
44
"propose": {
55
"calls": 100,
6-
"min": 172938,
7-
"mean": 180182,
8-
"median": 179484,
9-
"max": 195380
6+
"min": 184955,
7+
"mean": 192119,
8+
"median": 191501,
9+
"max": 207397
1010
},
1111
"setupEpoch": {
1212
"calls": 100,
13-
"min": 37790,
14-
"mean": 40785,
15-
"median": 39790,
16-
"max": 103864
13+
"min": 37714,
14+
"mean": 40849,
15+
"median": 39714,
16+
"max": 108446
1717
},
1818
"submitEpochRootProof": {
1919
"calls": 2,
20-
"min": 778752,
21-
"mean": 799356,
22-
"median": 799356,
23-
"max": 819960
20+
"min": 792817,
21+
"mean": 813442,
22+
"median": 813442,
23+
"max": 834067
2424
}
2525
},
2626
"validators": {
2727
"propose": {
2828
"calls": 100,
29-
"min": 277179,
30-
"mean": 284434,
31-
"median": 283725,
32-
"max": 300076
29+
"min": 240190,
30+
"mean": 250072,
31+
"median": 250717,
32+
"max": 265515
3333
},
3434
"setupEpoch": {
3535
"calls": 100,
36-
"min": 37790,
37-
"mean": 48311,
38-
"median": 39790,
39-
"max": 354729
36+
"min": 37714,
37+
"mean": 48233,
38+
"median": 39714,
39+
"max": 354577
4040
},
4141
"submitEpochRootProof": {
4242
"calls": 2,
43-
"min": 778752,
44-
"mean": 799356,
45-
"median": 799356,
46-
"max": 819960
43+
"min": 903249,
44+
"mean": 923880,
45+
"median": 923880,
46+
"max": 944511
4747
},
4848
"aggregate3": {
4949
"calls": 100,
50-
"min": 324429,
51-
"mean": 331654,
52-
"median": 326830,
53-
"max": 351870
50+
"min": 285659,
51+
"mean": 297537,
52+
"median": 294826,
53+
"max": 316779
5454
}
5555
}
5656
},
5757
"alpha": {
5858
"no_validators": {
5959
"propose": {
6060
"calls": 100,
61-
"min": 209076,
62-
"mean": 219022,
63-
"median": 219062,
64-
"max": 235012
61+
"min": 221051,
62+
"mean": 230021,
63+
"median": 229305,
64+
"max": 246987
6565
},
6666
"setupEpoch": {
6767
"calls": 100,
68-
"min": 37790,
69-
"mean": 41912,
70-
"median": 39790,
71-
"max": 106381
68+
"min": 37714,
69+
"mean": 42024,
70+
"median": 39714,
71+
"max": 110967
7272
},
7373
"submitEpochRootProof": {
7474
"calls": 3,
75-
"min": 652535,
76-
"mean": 671999,
77-
"median": 652631,
78-
"max": 710831
75+
"min": 667274,
76+
"mean": 686752,
77+
"median": 667370,
78+
"max": 725612
7979
}
8080
},
8181
"validators": {
8282
"propose": {
8383
"calls": 100,
84-
"min": 409332,
85-
"mean": 419406,
86-
"median": 419268,
87-
"max": 435627
84+
"min": 322686,
85+
"mean": 337105,
86+
"median": 336752,
87+
"max": 355679
8888
},
8989
"setupEpoch": {
9090
"calls": 100,
91-
"min": 37790,
92-
"mean": 61658,
93-
"median": 39790,
94-
"max": 600014
91+
"min": 37714,
92+
"mean": 61573,
93+
"median": 39714,
94+
"max": 599674
9595
},
9696
"submitEpochRootProof": {
9797
"calls": 3,
98-
"min": 652535,
99-
"mean": 671999,
100-
"median": 652631,
101-
"max": 710831
98+
"min": 874205,
99+
"mean": 894446,
100+
"median": 876710,
101+
"max": 932423
102102
},
103103
"aggregate3": {
104104
"calls": 100,
105-
"min": 454901,
106-
"mean": 471485,
107-
"median": 472493,
108-
"max": 495734
105+
"min": 366071,
106+
"mean": 389549,
107+
"median": 388952,
108+
"max": 411347
109109
}
110110
}
111111
}

l1-contracts/src/core/libraries/rollup/ProposeLib.sol

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct InterimProposeValues {
4747
bytes32 attestationsHash;
4848
bytes32 payloadDigest;
4949
Epoch currentEpoch;
50+
bool isFirstBlockOfEpoch;
5051
}
5152

5253
/**
@@ -136,13 +137,16 @@ library ProposeLib {
136137
);
137138

138139
RollupStore storage rollupStore = STFLib.getStorage();
139-
uint256 blockNumber = rollupStore.tips.getPendingBlockNumber() + 1;
140+
CompressedChainTips tips = rollupStore.tips;
141+
142+
uint256 blockNumber = tips.getPendingBlockNumber() + 1;
143+
tips = tips.updatePendingBlockNumber(blockNumber);
140144

141145
// Blob commitments are collected and proven per root rollup proof (=> per epoch), so we need to know whether we are at the epoch start:
142-
bool isFirstBlockOfEpoch =
146+
v.isFirstBlockOfEpoch =
143147
v.currentEpoch > STFLib.getEpochForBlock(blockNumber - 1) || blockNumber == 1;
144148
bytes32 blobCommitmentsHash = BlobLib.calculateBlobCommitmentsHash(
145-
STFLib.getBlobCommitmentsHash(blockNumber - 1), v.blobCommitments, isFirstBlockOfEpoch
149+
STFLib.getBlobCommitmentsHash(blockNumber - 1), v.blobCommitments, v.isFirstBlockOfEpoch
146150
);
147151

148152
FeeHeader memory feeHeader = FeeLib.computeFeeHeader(
@@ -156,7 +160,7 @@ library ProposeLib {
156160
// Compute attestationsHash from the attestations
157161
v.attestationsHash = keccak256(abi.encode(_attestations));
158162

159-
rollupStore.tips = rollupStore.tips.updatePendingBlockNumber(blockNumber);
163+
rollupStore.tips = tips;
160164
rollupStore.archives[blockNumber] = _args.archive;
161165
STFLib.setTempBlockLog(
162166
blockNumber,

l1-contracts/src/core/libraries/rollup/STFLib.sol

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,15 @@ library STFLib {
8484

8585
function prune() internal {
8686
RollupStore storage rollupStore = STFLib.getStorage();
87-
uint256 pending = rollupStore.tips.getPendingBlockNumber();
87+
CompressedChainTips tips = rollupStore.tips;
88+
uint256 pending = tips.getPendingBlockNumber();
8889

8990
// @note We are not deleting the blocks, but we are "winding back" the pendingTip to the last block that was proven.
9091
// We can do because any new block proposed will overwrite a previous block in the block log,
9192
// so no values should "survive".
9293
// People must therefore read the chain using the pendingTip as a boundary.
93-
uint256 proven = rollupStore.tips.getProvenBlockNumber();
94-
rollupStore.tips = rollupStore.tips.updatePendingBlockNumber(proven);
94+
uint256 proven = tips.getProvenBlockNumber();
95+
rollupStore.tips = tips.updatePendingBlockNumber(proven);
9596

9697
emit IRollupCore.PrunedPending(proven, pending);
9798
}
@@ -160,9 +161,9 @@ library STFLib {
160161

161162
function getEffectivePendingBlockNumber(Timestamp _timestamp) internal view returns (uint256) {
162163
RollupStore storage rollupStore = STFLib.getStorage();
163-
return STFLib.canPruneAtTime(_timestamp)
164-
? rollupStore.tips.getProvenBlockNumber()
165-
: rollupStore.tips.getPendingBlockNumber();
164+
CompressedChainTips tips = rollupStore.tips;
165+
return
166+
STFLib.canPruneAtTime(_timestamp) ? tips.getProvenBlockNumber() : tips.getPendingBlockNumber();
166167
}
167168

168169
function getEpochForBlock(uint256 _blockNumber) internal view returns (Epoch) {
@@ -176,11 +177,14 @@ library STFLib {
176177

177178
function canPruneAtTime(Timestamp _ts) internal view returns (bool) {
178179
RollupStore storage rollupStore = STFLib.getStorage();
179-
if (rollupStore.tips.getPendingBlockNumber() == rollupStore.tips.getProvenBlockNumber()) {
180+
181+
CompressedChainTips tips = rollupStore.tips;
182+
183+
if (tips.getPendingBlockNumber() == tips.getProvenBlockNumber()) {
180184
return false;
181185
}
182186

183-
Epoch oldestPendingEpoch = getEpochForBlock(rollupStore.tips.getProvenBlockNumber() + 1);
187+
Epoch oldestPendingEpoch = getEpochForBlock(tips.getProvenBlockNumber() + 1);
184188
Epoch currentEpoch = _ts.epochFromTimestamp();
185189

186190
return !oldestPendingEpoch.isAcceptingProofsAtEpoch(currentEpoch);

l1-contracts/test/benchmark/happy.t.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,10 @@ contract BenchmarkRollupTest is FeeModelTestPoints, DecoderBase {
430430
});
431431
multicall.aggregate3(calls);
432432
} else {
433+
CommitteeAttestations memory attestations = SignatureLib.packAttestations(b.attestations);
434+
433435
vm.prank(proposer);
434-
rollup.propose(
435-
b.proposeArgs, SignatureLib.packAttestations(b.attestations), b.signers, b.blobInputs
436-
);
436+
rollup.propose(b.proposeArgs, attestations, b.signers, b.blobInputs);
437437
}
438438

439439
nextSlot = nextSlot + Slot.wrap(1);

0 commit comments

Comments
 (0)