You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by jo...@apache.org on 2020/12/12 21:46:38 UTC

[arrow] branch master updated: ARROW-10878: [Rust] Simplify extend_from_slice

This is an automated email from the ASF dual-hosted git repository.

jorgecarleitao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new f8e9119  ARROW-10878: [Rust] Simplify extend_from_slice
f8e9119 is described below

commit f8e9119c01d25b1643fc3bc383dde6c9c75f3c39
Author: Heres, Daniel <da...@gmail.com>
AuthorDate: Sat Dec 12 21:45:44 2020 +0000

    ARROW-10878: [Rust] Simplify extend_from_slice
    
    This is just a small simplification to `extend_from_slice` to reuse some more computations which helps performance.
    
    Also adds an `#[inline]`.
    
    Closes #8898 from Dandandan/extend_from_slice
    
    Authored-by: Heres, Daniel <da...@gmail.com>
    Signed-off-by: Jorge C. Leitao <jo...@gmail.com>
---
 rust/arrow/src/buffer.rs | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/rust/arrow/src/buffer.rs b/rust/arrow/src/buffer.rs
index f917959..8af9396 100644
--- a/rust/arrow/src/buffer.rs
+++ b/rust/arrow/src/buffer.rs
@@ -836,15 +836,16 @@ impl MutableBuffer {
     }
 
     /// Extends the buffer from a byte slice, incrementing its capacity if needed.
+    #[inline]
     pub fn extend_from_slice(&mut self, bytes: &[u8]) {
-        let remaining_capacity = self.capacity - self.len;
-        if bytes.len() > remaining_capacity {
-            self.reserve(self.len + bytes.len());
+        let new_len = self.len + bytes.len();
+        if new_len > self.capacity {
+            self.reserve(new_len);
         }
         unsafe {
             memory::memcpy(self.data.add(self.len), bytes.as_ptr(), bytes.len());
         }
-        self.len += bytes.len();
+        self.len = new_len;
     }
 
     /// Extends the buffer by `len` with all bytes equal to `0u8`, incrementing its capacity if needed.