jorgecarleitao commented on a change in pull request #9076:
URL: https://github.com/apache/arrow/pull/9076#discussion_r550910490



##########
File path: rust/arrow/src/buffer.rs
##########
@@ -852,17 +870,34 @@ impl MutableBuffer {
         }
     }
 
-    /// Extends the buffer from a byte slice, incrementing its capacity if 
needed.
+    /// Extends the buffer from a slice, increasing its capacity if needed.
+    #[inline]
+    pub fn extend_from_slice<T: ToByteSlice>(&mut self, items: &[T]) {
+        let additional = items.len() * std::mem::size_of::<T>();
+        let new_len = self.len + additional;
+        if new_len > self.capacity {
+            self.reserve(additional);
+        }
+        unsafe {
+            let dst = self.data.as_ptr().add(self.len) as *mut T;
+            let src = items.as_ptr() as *const T;
+            std::ptr::copy_nonoverlapping(src, dst, items.len())
+        }
+        self.len = new_len;
+    }
+
+    /// Extends the buffer with a new item, increasing its capacity if needed.
     #[inline]
-    pub fn extend_from_slice(&mut self, bytes: &[u8]) {
-        let additional = bytes.len();
-        if self.len + additional > self.capacity() {
+    pub fn push<T: ToByteSlice>(&mut self, item: &T) {
+        let additional = std::mem::size_of::<T>();
+        let new_len = self.len + additional;
+        if new_len > self.capacity {

Review comment:
       I though about that also, but it requires more investigations. We would 
need to make `MutableBuffer` a generic over `T` and that has implications in 
our ability to build generic structures.
   
   For example, `builders::FieldData` relies on a generic `MutableBuffer`, and 
`MutableArrayData` also.
   
   Note that growing and freezing a `MutableBuffer` is now faster than 
`Vec<u32>` to store `u32`. We do still have a performance problem on which 
growing a `MutableBuffer` is faster than pre-reserving it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to