-
Notifications
You must be signed in to change notification settings - Fork 287
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
Reuse capacity when possible in <BytesMut as Buf>::advance
impl
#698
Conversation
<BytesMut as Buf>::advance
impl
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.
Would it make more sense to move this check to advance_unchecked
?
I'm not quite sure how to structure this because the thing with |
The conflict with |
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.
This is nice, thanks! I only have two small requests:
- Can we use
self.remaining()
orself.len
consistently in this function? Either one is fine with me, but it's not obvious that those values are synonymous. - Can we move this conditional before the
assert!
above? Ifcnt == self.len
, we already know thatcnt <= self.remaining()
so we don't need to check it twice.
Good idea. Fixed! |
This change broke our usage of
In our case, we expect that |
That's unfortunate, we may have to revert this change. Can you open a bug report with details about this regression? |
Opened #725 |
While thinking about usage of
BytesMut
in my own code I've realized sometimes the following pattern (explained by the following pseudocode) is encountered:In this particular example it would be more efficient to
buf.clear()
whenconsumed == buf.len()
, instead of callingadvance
and eventually ending-up going through the allocation machinery.This PR makes
advance
do it automatically for cases in which it's safe to do (which is why I didn't put it insideadvance_unchecked
). I'm not sure theBuf
API allows it, the only thing I've found is#[must_use = "consider BytesMut::advance(len()) if you don't need the other half"]
onBytesMut::split()
.