-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
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.
I've not looked at the code. But I want to make sure you're aware that
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. |
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 |
@@ -2324,11 +2321,6 @@ PeerImp::addLedger( | |||
recentLedgers_.end()) | |||
return; | |||
|
|||
// VFALCO TODO See if a sorted vector would be better. |
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.
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.
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.
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.
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.
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.
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 toboost::circular_buffer
.