set_vec_pos does not need a second parameter (#672)

The second argument to `set_vec_pos` always contains the value of
`self.data`. Let's just use `self.data` and remove the second parameter
altogether.
This commit is contained in:
Brad Dunbar
2024-02-06 11:03:37 -08:00
committed by GitHub
parent c6972d6132
commit f586ffc525
+11 -13
View File
@@ -247,7 +247,7 @@ impl BytesMut {
if self.kind() == KIND_VEC { if self.kind() == KIND_VEC {
// Just re-use `Bytes` internal Vec vtable // Just re-use `Bytes` internal Vec vtable
unsafe { unsafe {
let (off, _) = self.get_vec_pos(); let off = self.get_vec_pos();
let vec = rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off); let vec = rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off);
mem::forget(self); mem::forget(self);
let mut b: Bytes = vec.into(); let mut b: Bytes = vec.into();
@@ -596,7 +596,7 @@ impl BytesMut {
// We need to make sure that this optimization does not kill the // We need to make sure that this optimization does not kill the
// amortized runtimes of BytesMut's operations. // amortized runtimes of BytesMut's operations.
unsafe { unsafe {
let (off, prev) = self.get_vec_pos(); let off = self.get_vec_pos();
// Only reuse space if we can satisfy the requested additional space. // Only reuse space if we can satisfy the requested additional space.
// //
@@ -621,7 +621,7 @@ impl BytesMut {
// Since `off >= self.len()`, the two regions don't overlap. // Since `off >= self.len()`, the two regions don't overlap.
ptr::copy_nonoverlapping(self.ptr.as_ptr(), base_ptr, self.len); ptr::copy_nonoverlapping(self.ptr.as_ptr(), base_ptr, self.len);
self.ptr = vptr(base_ptr); self.ptr = vptr(base_ptr);
self.set_vec_pos(0, prev); self.set_vec_pos(0);
// Length stays constant, but since we moved backwards we // Length stays constant, but since we moved backwards we
// can gain capacity back. // can gain capacity back.
@@ -867,11 +867,10 @@ impl BytesMut {
// complicated. First, we have to track how far ahead the // complicated. First, we have to track how far ahead the
// "start" of the byte buffer from the beginning of the vec. We // "start" of the byte buffer from the beginning of the vec. We
// also have to ensure that we don't exceed the maximum shift. // also have to ensure that we don't exceed the maximum shift.
let (mut pos, prev) = self.get_vec_pos(); let pos = self.get_vec_pos() + start;
pos += start;
if pos <= MAX_VEC_POS { if pos <= MAX_VEC_POS {
self.set_vec_pos(pos, prev); self.set_vec_pos(pos);
} else { } else {
// The repr must be upgraded to ARC. This will never happen // The repr must be upgraded to ARC. This will never happen
// on 64 bit systems and will only happen on 32 bit systems // on 64 bit systems and will only happen on 32 bit systems
@@ -979,19 +978,18 @@ impl BytesMut {
} }
#[inline] #[inline]
unsafe fn get_vec_pos(&mut self) -> (usize, usize) { unsafe fn get_vec_pos(&mut self) -> usize {
debug_assert_eq!(self.kind(), KIND_VEC); debug_assert_eq!(self.kind(), KIND_VEC);
let prev = self.data as usize; self.data as usize >> VEC_POS_OFFSET
(prev >> VEC_POS_OFFSET, prev)
} }
#[inline] #[inline]
unsafe fn set_vec_pos(&mut self, pos: usize, prev: usize) { unsafe fn set_vec_pos(&mut self, pos: usize) {
debug_assert_eq!(self.kind(), KIND_VEC); debug_assert_eq!(self.kind(), KIND_VEC);
debug_assert!(pos <= MAX_VEC_POS); debug_assert!(pos <= MAX_VEC_POS);
self.data = invalid_ptr((pos << VEC_POS_OFFSET) | (prev & NOT_VEC_POS_MASK)); self.data = invalid_ptr((pos << VEC_POS_OFFSET) | (self.data as usize & NOT_VEC_POS_MASK));
} }
/// Returns the remaining spare capacity of the buffer as a slice of `MaybeUninit<u8>`. /// Returns the remaining spare capacity of the buffer as a slice of `MaybeUninit<u8>`.
@@ -1040,7 +1038,7 @@ impl Drop for BytesMut {
if kind == KIND_VEC { if kind == KIND_VEC {
unsafe { unsafe {
let (off, _) = self.get_vec_pos(); let off = self.get_vec_pos();
// Vector storage, free the vector // Vector storage, free the vector
let _ = rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off); let _ = rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off);
@@ -1625,7 +1623,7 @@ impl From<BytesMut> for Vec<u8> {
let mut vec = if kind == KIND_VEC { let mut vec = if kind == KIND_VEC {
unsafe { unsafe {
let (off, _) = bytes.get_vec_pos(); let off = bytes.get_vec_pos();
rebuild_vec(bytes.ptr.as_ptr(), bytes.len, bytes.cap, off) rebuild_vec(bytes.ptr.as_ptr(), bytes.len, bytes.cap, off)
} }
} else { } else {