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

Fix MutableBuffer::into_buffer leaking its extra capacity into the final buffer #6300

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion arrow-array/src/builder/generic_bytes_view_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ mod tests {
exp_builder.flush_in_progress();
assert_eq!(
exp_builder.completed.last().unwrap().capacity(),
MAX_BLOCK_SIZE as usize
STARTING_BLOCK_SIZE as usize
);
}
}
12 changes: 7 additions & 5 deletions arrow-buffer/src/buffer/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ mod tests {
let b = Buffer::from_vec(a);
let back = b.into_vec::<i128>().unwrap();
assert_eq!(back.len(), 0);
assert_eq!(back.capacity(), 20);
assert_eq!(back.capacity(), 0);

// Test vec with values
let mut a: Vec<i128> = Vec::with_capacity(3);
Expand All @@ -745,7 +745,7 @@ mod tests {
let b = Buffer::from_vec(a);
let back = b.into_vec::<i128>().unwrap();
assert_eq!(back.len(), 7);
assert_eq!(back.capacity(), 20);
assert_eq!(back.capacity(), 8);

// Test incorrect alignment
let a: Vec<i128> = Vec::new();
Expand Down Expand Up @@ -779,7 +779,9 @@ mod tests {
let mut b: Vec<i128> = Vec::with_capacity(5);
b.extend_from_slice(&[1, 2, 3, 4]);
let b = Buffer::from_vec(b);
b.into_vec::<i256>().unwrap_err();
assert_eq!(b.len(), 64);
assert_eq!(b.capacity(), 64);
b.into_vec::<i256>().unwrap();

// Truncates length
// This is an implementation quirk, but isn't harmful
Expand Down Expand Up @@ -808,7 +810,7 @@ mod tests {
// Succeeds as no outstanding shared reference
let back = slice.into_vec::<i128>().unwrap();
assert_eq!(&back, &[1, 4, 7, 8]);
assert_eq!(back.capacity(), 20);
assert_eq!(back.capacity(), 8);

// Slicing by non-multiple length truncates
let mut a: Vec<i128> = Vec::with_capacity(8);
Expand All @@ -820,7 +822,7 @@ mod tests {

let back = slice.into_vec::<i128>().unwrap();
assert_eq!(&back, &[1, 4]);
assert_eq!(back.capacity(), 8);
assert_eq!(back.capacity(), 4);

// Offset prevents conversion
let a: Vec<u32> = vec![1, 3, 4, 6];
Expand Down
5 changes: 4 additions & 1 deletion arrow-buffer/src/buffer/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@ impl MutableBuffer {
}

#[inline]
pub(super) fn into_buffer(self) -> Buffer {
pub(super) fn into_buffer(mut self) -> Buffer {
// Don't leak our extra capacity into the final buffer.
self.shrink_to_fit();
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this force a copy in the case where the capacity is larger that the actual need?


let bytes = unsafe { Bytes::new(self.data, self.len, Deallocation::Standard(self.layout)) };
std::mem::forget(self);
Buffer::from_bytes(bytes)
Expand Down
2 changes: 1 addition & 1 deletion arrow-flight/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ mod tests {
])
.unwrap();

verify_encoded_split(batch, 112).await;
verify_encoded_split(batch, 392).await;
}

#[tokio::test]
Expand Down