Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 43A92200BF5 for ; Sat, 7 Jan 2017 22:16:47 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 377FF160B3C; Sat, 7 Jan 2017 21:16:47 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 7EB35160B27 for ; Sat, 7 Jan 2017 22:16:46 +0100 (CET) Received: (qmail 95118 invoked by uid 500); 7 Jan 2017 21:16:45 -0000 Mailing-List: contact commits-help@arrow.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@arrow.apache.org Delivered-To: mailing list commits@arrow.apache.org Received: (qmail 95107 invoked by uid 99); 7 Jan 2017 21:16:45 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 07 Jan 2017 21:16:45 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C36A9DFA00; Sat, 7 Jan 2017 21:16:44 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: wesm@apache.org To: commits@arrow.apache.org Message-Id: <3b01ff6bede6412c89e41e3e8c316a28@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: arrow git commit: ARROW-360: C++: Add method to shrink PoolBuffer using realloc Date: Sat, 7 Jan 2017 21:16:44 +0000 (UTC) archived-at: Sat, 07 Jan 2017 21:16:47 -0000 Repository: arrow Updated Branches: refs/heads/master 74685f386 -> 7d1f1cf91 ARROW-360: C++: Add method to shrink PoolBuffer using realloc Added no explicit unit test for this as I want to keep the freedom to the allocator in the future to advise the PoolBuffer on an acceptable minimal size. In some cases it might be worth it to occupy a whole page. Resizing to a smaller size is tested though, so we already have a unit test ensuring that this code runs smoothly. Author: Uwe L. Korn Closes #272 from xhochy/ARROW-360 and squashes the following commits: f4992ee [Uwe L. Korn] Adjust DCHECK for zero size arrays 040d3b4 [Uwe L. Korn] ARROW-360: C++: Add method to shrink PoolBuffer using realloc Project: http://git-wip-us.apache.org/repos/asf/arrow/repo Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/7d1f1cf9 Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/7d1f1cf9 Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/7d1f1cf9 Branch: refs/heads/master Commit: 7d1f1cf91b798259de18ebd772b213e12a6dd194 Parents: 74685f3 Author: Uwe L. Korn Authored: Sat Jan 7 16:16:31 2017 -0500 Committer: Wes McKinney Committed: Sat Jan 7 16:16:31 2017 -0500 ---------------------------------------------------------------------- cpp/src/arrow/buffer.cc | 20 +++++++++++++++++++- cpp/src/arrow/test-util.h | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/arrow/blob/7d1f1cf9/cpp/src/arrow/buffer.cc ---------------------------------------------------------------------- diff --git a/cpp/src/arrow/buffer.cc b/cpp/src/arrow/buffer.cc index 6d55f88..2e64ffd 100644 --- a/cpp/src/arrow/buffer.cc +++ b/cpp/src/arrow/buffer.cc @@ -92,7 +92,25 @@ Status PoolBuffer::Reserve(int64_t new_capacity) { } Status PoolBuffer::Resize(int64_t new_size) { - RETURN_NOT_OK(Reserve(new_size)); + if (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) { + // Buffer hasn't got yet the requested size. + if (new_size == 0) { + pool_->Free(mutable_data_, capacity_); + capacity_ = 0; + mutable_data_ = nullptr; + data_ = nullptr; + } else { + RETURN_NOT_OK(pool_->Reallocate(capacity_, new_size, &mutable_data_)); + data_ = mutable_data_; + capacity_ = new_size; + } + } + } size_ = new_size; return Status::OK(); } http://git-wip-us.apache.org/repos/asf/arrow/blob/7d1f1cf9/cpp/src/arrow/test-util.h ---------------------------------------------------------------------- diff --git a/cpp/src/arrow/test-util.h b/cpp/src/arrow/test-util.h index e595749..f2da824 100644 --- a/cpp/src/arrow/test-util.h +++ b/cpp/src/arrow/test-util.h @@ -184,7 +184,7 @@ static inline void random_ascii(int n, uint32_t seed, uint8_t* out) { template void rand_uniform_int(int n, uint32_t seed, T min_value, T max_value, T* out) { - DCHECK(out); + DCHECK(out || (n == 0)); std::mt19937 gen(seed); std::uniform_int_distribution d(min_value, max_value); for (int i = 0; i < n; ++i) {