tustvold commented on code in PR #6178:
URL: https://github.com/apache/arrow-rs/pull/6178#discussion_r1709440874


##########
arrow-buffer/src/buffer/mutable.rs:
##########
@@ -668,6 +668,56 @@ impl MutableBuffer {
     }
 }
 
+impl<'a> MutableBuffer {
+    /// Creates a [`MutableBuffer`] from an `&[u8]` [`Iterator`] with a 
trusted (upper) length.
+    /// Prefer this to `collect` whenever possible, as it is faster.
+    /// # Panic
+    /// This method will panic if `&u8.len()` != `item_size`.
+    /// # Example
+    /// ```
+    /// # use arrow_buffer::MutableBuffer;
+    /// # use arrow_buffer::ToByteSlice;
+    /// let iter = vec![[1_u8, 2].to_byte_slice(), [3_u8, 
4].to_byte_slice()].into_iter();
+    /// let buf = unsafe {
+    ///     MutableBuffer::from_trusted_len_iter_slice_u8(iter, 2)
+    /// };
+    /// assert_eq!(4, buf.len());
+    /// ```
+    /// # Safety
+    /// This method assumes that the iterator's size is correct and is 
undefined behavior
+    /// to use it on an iterator that reports an incorrect length.
+    // This implementation is required for two reasons:
+    // 1. there is no trait `TrustedLen` in stable rust and therefore
+    //    we can't specialize `extend` for `TrustedLen` like `Vec` does.
+    // 2. `from_trusted_len_iter_slice_u8` is faster.
+    #[inline]
+    pub unsafe fn from_trusted_len_iter_slice_u8<I: Iterator<Item = &'a [u8]>>(

Review Comment:
   Right from_trusted_len_iter and from_trusted_len_iter_bool make sense 
because they're using primitives with a fixed width known at compile time, this 
allows for effective vectorisation. With variable width types, as in 
from_trusted_len_iter_slice_u8, the compiler will just insert a memcpy for each 
element, and there will be no performance benefit



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to