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

Conversation

braddunbar
Copy link
Contributor

The rebuild_vec function is always called with the same first three arguments: the pointer, length, and capacity of a particular BytesMut instance. Instead of passing all of those each time, we can use a method on BytesMut. Sound good?

The rebuild_vec function is always called with the same first three
arguments: the pointer, length, and capacity of a particular BytesMut
instance. Instead of passing all of those each time, we can use a method
on BytesMut.
src/bytes_mut.rs Outdated
Comment on lines 1038 to 1040
/// 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!

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Usually we would have safety comments that explain why an unsafe method is safe to call.

Comment on lines 951 to 955
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),
});
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.

@braddunbar
Copy link
Contributor Author

You're right - I prefer safety comments like that too and I definitely didn't follow them here. I think this is because rebuild_vec is doing a lot here. I'm going to close this for now and come back to it with a different approach.

Thank you for your thoughts @Darksonn!

@braddunbar braddunbar closed this Jan 31, 2024
@braddunbar braddunbar deleted the rebuild-vec branch January 31, 2024 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants