File tree 2 files changed +18
-3
lines changed
2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -559,9 +559,8 @@ impl BytesMut {
559
559
unsafe {
560
560
let ( off, prev) = self . get_vec_pos ( ) ;
561
561
562
- // Only reuse space if we stand to gain at least capacity/2
563
- // bytes of space back
564
- if off >= additional && off >= ( self . cap / 2 ) {
562
+ // Only reuse space if we can satisfy the requested additional space.
563
+ if self . capacity ( ) - self . len ( ) + off >= additional {
565
564
// There's space - reuse it
566
565
//
567
566
// Just move the pointer back to the start after copying
Original file line number Diff line number Diff line change @@ -929,6 +929,22 @@ fn bytes_buf_mut_advance() {
929
929
}
930
930
}
931
931
932
+ #[ test]
933
+ fn bytes_buf_mut_reuse_when_fully_consumed ( ) {
934
+ use bytes:: { Buf , BytesMut } ;
935
+ let mut buf = BytesMut :: new ( ) ;
936
+ buf. reserve ( 8192 ) ;
937
+ buf. extend_from_slice ( & [ 0u8 ; 100 ] [ ..] ) ;
938
+
939
+ let p = & buf[ 0 ] as * const u8 ;
940
+ buf. advance ( 100 ) ;
941
+
942
+ buf. reserve ( 8192 ) ;
943
+ buf. extend_from_slice ( b" " ) ;
944
+
945
+ assert_eq ! ( & buf[ 0 ] as * const u8 , p) ;
946
+ }
947
+
932
948
#[ test]
933
949
#[ should_panic]
934
950
fn bytes_reserve_overflow ( ) {
You can’t perform that action at this time.
0 commit comments