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

Mutablebuffer::shrink_to_fit #318

Merged
merged 2 commits into from
May 19, 2021
Merged
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
42 changes: 42 additions & 0 deletions arrow/src/buffer/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,37 @@ impl MutableBuffer {
self.len = new_len;
}

/// Shrinks the capacity of the buffer as much as possible.
/// The new capacity will aligned to the nearest 64 bit alignment.
///
/// # Example
/// ```
/// # use arrow::buffer::{Buffer, MutableBuffer};
/// // 2 cache lines
/// let mut buffer = MutableBuffer::new(128);
/// assert_eq!(buffer.capacity(), 128);
/// buffer.push(1);
/// buffer.push(2);
///
/// buffer.shrink_to_fit();
/// assert!(buffer.capacity() >= 64 && buffer.capacity() < 128);
/// ```
pub fn shrink_to_fit(&mut self) {
let new_capacity = bit_util::round_upto_multiple_of_64(self.len);
if new_capacity < self.capacity {
// JUSTIFICATION
// Benefit
// necessity
// Soundness
// `self.data` is valid for `self.capacity`.
let ptr =
unsafe { alloc::reallocate(self.data, self.capacity, new_capacity) };

self.data = ptr;
self.capacity = new_capacity;
}
}

/// Returns whether this buffer is empty or not.
#[inline]
pub const fn is_empty(&self) -> bool {
Expand Down Expand Up @@ -746,4 +777,15 @@ mod tests {
buf2.reserve(65);
assert!(buf != buf2);
}

#[test]
fn test_mutable_shrink_to_fit() {
let mut buffer = MutableBuffer::new(128);
assert_eq!(buffer.capacity(), 128);
buffer.push(1);
buffer.push(2);

buffer.shrink_to_fit();
assert!(buffer.capacity() >= 64 && buffer.capacity() < 128);
}
}