Commit 588faf9
feat(eth-sparse-mpt): add ~3x faster v3 state-root implementation and harness tools (#887)
## 📝 Summary
I will state upfront that this code is almost entirely written with
Codex. However, the results seem promising and it shares much overlap
with the existing v2 state root anyway.
I asked Codex to setup a harness that measured performance across
different depths of cache warmth and compared state root implementations
to each other and Reth's for correctness. Then I asked it to optimize v2
and implement a v3 state root. This was effective because the agent
could simply iterate if it noticed it had the wrong root or if it was
slower. Finally, I ran the harness for 1000+ blocks and the new v3
always produced the same root as v2 and reth, so I have some confidence
in the implementation. You can find the testing scripts in this PR,
specifically `correct_and_bench.rs`. Note that these require unwinding a
reth node block by block (thank you Vitaly for this trick).
The results are a significant speedup relative to v2. Over 1,000 blocks:
| Scope | v2 p50 | v3 p50 | v2 p99 | v3 p99 | v2 mean | v3 mean |
|---|---:|---:|---:|---:|---:|---:|
| Overall (excluding warm=100, n=3213) | 3.01 | 0.88 | 4.71 | 1.87 |
3.19 | 0.97 |
| Warm % | v2 p50 | v3 p50 | p50 speedup | v2 p99 | v3 p99 | p99 speedup
| v2 mean | v3 mean | mean speedup |
|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|
| 70 | 4.16 | 1.09 | 3.82x | 5.96 | 2.22 | 2.68x | 4.32 | 1.21 | 3.58x |
| 80 | 3.05 | 0.94 | 3.26x | 4.75 | 1.93 | 2.46x | 3.23 | 1.03 | 3.14x |
| 90 | 1.83 | 0.61 | 3.00x | 3.41 | 1.45 | 2.35x | 2.00 | 0.68 | 2.95x |
Here's a moving average:
<img width="2160" height="1620" alt="image"
src="https://github.com/user-attachments/assets/2e8778d0-63c3-4d72-bb17-f0a442c7af6b"
/>
The one notable regression is that in ~2.5% of blocks there were v3 p99
cases that were greater than v2. On average these regressions added
1.89ms, and the worst observed was 23.9ms for a single block.
## 💡 Motivation and Context
Calculating the state root is in the hot path for a new block so making
it faster lowers latency for all our users.
---
## ✅ I have completed the following steps:
* [x] Run `make lint`
* [x] Run `make test`
* [x] Added tests (if applicable)
---------
Co-authored-by: Vitalii Drohan <vitaliy.drogan@gmail.com>1 parent 4c12965 commit 588faf9
17 files changed
Lines changed: 5818 additions & 13 deletions
File tree
- crates/eth-sparse-mpt
- benches
- scripts
- src
- bin
- v2/trie
- v_experimental
- trie
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
93 | 94 | | |
94 | 95 | | |
95 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
25 | 29 | | |
26 | 30 | | |
| 31 | + | |
| 32 | + | |
27 | 33 | | |
| 34 | + | |
28 | 35 | | |
29 | 36 | | |
| 37 | + | |
30 | 38 | | |
31 | 39 | | |
32 | 40 | | |
| |||
48 | 56 | | |
49 | 57 | | |
50 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
51 | 69 | | |
52 | 70 | | |
53 | 71 | | |
| |||
71 | 89 | | |
72 | 90 | | |
73 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
Lines changed: 60 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments