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

perf(persistence): reuse cursor for updating history indices #13622

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

hai-rise
Copy link
Contributor

@hai-rise hai-rise commented Jan 3, 2025

In our latest test load, save_blocks spends ~2.5% of its time creating and dropping new cursors in take_shard alone. Another ~15% is spent on related tx.delete and tx.put that internally also create a new cursor per call.

This PR minimizes the number of cursors per append_history_index from the number of changes (accounts or storage slots) * (2 + number of block shards) to only 1 cursor per table.

image

P/s: this is untested code but I hope it improves save_blocks by 3~5% for us.

where
T: Table<Value = BlockNumberList>,
{
let mut cursor = self.tx.cursor_read::<T>()?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This created a new cursor per account or storage slot change.

// delete old shard so new one can be inserted.
self.tx.delete::<T>(shard_key, None)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This created another cursor and re-seeked even when cursor is already on the needed key.

while let Some(list) = chunks.next() {
let highest_block_number = if chunks.peek().is_some() {
*list.last().expect("`chunks` does not return empty list")
} else {
// Insert last list with `u64::MAX`.
u64::MAX
};
self.tx.put::<T>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This created another cursor per chunk.

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

ty,

seems okay to be, pending @rkrasiuk @joshieDo

Comment on lines +842 to +840
let mut cursor = self.tx.cursor_write::<T>()?;
for (partial_key, indices) in index_updates {
let mut last_shard =
self.take_shard::<T>(sharded_key_factory(partial_key, u64::MAX))?;
self.take_shard::<T>(&mut cursor, sharded_key_factory(partial_key, u64::MAX))?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this makes sense @rkrasiuk

@mattsse mattsse added C-perf A change motivated by improving speed, memory usage or disk footprint A-db Related to the database labels Jan 3, 2025
Copy link
Member

@rkrasiuk rkrasiuk left a comment

Choose a reason for hiding this comment

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

makes sense to me too

while let Some(list) = chunks.next() {
let highest_block_number = if chunks.peek().is_some() {
*list.last().expect("`chunks` does not return empty list")
} else {
// Insert last list with `u64::MAX`.
u64::MAX
};
self.tx.put::<T>(
cursor.insert(
Copy link
Member

Choose a reason for hiding this comment

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

@shekhirin @joshieDo any difference between tx.put and cursor.insert?

Copy link
Collaborator

@joshieDo joshieDo Jan 8, 2025

Choose a reason for hiding this comment

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

when inserting multiple values, generally yes: #1130 (comment)

@mattsse mattsse added this pull request to the merge queue Jan 8, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 8, 2025
@mattsse
Copy link
Collaborator

mattsse commented Jan 8, 2025

@hai-rise mind rebasing this pr?

@mattsse mattsse merged commit ceaa3d3 into paradigmxyz:main Jan 9, 2025
41 of 43 checks passed
@hai-rise hai-rise deleted the reuse-cursor branch January 9, 2025 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-db Related to the database C-perf A change motivated by improving speed, memory usage or disk footprint
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants