[ 
https://issues.apache.org/jira/browse/ARROW-2398?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16426809#comment-16426809
 ] 

ASF GitHub Bot commented on ARROW-2398:
---------------------------------------

xhochy commented on a change in pull request #1838: ARROW-2398: [Rust] Create 
Builder<T> for building buffers directly in aligned memory
URL: https://github.com/apache/arrow/pull/1838#discussion_r179441418
 
 

 ##########
 File path: rust/src/buffer.rs
 ##########
 @@ -18,10 +18,74 @@
 use bytes::Bytes;
 use libc;
 use std::mem;
+use std::ptr;
 use std::slice;
 
 use super::memory::*;
 
+/// Buffer builder with zero-copy build method
+pub struct Builder<T> {
+    data: *mut T,
+    len: usize,
+    capacity: usize,
+}
+
+impl<T> Builder<T> {
+    /// Creates a builder with a default capacity
+    pub fn new() -> Self {
+        Builder::with_capacity(64)
+    }
+
+    /// Creates a builder with a fixed capacity
+    pub fn with_capacity(capacity: usize) -> Self {
+        let sz = mem::size_of::<T>();
+        let buffer = allocate_aligned((capacity * sz) as i64).unwrap();
+        Builder {
+            len: 0,
+            capacity,
+            data: unsafe { mem::transmute::<*const u8, *mut T>(buffer) },
+        }
+    }
+
+    /// Push a value into the builder, growing the internal buffer as needed
+    pub fn push(&mut self, v: T) {
+        assert!(!self.data.is_null());
+        if self.len == self.capacity {
+            let sz = mem::size_of::<T>();
+            let new_capacity = self.capacity * 2;
+            unsafe {
+                let old_buffer = self.data;
+                let new_buffer = allocate_aligned((new_capacity * sz) as 
i64).unwrap();
+                libc::memcpy(
+                    mem::transmute::<*const u8, *mut libc::c_void>(new_buffer),
+                    mem::transmute::<*const T, *const 
libc::c_void>(old_buffer),
+                    self.len * sz,
+                );
+                self.capacity = new_capacity;
+                self.data = mem::transmute::<*const u8, *mut T>(new_buffer);
+                mem::drop(old_buffer);
+            }
+        }
+        assert!(self.len < self.capacity);
+        unsafe {
+            *self.data.offset(self.len as isize) = v;
+        }
+        self.len += 1;
+    }
+
+    /// Build a Buffer from the existing memory
+    pub fn build(&mut self) -> Buffer<T> {
 
 Review comment:
   Is there a reason that this is called `build`? In C++ we call this `Finish` 
and it would be nice to have them consistent. If `build` is a typical name used 
in Rust, it's probably better to keep it as is.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> [Rust] Provide a zero-copy builder for type-safe Buffer<T>
> ----------------------------------------------------------
>
>                 Key: ARROW-2398
>                 URL: https://issues.apache.org/jira/browse/ARROW-2398
>             Project: Apache Arrow
>          Issue Type: New Feature
>          Components: Rust
>            Reporter: Andy Grove
>            Assignee: Andy Grove
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.10.0
>
>
> This PR implements a builder so that buffers can be populated directly in 
> aligned memory (as opposed to being created from Vec<T>).
>  
> https://github.com/apache/arrow/pull/1838



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to