-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[RPC][BUGFIX][BACKPORT-0.6] Fix bug in rpc ring buffer shrink #5516
Conversation
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.
LGTM
src/support/ring_buffer.h
Outdated
size_t old_bytes = bytes_available_; | ||
|
||
std::vector<char> tmp(old_bytes); | ||
|
||
Read(&tmp[0], old_bytes); | ||
ring_.resize(kInitCapacity); | ||
if (old_bytes != 0) { | ||
Read(&tmp[0], old_bytes); | ||
} | ||
|
||
size_t new_size = kInitCapacity; | ||
new_size = std::max(new_size, bytes_available_); | ||
new_size = std::max(new_size, n); | ||
|
||
ring_.resize(new_size); | ||
ring_.shrink_to_fit(); | ||
|
||
memcpy(&ring_[0], &tmp[0], old_bytes); | ||
if (old_bytes != 0) { | ||
memcpy(&ring_[0], &tmp[0], old_bytes); | ||
} | ||
|
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.
Not performance critical but I think it could be better recast as below.
if (bytes_available != 0)
{
... existing code
}
else
{
ring_.resize(kInitCapacity);
ring_.shrink_to_fit();
}
Avoids unnecessary runtime calls to std::max with 0 and additional checks that can go. And for bonus points, encapsulate ring_.resize(new_size) and ring_.shrink_to_fit into a small helper function if you so choose.
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.
Thanks for the comment, we I updated the content to make it a bit more compact, we still need to compare to n in case n is larger than initcapacity
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 see, yes of-course I can now see that capacity can be shrunk to max (kinitCapacity, n, bytes_available). Might be worth a small comment.
regards
Ramana
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.
Minor typos which will probably break the build pretty quickly.
tag @FrozenGene who also met this problem |
cc @merrymercy @giuseros @ZihengJiang @areusch @tmoreau89