You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2017/01/10 18:27:40 UTC

arrow git commit: ARROW-469: C++: Add option so that resize doesn't decrease the capacity

Repository: arrow
Updated Branches:
  refs/heads/master 8d917c1f9 -> 543e50814


ARROW-469: C++: Add option so that resize doesn't decrease the capacity

Author: Uwe L. Korn <uw...@xhochy.com>

Closes #277 from xhochy/ARROW-469 and squashes the following commits:

f59059f [Uwe L. Korn] ARROW-469: C++: Add option so that resize doesn't decrease the capacity


Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/543e5081
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/543e5081
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/543e5081

Branch: refs/heads/master
Commit: 543e50814c15d58387683a43b5abc661c4acc484
Parents: 8d917c1
Author: Uwe L. Korn <uw...@xhochy.com>
Authored: Tue Jan 10 13:27:34 2017 -0500
Committer: Wes McKinney <we...@twosigma.com>
Committed: Tue Jan 10 13:27:34 2017 -0500

----------------------------------------------------------------------
 cpp/src/arrow/buffer-test.cc | 11 ++++++++++-
 cpp/src/arrow/buffer.cc      | 11 ++++++-----
 cpp/src/arrow/buffer.h       | 13 ++++++++-----
 3 files changed, 24 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/arrow/blob/543e5081/cpp/src/arrow/buffer-test.cc
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/buffer-test.cc b/cpp/src/arrow/buffer-test.cc
index c1d027b..2ded1e1 100644
--- a/cpp/src/arrow/buffer-test.cc
+++ b/cpp/src/arrow/buffer-test.cc
@@ -53,8 +53,17 @@ TEST_F(TestBuffer, Resize) {
   ASSERT_EQ(200, buf.size());
 
   // Make it smaller, too
-  ASSERT_OK(buf.Resize(50));
+  ASSERT_OK(buf.Resize(50, true));
   ASSERT_EQ(50, buf.size());
+  // We have actually shrunken in size
+  // The spec requires that capacity is a multiple of 64
+  ASSERT_EQ(64, buf.capacity());
+
+  // Resize to a larger capacity again to test shrink_to_fit = false
+  ASSERT_OK(buf.Resize(100));
+  ASSERT_EQ(128, buf.capacity());
+  ASSERT_OK(buf.Resize(50, false));
+  ASSERT_EQ(128, buf.capacity());
 }
 
 TEST_F(TestBuffer, ResizeOOM) {

http://git-wip-us.apache.org/repos/asf/arrow/blob/543e5081/cpp/src/arrow/buffer.cc
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/buffer.cc b/cpp/src/arrow/buffer.cc
index 2e64ffd..6cce0ef 100644
--- a/cpp/src/arrow/buffer.cc
+++ b/cpp/src/arrow/buffer.cc
@@ -91,13 +91,14 @@ Status PoolBuffer::Reserve(int64_t new_capacity) {
   return Status::OK();
 }
 
-Status PoolBuffer::Resize(int64_t new_size) {
-  if (new_size > size_) {
+Status PoolBuffer::Resize(int64_t new_size, bool shrink_to_fit) {
+  if (!shrink_to_fit || (new_size > size_)) {
     RETURN_NOT_OK(Reserve(new_size));
   } else {
     // Buffer is not growing, so shrink to the requested size without
     // excess space.
-    if (capacity_ != new_size) {
+    int64_t new_capacity = BitUtil::RoundUpToMultipleOf64(new_size);
+    if (capacity_ != new_capacity) {
       // Buffer hasn't got yet the requested size.
       if (new_size == 0) {
         pool_->Free(mutable_data_, capacity_);
@@ -105,9 +106,9 @@ Status PoolBuffer::Resize(int64_t new_size) {
         mutable_data_ = nullptr;
         data_ = nullptr;
       } else {
-        RETURN_NOT_OK(pool_->Reallocate(capacity_, new_size, &mutable_data_));
+        RETURN_NOT_OK(pool_->Reallocate(capacity_, new_capacity, &mutable_data_));
         data_ = mutable_data_;
-        capacity_ = new_size;
+        capacity_ = new_capacity;
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/arrow/blob/543e5081/cpp/src/arrow/buffer.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/buffer.h b/cpp/src/arrow/buffer.h
index 27437ca..ac78808 100644
--- a/cpp/src/arrow/buffer.h
+++ b/cpp/src/arrow/buffer.h
@@ -127,10 +127,13 @@ class ARROW_EXPORT MutableBuffer : public Buffer {
 
 class ARROW_EXPORT ResizableBuffer : public MutableBuffer {
  public:
-  // Change buffer reported size to indicated size, allocating memory if
-  // necessary.  This will ensure that the capacity of the buffer is a multiple
-  // of 64 bytes as defined in Layout.md.
-  virtual Status Resize(int64_t new_size) = 0;
+  /// Change buffer reported size to indicated size, allocating memory if
+  /// necessary.  This will ensure that the capacity of the buffer is a multiple
+  /// of 64 bytes as defined in Layout.md.
+  ///
+  /// @param shrink_to_fit On deactivating this option, the capacity of the Buffer won't
+  /// decrease.
+  virtual Status Resize(int64_t new_size, bool shrink_to_fit = true) = 0;
 
   // Ensure that buffer has enough memory allocated to fit the indicated
   // capacity (and meets the 64 byte padding requirement in Layout.md).
@@ -147,7 +150,7 @@ class ARROW_EXPORT PoolBuffer : public ResizableBuffer {
   explicit PoolBuffer(MemoryPool* pool = nullptr);
   virtual ~PoolBuffer();
 
-  Status Resize(int64_t new_size) override;
+  Status Resize(int64_t new_size, bool shrink_to_fit = true) override;
   Status Reserve(int64_t new_capacity) override;
 
  private: