Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ use reth_trie::{
TrieCursorIter,
},
updates::{StorageTrieUpdatesSorted, TrieUpdatesSorted},
HashedPostStateSorted, StoredNibbles, StoredNibblesSubKey, TrieChangeSetsEntry,
HashedPostState, HashedPostStateSorted, StoredNibbles, StoredNibblesSubKey,
TrieChangeSetsEntry,
};
use reth_trie_db::{
DatabaseAccountTrieCursor, DatabaseStorageTrieCursor, DatabaseTrieCursorFactory,
Expand Down Expand Up @@ -305,12 +306,14 @@ impl<TX: DbTx + DbTxMut + 'static, N: NodeTypesForProvider> DatabaseProvider<TX,

debug!(target: "providers::db", block_count = %blocks.len(), "Writing blocks and execution data to storage");

let mut aggregated_hashed_state = HashedPostState::default();

// TODO: Do performant / batched writes for each type of object
// instead of a loop over all blocks,
// meaning:
// * blocks
// * state
// * hashed state
// * hashed state (already done)
// * trie updates (cannot naively extend, need helper)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comment isn't accurate, we can collect a TrieUpdatesSorted and extend it with the trie updates from each block, and then only do a single write_trie_updates_sorted at the end, just like you're doing here with the hashed state. Might be worth trying out in this PR?

@duyquang6 duyquang6 Nov 14, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hi @mediocregopher, about comment, that is obsolete I think

Let me try benchmark and update to you again

@duyquang6 duyquang6 Nov 14, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank for suggestion, here is bench result

I use --engine.persistence-threshold 6, so it need persist 7 blocks per interval:
before:

Image

after:

Image

reduce from 125ms -> ~100ms
Note: with default --engine.persistence-threshold 2: flush 3 blocks interval, the difference is not much

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very nice, thanks for re-benching it 🙏

// * indices (already done basically)
// Insert the blocks
Expand All @@ -324,15 +327,19 @@ impl<TX: DbTx + DbTxMut + 'static, N: NodeTypesForProvider> DatabaseProvider<TX,
// Must be written after blocks because of the receipt lookup.
self.write_state(&execution_output, OriginalValuesKnown::No)?;

// insert hashes and intermediate merkle nodes
self.write_hashed_state(&Arc::unwrap_or_clone(hashed_state).into_sorted())?;
aggregated_hashed_state.extend(Arc::unwrap_or_clone(hashed_state));

// sort trie updates and insert changesets
let trie_updates_sorted = (*trie_updates).clone().into_sorted();
self.write_trie_changesets(block_number, &trie_updates_sorted, None)?;
self.write_trie_updates_sorted(&trie_updates_sorted)?;
}

// batch insert hashes and intermediate merkle nodes
Comment thread
duyquang6 marked this conversation as resolved.
Outdated
if !aggregated_hashed_state.is_empty() {
self.write_hashed_state(&aggregated_hashed_state.into_sorted())?;
}

// update history indices
self.update_history_indices(first_number..=last_block_number)?;

Expand Down