Apache DataFusion SQL Query Engine
ARROW-3649: [Rust] Refactor MutableBuffer's resize
Chao Sun959b4171fdf5
diff --git a/rust/src/buffer.rs b/rust/src/buffer.rsindex dd6069a988..cec38dbcff 100644--- a/rust/src/buffer.rs+++ b/rust/src/buffer.rs@@ -176,15 +176,6 @@ impl MutableBuffer {self}- /// Manually sets the `len` of this buffer- pub fn set_len(&mut self, new_len: usize) {- assert!(- new_len <= self.capacity,- "Unable to set len outside capacity"- );- self.len = new_len;- }-/// Ensure that `count` bytes from `start` contain zero bits////// This is used to initialize the bits in a buffer, however, it has no impact on the `len`@@ -196,23 +187,41 @@ impl MutableBuffer {}}- /// Adjust the capacity of this buffer to be at least `new_capacity`.+ /// Ensures that this buffer has at least `capacity` slots in this buffer. This will+ /// also ensure the new capacity will be a multiple of 64 bytes.+ ///+ /// Returns the new capacity for this buffer.+ pub fn reserve(&mut self, capacity: usize) -> Result<usize> {+ if capacity > self.capacity {+ let new_capacity = bit_util::round_upto_multiple_of_64(capacity as i64);+ let new_capacity = cmp::max(new_capacity, self.capacity as i64 * 2) as usize;+ let new_data = memory::reallocate(self.capacity, new_capacity, self.data)?;+ self.data = new_data as *mut u8;+ self.capacity = new_capacity;+ }+ Ok(self.capacity)+ }++ /// Resizes the buffer so that the `len` will equal to the `new_len`.///- /// If the `new_capacity` is less than the current capacity, nothing is done and `Ok`- /// will be returned. Otherwise, the new capacity value will be chosen between the- /// larger one of the incoming `new_capacity` (after rounding up to the nearest 64)- /// and the doubled value of the existing capacity, the capacity added is returned.- pub fn resize(&mut self, new_capacity: usize) -> Result<usize> {- if new_capacity <= self.capacity {- return Ok(0);+ /// If `new_len` is greater than `len`, the buffer's length is simply adjusted to be+ /// the former, optionally extending the capacity. The data between `len` and+ /// `new_len` will remain unchanged.+ ///+ /// If `new_len` is less than `len`, the buffer will be truncated.+ pub fn resize(&mut self, new_len: usize) -> Result<()> {+ if new_len > self.len {+ self.reserve(new_len)?;+ } else {+ let new_capacity = bit_util::round_upto_multiple_of_64(new_len as i64) as usize;+ if new_capacity < self.capacity {+ let new_data = memory::reallocate(self.capacity, new_capacity, self.data)?;+ self.data = new_data as *mut u8;+ self.capacity = new_capacity;+ }}- let new_capacity = bit_util::round_upto_multiple_of_64(new_capacity as i64);- let new_capacity = cmp::max(new_capacity, self.capacity as i64 * 2) as usize;- let new_data = memory::reallocate(self.capacity, new_capacity, self.data)?;- self.data = new_data as *mut u8;- let capacity_added = new_capacity - self.capacity;- self.capacity = new_capacity;- Ok(capacity_added)+ self.len = new_len;+ Ok(())}/// Returns whether this buffer is empty or not.@@ -450,17 +459,46 @@ mod tests {}}+ #[test]+ fn test_mutable_reserve() {+ let mut buf = MutableBuffer::new(1);+ assert_eq!(64, buf.capacity());++ // Reserving a smaller capacity should have no effect.+ let mut new_cap = buf.reserve(10).expect("reserve should be OK");+ assert_eq!(64, new_cap);+ assert_eq!(64, buf.capacity());++ new_cap = buf.reserve(100).expect("reserve should be OK");+ assert_eq!(128, new_cap);+ assert_eq!(128, buf.capacity());+ }+#[test]fn test_mutable_resize() {let mut buf = MutableBuffer::new(1);assert_eq!(64, buf.capacity());+ assert_eq!(0, buf.len());++ buf.resize(20).expect("resize should be OK");+ assert_eq!(64, buf.capacity());+ assert_eq!(20, buf.len());- // resizing to a smaller value should have no effect.buf.resize(10).expect("resize should be OK");assert_eq!(64, buf.capacity());+ assert_eq!(10, buf.len());buf.resize(100).expect("resize should be OK");assert_eq!(128, buf.capacity());+ assert_eq!(100, buf.len());++ buf.resize(30).expect("resize should be OK");+ assert_eq!(64, buf.capacity());+ assert_eq!(30, buf.len());++ buf.resize(0).expect("resize should be OK");+ assert_eq!(0, buf.capacity());+ assert_eq!(0, buf.len());}#[test]diff --git a/rust/src/builder.rs b/rust/src/builder.rsindex 2f694b033e..90ab606715 100644--- a/rust/src/builder.rs+++ b/rust/src/builder.rs@@ -61,37 +61,28 @@ macro_rules! impl_buffer_builder {(byte_capacity / mem::size_of::<$native_ty>()) as i64}- /// Push a value into the builder, growing the internal buffer as needed+ /// Pushes a value into the builder, growing the internal buffer as needed.pub fn push(&mut self, v: $native_ty) -> Result<()> {self.reserve(1)?;self.write_bytes(v.to_byte_slice(), 1)}- /// Push a slice of type T, growing the internal buffer as needed+ /// Pushes a slice of type `T`, growing the internal buffer as needed.pub fn push_slice(&mut self, slice: &[$native_ty]) -> Result<()> {let array_slots = slice.len() as i64;self.reserve(array_slots)?;self.write_bytes(slice.to_byte_slice(), array_slots)}- /// Reserve memory for n elements of type T+ /// Reserves memory for `n` elements of type `T`.pub fn reserve(&mut self, n: i64) -> Result<()> {let new_capacity = self.len + n;- if new_capacity > self.capacity() {- return self.grow(new_capacity);- }- Ok(())- }-- /// Grow the internal buffer to `new_capacity`, where `new_capacity` is the capacity in- /// elements of type T- fn grow(&mut self, new_capacity: i64) -> Result<()> {let byte_capacity = mem::size_of::<$native_ty>() * new_capacity as usize;- self.buffer.resize(byte_capacity)?;+ self.buffer.reserve(byte_capacity)?;Ok(())}- /// Build an immutable `Buffer` from the existing internal `MutableBuffer`'s memory+ /// Consumes this builder and returns an immutable `Buffer`.pub fn finish(self) -> Buffer {self.buffer.freeze()}@@ -127,7 +118,7 @@ impl_buffer_builder!(f32);impl_buffer_builder!(f64);impl BufferBuilder<bool> {- /// Creates a builder with a fixed initial capacity+ /// Creates a builder with a fixed initial capacity.pub fn new(capacity: i64) -> Self {let byte_capacity = bit_util::ceil(capacity, 8);let actual_capacity = bit_util::round_upto_multiple_of_64(byte_capacity) as usize;@@ -140,23 +131,23 @@ impl BufferBuilder<bool> {}}- /// Returns the number of array elements (slots) in the builder+ /// Returns the number of array elements (slots) in the builder.pub fn len(&self) -> i64 {self.len}- /// Returns the current capacity of the builder (number of elements)+ /// Returns the current capacity of the builder (number of elements).pub fn capacity(&self) -> i64 {let byte_capacity = self.buffer.capacity() as i64;byte_capacity * 8}- /// Push a value into the builder, growing the internal buffer as needed+ /// Pushes a value into the builder, growing the internal buffer as needed.pub fn push(&mut self, v: bool) -> Result<()> {self.reserve(1)?;if v {// For performance the `len` of the buffer is not updated on each push but- // is updated in the `freeze` method instead+ // is updated in the `freeze` method instead.unsafe {bit_util::set_bit_raw(self.buffer.raw_data() as *mut u8, (self.len) as usize);}@@ -165,7 +156,7 @@ impl BufferBuilder<bool> {Ok(())}- /// Push a slice of type T, growing the internal buffer as needed+ /// Pushes a slice of type `T`, growing the internal buffer as needed.pub fn push_slice(&mut self, slice: &[bool]) -> Result<()> {let array_slots = slice.len();for i in 0..array_slots {@@ -174,30 +165,25 @@ impl BufferBuilder<bool> {Ok(())}- /// Reserve memory for n elements of type T+ /// Reserves memory for `n` elements of type `T`.pub fn reserve(&mut self, n: i64) -> Result<()> {let new_capacity = self.len + n;if new_capacity > self.capacity() {- return self.grow(new_capacity);+ let new_byte_capacity = bit_util::ceil(new_capacity, 8) as usize;+ let existing_capacity = self.buffer.capacity();+ let new_capacity = self.buffer.reserve(new_byte_capacity)?;+ self.buffer+ .set_null_bits(existing_capacity, new_capacity - existing_capacity);}Ok(())}- /// Grow the internal buffer to `new_capacity`, where `new_capacity` is the capacity in- /// elements of type T- fn grow(&mut self, new_capacity: i64) -> Result<()> {- let new_byte_capacity = bit_util::ceil(new_capacity, 8) as usize;- let existing_capacity = self.buffer.capacity();- let capacity_added = self.buffer.resize(new_byte_capacity)?;- self.buffer.set_null_bits(existing_capacity, capacity_added);- Ok(())- }-- /// Build an immutable `Buffer` from the existing internal `MutableBuffer`'s memory+ /// Consumes this and returns an immutable `Buffer`.pub fn finish(mut self) -> Buffer {- // `push` does not update the buffer's `len` to it is done before `freeze` is called- let new_buffer_len = bit_util::ceil(self.len, 8);- self.buffer.set_len(new_buffer_len as usize);+ // `push` does not update the buffer's `len` so do it before `freeze` is called.+ let new_buffer_len = bit_util::ceil(self.len, 8) as usize;+ debug_assert!(new_buffer_len >= self.buffer.len());+ self.buffer.resize(new_buffer_len).unwrap();self.buffer.freeze()}}