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

[RPC][BUGFIX][BACKPORT-0.6] Fix bug in rpc ring buffer shrink #5516

Merged
merged 3 commits into from
May 5, 2020

Conversation

tqchen
Copy link
Member

@tqchen tqchen commented May 5, 2020

Copy link
Contributor

@tmoreau89 tmoreau89 left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 74 to 91
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);
}

Copy link
Contributor

@u99127 u99127 May 5, 2020

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.

Copy link
Member Author

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

Copy link
Contributor

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

Copy link
Contributor

@u99127 u99127 left a 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.

src/support/ring_buffer.h Outdated Show resolved Hide resolved
src/support/ring_buffer.h Outdated Show resolved Hide resolved
@merrymercy
Copy link
Member

merrymercy commented May 5, 2020

tag @FrozenGene who also met this problem

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.

5 participants