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 boost::circular_buffer: #3400

Closed
wants to merge 1 commit into from
Closed

Use boost::circular_buffer: #3400

wants to merge 1 commit into from

Conversation

nbougalis
Copy link
Contributor

The existing code used std::deque along with a size check to constrain the size of a buffer and, effectively, "hand rolled" a circular buffer. This change simply migrates directly to boost::circular_buffer.

The existing code used std::deque along with a size check to constrain the
size of a buffer and, effectively, "hand rolled" a circular buffer. This
change simply migrates directly to boost::circular_buffer.
@scottschurr
Copy link
Collaborator

I've not looked at the code. But I want to make sure you're aware that boost::circular_buffer is fixed size and (from the documentation)...

When its capacity is exhausted, newly inserted elements will cause elements to be overwritten, either at the beginning or end of the buffer (depending on what insert operation is used).

If you're okay with that in this particular application then change ahead. I've never found a place where these characteristics are useful. I always want to know when overflow occurs. I never want the buffer to silently do the wrong thing.

@nbougalis
Copy link
Contributor Author

nbougalis commented May 14, 2020

Appreciate the heads up @scottschurr. Yeah, this is the right thing in this instance and it is, in fact, what the code was already doing: if the deque was "full" (i.e. contained a given number of items), pop the front and then push the new item to the tail. The very definition of a circular buffer.

@@ -2324,11 +2321,6 @@ PeerImp::addLedger(
recentLedgers_.end())
return;

// VFALCO TODO See if a sorted vector would be better.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering why this comment is here. It seems that circular_buffer is the obvious choice here, so I'm wondering why Vinny was suggesting a sorted vector.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We use std::find in a few places, so a sorted vector would make it possible to use std::binary_search instead of std::find.

It would trade additional insertion complexity for fewer comparisons during a search. I don't know how this comes down.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've done a few studies on this and it depends on the length of the sequence, complexity of the comparison operator and size of each element. But the takeaway is that if sizeof and comparison are fairly cheap, the linear search wins for a surprising length. I don't have the exact numbers off the top of my head, but I believe it was in the hundreds, not in the tens, before binary search began to win.

@nbougalis nbougalis added the Ready to merge *PR author* thinks it's ready to merge. Has passed code review. Perf sign-off may still be required. label May 18, 2020
This was referenced May 21, 2020
@manojsdoshi manojsdoshi mentioned this pull request May 27, 2020
@nbougalis nbougalis deleted the circularbuffer branch October 16, 2023 06:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready to merge *PR author* thinks it's ready to merge. Has passed code review. Perf sign-off may still be required.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants