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

Not shrink Vec on coverting to Bytes (BytesMut::freeze improve) #418

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
12 changes: 5 additions & 7 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,17 +768,15 @@ impl From<&'static str> for Bytes {

impl From<Vec<u8>> for Bytes {
fn from(vec: Vec<u8>) -> Bytes {
// into_boxed_slice doesn't return a heap allocation for empty vectors,
// so the pointer isn't aligned enough for the KIND_VEC stashing to
// work.
// Empty Vec doesn't have a heap allocation, so the pointer isn't
// aligned enough for the KIND_VEC stashing to work.
if vec.is_empty() {
return Bytes::new();
}

let slice = vec.into_boxed_slice();
let len = slice.len();
let ptr = slice.as_ptr();
drop(Box::into_raw(slice));
let mut v = mem::ManuallyDrop::new(vec);
let len = v.len();
let ptr = v.as_mut_ptr();

if ptr as usize & 0x1 == 0 {
let data = ptr as usize | KIND_VEC;
Expand Down