Skip to content
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

feat(core): batch heavy write operations #1358

Merged
merged 14 commits into from
Dec 10, 2024
Merged

Conversation

jrchatruc
Copy link
Collaborator

@jrchatruc jrchatruc commented Nov 29, 2024

Motivation

This PR batches the three current heaviest db write operations on the node (add_block, store_receipts, and commit_node) so that a single db transaction is used for them. This reduces the time spent flushing to disc with msync (on libmdbx) or fsync (on redb) from around ~40-50% to ~0.35%.

Non-batched version:
flamegraph_not_batched

Batched version (the msync on this flamegraph is one of the tiny red bars to the left):
flamegraph_batched

Description

@jrchatruc jrchatruc marked this pull request as ready for review November 29, 2024 21:16
@jrchatruc jrchatruc requested a review from a team as a code owner November 29, 2024 21:16
@jrchatruc jrchatruc force-pushed the batch-heavy-write-operations branch from c310f7f to 67590b7 Compare November 29, 2024 23:01
@jrchatruc jrchatruc force-pushed the batch-heavy-write-operations branch from 67590b7 to 4dec142 Compare November 29, 2024 23:01
@@ -14,4 +14,6 @@ use crate::error::TrieError;
pub trait TrieDB {
fn get(&self, key: Vec<u8>) -> Result<Option<Vec<u8>>, TrieError>;
fn put(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), TrieError>;
// fn put_batch(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), TrieError>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: maybe remove this ?

@@ -25,4 +25,14 @@ impl TrieDB for InMemoryTrieDB {
self.inner.lock().unwrap().insert(key, value);
Ok(())
}

fn put_batch(&self, key_values: Vec<(Vec<u8>, Vec<u8>)>) -> Result<(), TrieError> {
let mut db = self.inner.lock().unwrap();
Copy link
Contributor

@fkrause98 fkrause98 Dec 4, 2024

Choose a reason for hiding this comment

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

Don't unwrap here, let's return an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed unwraps here 4845ce8

@jrchatruc jrchatruc added this pull request to the merge queue Dec 10, 2024
Merged via the queue into main with commit c9d0e48 Dec 10, 2024
12 checks passed
@jrchatruc jrchatruc deleted the batch-heavy-write-operations branch December 10, 2024 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants