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

Optimizes AppendVec::scan_pubkeys() when using file io #2077

Merged
merged 3 commits into from
Jul 11, 2024

Conversation

brooksprumo
Copy link

@brooksprumo brooksprumo commented Jul 10, 2024

Problem

As noted in #1394 (comment), when using file i/o to access append vecs, the scan_pubkeys() implementation is not optimal. It currently calls scan_accounts(), which will copy all the account fields—including the data—even though only the pubkey is needed.

We call scan_pubkeys() from clean inside of construct_candidate_clean_keys(), so having a more-optimal impl is beneficial.

Summary of Changes

Only get the pubkey from each account when scanning.

@brooksprumo brooksprumo self-assigned this Jul 10, 2024
@brooksprumo brooksprumo force-pushed the append-vec/scan-pubkeys branch 3 times, most recently from f9204db to 31158ff Compare July 10, 2024 15:41
@brooksprumo brooksprumo changed the title Optimizes AppendVec::scan_pubkeys() when using file i/o Optimizes AppendVec::scan_pubkeys() when using file io Jul 10, 2024
@brooksprumo brooksprumo force-pushed the append-vec/scan-pubkeys branch from 31158ff to f7b1f92 Compare July 10, 2024 20:03
@brooksprumo brooksprumo marked this pull request as ready for review July 10, 2024 20:07
@brooksprumo brooksprumo requested a review from HaoranYi July 10, 2024 20:26
@brooksprumo
Copy link
Author

@HaoranYi I'm requesting a review before CI finishes because this PR has already successfully gone through CI prior to me rebasing.

callback(stored_meta.pubkey());
});
AppendVecFileBacking::File(file) => {
let buffer_size = std::cmp::min(SCAN_BUFFER_SIZE, self.len());
Copy link

@HaoranYi HaoranYi Jul 10, 2024

Choose a reason for hiding this comment

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

SCAN_BUFFER_SIZE is around 30MB. And the data, which we are really interested in, is 136B. It seems like we are over fetching the buffer and don't skip much of the account's data.

For example, let's say that account data.len is about 100K. Then, each time we read, we fetch up to 300 accounts in the buffer. And we will only skip 1 account after we used all 300 accounts in the buffer. then fetch another 300 Accounts... Only 1 out of 300 accounts are skipped.

How about making 'SCAN_BUFFER_SIZE' smaller, i.e. 1K?
For the example above, 1K buffer will nicely skip loading 99K data of the accounts.

Copy link

@HaoranYi HaoranYi Jul 10, 2024

Choose a reason for hiding this comment

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

Maybe 1M is better.
There is a tradeoff to consider. On one hand, it would be more efficient to read larger buffer at a time. On the other hand, too large a buffer would cause us to read too much unused data...

Choose a reason for hiding this comment

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

wdyt?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I think this is a valid concern. And yes, I agree, it is a tradeoff. The point of this PR is to avoid unnecessary reading/copying, so a smaller buffer is better in that regard. However, we also don't want a lot of syscalls; a larger buffer is better in that case. I'm not sure what the sweet spot is. IOW, at what buffer size is it equivalently expensive to make a syscall?

I've bumped it down to a page size. Maybe that's too small?

Choose a reason for hiding this comment

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

Probably we can write a benchmark: generate 20K account, each account data 1k-10K data and try different buffer size?

Choose a reason for hiding this comment

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

Also, it seems that we can reduce the size of the buffer for scan_index too.

Copy link
Author

Choose a reason for hiding this comment

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

Good idea; I'm working on a benchmark now.

With preliminary benchmark results, I've found a buffer size of 256 KiB to work well. I think this should be good enough for now, for this PR. File IO is only used by us for testing, and I'd like to get some value in here so we can test on mnb too.

Choose a reason for hiding this comment

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

ok. sounds good to me.

@brooksprumo brooksprumo requested a review from HaoranYi July 11, 2024 15:39
Copy link

@HaoranYi HaoranYi left a comment

Choose a reason for hiding this comment

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

lgtm

@brooksprumo brooksprumo merged commit 28ee45b into anza-xyz:master Jul 11, 2024
40 checks passed
@brooksprumo brooksprumo deleted the append-vec/scan-pubkeys branch July 11, 2024 17:08
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