You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/12/30 11:31:57 UTC

[GitHub] [arrow] Dandandan commented on a change in pull request #9044: ARROW-11045: [Rust] Fix performance issues of allocator

Dandandan commented on a change in pull request #9044:
URL: https://github.com/apache/arrow/pull/9044#discussion_r550162644



##########
File path: rust/arrow/src/memory.rs
##########
@@ -180,65 +187,62 @@ pub unsafe fn free_aligned(ptr: *mut u8, size: usize) {
 ///
 /// * new_size, when rounded up to the nearest multiple of [ALIGNMENT], must not overflow (i.e.,
 /// the rounded value must be less than usize::MAX).
-pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8 {
-    if ptr == BYPASS_PTR.as_ptr() {
+pub unsafe fn reallocate(
+    ptr: NonNull<u8>,
+    old_size: usize,
+    new_size: usize,
+) -> NonNull<u8> {
+    if ptr == BYPASS_PTR {
         return allocate_aligned(new_size);
     }
 
     if new_size == 0 {
         free_aligned(ptr, old_size);
-        return BYPASS_PTR.as_ptr();
+        return BYPASS_PTR;
     }
 
     ALLOCATIONS.fetch_add(
         new_size as isize - old_size as isize,
         std::sync::atomic::Ordering::SeqCst,
     );
-    let new_ptr = std::alloc::realloc(
-        ptr,
+    let raw_ptr = std::alloc::realloc(
+        ptr.as_ptr(),
         Layout::from_size_align_unchecked(old_size, ALIGNMENT),
         new_size,
     );
-
-    if !new_ptr.is_null() && new_size > old_size {
-        new_ptr.add(old_size).write_bytes(0, new_size - old_size);
+    let ptr = NonNull::new(raw_ptr).unwrap_or_else(|| {
+        handle_alloc_error(Layout::from_size_align_unchecked(new_size, ALIGNMENT))
+    });
+
+    if new_size > old_size {
+        ptr.as_ptr()
+            .add(old_size)
+            .write_bytes(0, new_size - old_size);

Review comment:
       Shouldn't we be able to remove initializing with 0 here as well?




----------------------------------------------------------------
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:
users@infra.apache.org