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

Move rebuild_vec into a method #659

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/bytes_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl BytesMut {
// Just re-use `Bytes` internal Vec vtable
unsafe {
let (off, _) = self.get_vec_pos();
let vec = rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off);
let vec = self.rebuild_vec(off);
mem::forget(self);
let mut b: Bytes = vec.into();
b.advance(off);
Expand Down Expand Up @@ -629,8 +629,7 @@ impl BytesMut {
} else {
// Not enough space, or reusing might be too much overhead:
// allocate more space!
let mut v =
ManuallyDrop::new(rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off));
let mut v = ManuallyDrop::new(self.rebuild_vec(off));
v.reserve(additional);

// Update the info
Expand Down Expand Up @@ -950,7 +949,7 @@ impl BytesMut {
// `Arc`, those three fields still are the components of the
// vector.
let shared = Box::new(Shared {
vec: rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off),
vec: self.rebuild_vec(off),
original_capacity_repr,
ref_count: AtomicUsize::new(ref_cnt),
});
Comment on lines 951 to 955
Copy link
Contributor

Choose a reason for hiding this comment

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

And you don't actually follow the safety requirements as stated here.

Expand Down Expand Up @@ -1035,6 +1034,16 @@ impl BytesMut {
slice::from_raw_parts_mut(ptr.cast(), len)
}
}

/// Rebuild a vec from `offset` with the existing pointer, length, and capacity.
#[inline]
unsafe fn rebuild_vec(&self, offset: usize) -> Vec<u8> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you document a bit about why this is unsafe? That is, explain that this transfers ownership of the allocation to the vector, but that it doesn't remove the pointers from the BytesMut that it is called on. (And of course, the offset must be correct.)

I know that it didn't explain this before your change, but by moving it to BytesMut, you're introducing the connotation that it could modify the BytesMut to no longer own the allocation. That connotation was not there when it was a standalone method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing that out @Darksonn! I added some information in f62a4d0. Let me know if you prefer something different.

Also, the source of offset is always the offset from self.get_vec_pos, so we could probably remove that requirement by fetching it in this method. I'll attempt that separately though!

let ptr = self.ptr.as_ptr().offset(-(offset as isize));
let len = self.len + offset;
let cap = self.cap + offset;

Vec::from_raw_parts(ptr, len, cap)
}
}

impl Drop for BytesMut {
Expand All @@ -1046,7 +1055,7 @@ impl Drop for BytesMut {
let (off, _) = self.get_vec_pos();

// Vector storage, free the vector
let _ = rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off);
let _ = self.rebuild_vec(off);
}
} else if kind == KIND_ARC {
unsafe { release_shared(self.data) };
Expand Down Expand Up @@ -1626,7 +1635,7 @@ impl From<BytesMut> for Vec<u8> {
let mut vec = if kind == KIND_VEC {
unsafe {
let (off, _) = bytes.get_vec_pos();
rebuild_vec(bytes.ptr.as_ptr(), bytes.len, bytes.cap, off)
bytes.rebuild_vec(off)
}
} else if kind == KIND_ARC {
let shared = bytes.data as *mut Shared;
Expand Down Expand Up @@ -1695,14 +1704,6 @@ fn offset_from(dst: *mut u8, original: *mut u8) -> usize {
dst as usize - original as usize
}

unsafe fn rebuild_vec(ptr: *mut u8, mut len: usize, mut cap: usize, off: usize) -> Vec<u8> {
let ptr = ptr.offset(-(off as isize));
len += off;
cap += off;

Vec::from_raw_parts(ptr, len, cap)
}

// ===== impl SharedVtable =====

static SHARED_VTABLE: Vtable = Vtable {
Expand Down