Reuse capacity when possible in <BytesMut as Buf>::advance impl#698
Reuse capacity when possible in <BytesMut as Buf>::advance impl#698Darksonn merged 1 commit intotokio-rs:masterfrom M4SS-Code:bytesmut-advance-reuse
<BytesMut as Buf>::advance impl#698Conversation
<BytesMut as Buf>::advance impl
Darksonn
left a comment
There was a problem hiding this comment.
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 |
braddunbar
left a comment
There was a problem hiding this comment.
This is nice, thanks! I only have two small requests:
- Can we use
self.remaining()orself.lenconsistently 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
BytesMutin 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 callingadvanceand eventually ending-up going through the allocation machinery.This PR makes
advancedo 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 theBufAPI 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().