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

Use VecDeque as a queue instead of Vec #28190

Merged
merged 1 commit into from
Oct 4, 2022
Merged

Conversation

steviez
Copy link
Contributor

@steviez steviez commented Oct 3, 2022

Problem

traverse_children_mut() searches through blocks by pushing block's children onto a queue. Popping from the front of a Vec of n items requires the remaining n-1 items to be shifted to fill the first n-1 positions. VecDeque is a ring buffer, and as such, saves the memcpy when popping from the front.

For what it is worth, this change will probably have negligible impact. The next_slots queue should never really get that large; in the absence of forking, this queue would just be a single element, with a parent getting popped off and then replaced by its' child. With normal forking (ie current epoch boundary issue), we are typically looking at on the order of 10 forks. Regardless, VecDeque is the right structure here.

Summary of Changes

Use VecDeque instead of Vec.

Copy link
Contributor

@yhchiang-sol yhchiang-sol left a comment

Choose a reason for hiding this comment

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

Looks good.

Agree that the actual performance gain might be negligible, but the pop and push pattern in the code is the exact use case of VecDeque. While Vec is also a growable container, VecDeque should be no worse than Vec in this case.

@steviez steviez merged commit 49dbae7 into solana-labs:master Oct 4, 2022
@steviez steviez deleted the vec_deque branch October 4, 2022 03:56
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