-
Notifications
You must be signed in to change notification settings - Fork 2.2k
perf(persistence): optimize append_history_index with upsert #19825
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
base: main
Are you sure you want to change the base?
Conversation
|
This is actually a footgun with MDBX implementation: DUPSORT tables do not support upsert as you would expect. When upsert is called on a DUPSORT table it will actually only insert and leave any previous entry in place. reth/crates/storage/db/src/implementation/mdbx/cursor.rs Lines 240 to 243 in c57792c
So unless this PR only affects upsert on non-DUPSORT tables we cannot merge it. |
25ae39c to
425727b
Compare
Hi sir! Nice catch I've added a runtime assertion to prevent |
Ah nice, I thought the history tables were dupsort but you're right, they are not, my bad. Thanks for adding the assert 👍 |
mediocregopher
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM but would definitely like a sanity check from @shekhirin or @joshieDo
This reverts commit 6a16d17.
|
^ committed above to a wrong branch, sorry -.- |
|
I see, this makes sense! I think we can even improve it like that to avoid collecting diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs
index ca0564bbfa..b0baef3cd4 100644
--- a/crates/storage/provider/src/providers/database/provider.rs
+++ b/crates/storage/provider/src/providers/database/provider.rs
@@ -833,21 +833,19 @@ impl<TX: DbTxMut + DbTx + 'static, N: NodeTypes> DatabaseProvider<TX, N> {
}
// slow path: rechunk into multiple shards
- let all_indices: Vec<u64> = last_shard.iter().collect();
- let mut chunks = all_indices.chunks(sharded_key::NUM_OF_INDICES_IN_SHARD).peekable();
+ let chunks = last_shard.iter().chunks(sharded_key::NUM_OF_INDICES_IN_SHARD);
+ let mut chunks_peekable = chunks.into_iter().peekable();
- while let Some(list) = chunks.next() {
- let highest_block_number = if chunks.peek().is_some() {
- *list.last().expect("`chunks` does not return empty list")
+ while let Some(chunk) = chunks_peekable.next() {
+ let shard = BlockNumberList::new_pre_sorted(chunk);
+ let highest_block_number = if chunks_peekable.peek().is_some() {
+ shard.iter().next_back().expect("`chunks` does not return empty list")
} else {
// Insert last list with `u64::MAX`.
u64::MAX
};
- cursor.upsert(
- sharded_key_factory(partial_key, highest_block_number),
- &BlockNumberList::new_pre_sorted(list.iter().copied()),
- )?;
+ cursor.upsert(sharded_key_factory(partial_key, highest_block_number), &shard)?;
}
} |
Thanks! this should be more efficient since we avoid the intermediate allocation, updated |
Summary
append_history_indexProfiling
append_history_index:collect+delete_current+IntegerList::new_presortedcursor contribute dominate onappend_history_indexChanges
delete_currentoperation hotpathBefore
After
Profile result:
