Skip to content

perf(core): block snapshots with layers #2664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 126 commits into
base: main
Choose a base branch
from
Open

perf(core): block snapshots with layers #2664

wants to merge 126 commits into from

Conversation

edg-l
Copy link
Contributor

@edg-l edg-l commented May 5, 2025

Motivation

Adds block snapshots using disk and diff layers. Improving performance by reducing trie database queries.

Description

Heavily inspired by https://github.com/ethereum/go-ethereum/blob/master/core/state/snapshot/

Uses a base disk layer at block X, adding diff layers as blocks come at X + 1, X + 2, until the cap of max layers is reached, when that happens, flattening of layers is done, moving the bottom disk layer closer to the head block like a moving window.

When one queries for example for the account state at HEAD-4 block. The snapshot tree requests the snapshot at HEAD-4, then using a bloom filter which aggregates all accounts from previous diff layers, we can now if we skip to the disk layer directly or traverse the diff layers. Traversing the diff layers is faster than quering the database.

The disk layer has a flat database table mapping hashes to accounts and storages

Missing features:

  • Save diff layers to the database

Last load tests done:

pr commit: a8ca81f

macOS M3 36gb

pr load-test:

First blocks: 0.58, 0.55, 0.54

Last blocks 0.38, 0.37, 0.32

pr load-test-erc20:

First blocks: 1.04, 0.86, 0.83, 0.99, 0.99

Last blocks: 0.58, 0.58, 0.56

Main:

load-test:

First blocks: 0.38, 0.23, 0.23, 0.30, 0.27
Last blocks: 0.21, 0.20, 0.12

load-test-erc20:

First blocks: 0.60, 0.53, 0.47, 0.48, 0.58, 0.56, 0.54
Last blocks: 0.40, 0.40, 0.36

Copy link

github-actions bot commented May 5, 2025

Lines of code report

Total lines added: 1476
Total lines removed: 0
Total lines changed: 1476

Detailed view
+---------------------------------------------+-------+------+
| File                                        | Lines | Diff |
+---------------------------------------------+-------+------+
| ethrex/crates/blockchain/blockchain.rs      | 513   | +10  |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/api.rs                | 287   | +17  |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/error.rs              | 54    | +3   |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/lib.rs                | 13    | +2   |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/snapshot/cache.rs     | 16    | +16  |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/snapshot/difflayer.rs | 282   | +282 |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/snapshot/disklayer.rs | 105   | +105 |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/snapshot/error.rs     | 24    | +24  |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/snapshot/mod.rs       | 6     | +6   |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/snapshot/tree.rs      | 742   | +742 |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/store.rs              | 1394  | +102 |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/store_db/in_memory.rs | 655   | +31  |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/store_db/libmdbx.rs   | 1474  | +68  |
+---------------------------------------------+-------+------+
| ethrex/crates/storage/store_db/redb.rs      | 1318  | +68  |
+---------------------------------------------+-------+------+

@edg-l edg-l changed the title WIP: Snapshots feat(core): block snapshots with layers May 7, 2025
@edg-l edg-l changed the title feat(core): block snapshots with layers perf(core): block snapshots with layers May 7, 2025
Copy link

github-actions bot commented May 7, 2025

Benchmark for c144ed8

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 35.8±0.83ms 38.8±1.29ms +8.38%
Trie/cita-trie insert 1k 3.7±0.05ms 3.9±0.04ms +5.41%
Trie/ethrex-trie insert 10k 132.1±3.59ms 128.2±3.19ms -2.95%
Trie/ethrex-trie insert 1k 11.3±0.46ms 11.2±0.39ms -0.88%

Copy link

github-actions bot commented May 7, 2025

Benchmark Block Execution Results Comparison Against Main

Command Mean [s] Min [s] Max [s] Relative
base 178.872 ± 4.763 175.598 189.596 1.00
head 184.060 ± 2.756 180.037 189.569 1.03 ± 0.03

Copy link

github-actions bot commented May 7, 2025

Benchmark for 084ba79

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 33.7±1.29ms 41.0±2.02ms +21.66%
Trie/cita-trie insert 1k 3.7±0.03ms 3.7±0.11ms 0.00%
Trie/ethrex-trie insert 10k 138.8±3.52ms 134.8±3.78ms -2.88%
Trie/ethrex-trie insert 1k 11.3±0.13ms 11.3±0.31ms 0.00%

Copy link

github-actions bot commented May 7, 2025

Benchmark for 9cc084f

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 34.8±0.47ms 35.5±0.34ms +2.01%
Trie/cita-trie insert 1k 3.6±0.09ms 3.7±0.04ms +2.78%
Trie/ethrex-trie insert 10k 124.1±1.42ms 123.5±1.13ms -0.48%
Trie/ethrex-trie insert 1k 11.1±0.16ms 11.2±0.36ms +0.90%

Copy link

github-actions bot commented May 7, 2025

Benchmark for 23432e3

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 35.6±1.00ms 36.7±1.31ms +3.09%
Trie/cita-trie insert 1k 3.7±0.03ms 3.7±0.05ms 0.00%
Trie/ethrex-trie insert 10k 128.8±2.79ms 128.7±2.03ms -0.08%
Trie/ethrex-trie insert 1k 11.1±0.06ms 11.2±0.64ms +0.90%

Copy link

github-actions bot commented May 7, 2025

Benchmark for c6fa39c

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 36.7±1.94ms 35.8±0.68ms -2.45%
Trie/cita-trie insert 1k 3.7±0.02ms 3.7±0.04ms 0.00%
Trie/ethrex-trie insert 10k 130.7±2.53ms 130.1±2.32ms -0.46%
Trie/ethrex-trie insert 1k 11.0±0.03ms 11.4±0.47ms +3.64%

Copy link

github-actions bot commented May 8, 2025

Benchmark for 3275d89

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 39.5±2.00ms 34.6±1.35ms -12.41%
Trie/cita-trie insert 1k 3.7±0.05ms 3.7±0.08ms 0.00%
Trie/ethrex-trie insert 10k 134.2±2.33ms 131.9±2.14ms -1.71%
Trie/ethrex-trie insert 1k 11.3±0.13ms 11.1±0.05ms -1.77%

Copy link

github-actions bot commented May 8, 2025

Benchmark for 795388a

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 35.7±0.92ms 35.8±0.63ms +0.28%
Trie/cita-trie insert 1k 3.6±0.04ms 3.7±0.04ms +2.78%
Trie/ethrex-trie insert 10k 126.8±3.04ms 125.6±4.53ms -0.95%
Trie/ethrex-trie insert 1k 11.0±0.11ms 11.0±0.07ms 0.00%

Copy link

github-actions bot commented May 8, 2025

Benchmark for e40a6a8

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 38.3±2.56ms 38.9±2.58ms +1.57%
Trie/cita-trie insert 1k 3.7±0.10ms 3.7±0.04ms 0.00%
Trie/ethrex-trie insert 10k 135.3±2.41ms 135.1±3.31ms -0.15%
Trie/ethrex-trie insert 1k 11.3±0.12ms 11.4±0.40ms +0.88%

Copy link

Benchmark for 4fe7658

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 37.2±3.59ms 41.8±3.09ms +12.37%
Trie/cita-trie insert 1k 3.6±0.07ms 3.7±0.10ms +2.78%
Trie/ethrex-trie insert 10k 62.1±2.48ms 62.3±2.36ms +0.32%
Trie/ethrex-trie insert 1k 6.6±0.07ms 6.7±0.11ms +1.52%

@@ -547,6 +547,7 @@ impl Blockchain {
context.payload.header.requests_hash = context.requests_hash;
context.payload.header.gas_used = context.payload.header.gas_limit - context.remaining_gas;
context.account_updates = account_updates;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try not to include pretifier changes in such a big PR. This makes review more complicated.

Copy link

Benchmark for 7349e91

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 38.3±2.53ms 37.6±1.68ms -1.83%
Trie/cita-trie insert 1k 3.7±0.05ms 3.6±0.14ms -2.70%
Trie/ethrex-trie insert 10k 60.4±1.63ms 60.0±2.18ms -0.66%
Trie/ethrex-trie insert 1k 6.7±0.06ms 6.5±0.08ms -2.99%

if trie.get(&hash_address(address))?.is_none() {
if trie.get(hash_address(address))?.is_none() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to fix clippy warnings in a ad hoc PR and then rebase main to this one.

Copy link
Contributor

@fkrause98 fkrause98 May 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out 😄.

This wasn't happening in main, I figured out this was due to a change in the signature of Trie::get. Originally, this function received a &PathRLP (type alias to Vec<u8>), and this PR
changed said parameter to impl AsRef<u8>. I don't think this change is worthwile and also affected a lot of tests, incrementing the diff count and diff view unnecessarily. So I went and restored the original parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.