From commits-return-12886-archive-asf-public=cust-asf.ponee.io@tvm.apache.org Tue May 5 20:31:25 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 5FF02180626 for ; Tue, 5 May 2020 22:31:25 +0200 (CEST) Received: (qmail 74092 invoked by uid 500); 5 May 2020 20:31:24 -0000 Mailing-List: contact commits-help@tvm.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tvm.apache.org Delivered-To: mailing list commits@tvm.apache.org Received: (qmail 74083 invoked by uid 99); 5 May 2020 20:31:24 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 May 2020 20:31:24 +0000 From: =?utf-8?q?GitBox?= To: commits@tvm.apache.org Subject: =?utf-8?q?=5BGitHub=5D_=5Bincubator-tvm=5D_u99127_commented_on_a_change_in_p?= =?utf-8?q?ull_request_=235516=3A_=5BRPC=5D=5BBUGFIX=5D=5BBACKPORT-0=2E6=5D_?= =?utf-8?q?Fix_bug_in_rpc_ring_buffer_shrink?= Message-ID: <158871068473.26397.12683971057093971711.asfpy@gitbox.apache.org> Date: Tue, 05 May 2020 20:31:24 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit References: In-Reply-To: u99127 commented on a change in pull request #5516: URL: https://github.com/apache/incubator-tvm/pull/5516#discussion_r420387251 ########## File path: src/support/ring_buffer.h ########## @@ -63,17 +67,28 @@ class RingBuffer { size_t ncopy = head_ptr_ + bytes_available_ - old_size; memcpy(&ring_[0] + old_size, &ring_[0], ncopy); } - } else if (ring_.size() > n * 8 && ring_.size() > kInitCapacity && bytes_available_ > 0) { - // shrink too large temporary buffer to avoid out of memory on some embedded devices + } else if (ring_.size() > n * 8 && + ring_.size() > kInitCapacity) { + // shrink too large temporary buffer to + // avoid out of memory on some embedded devices size_t old_bytes = bytes_available_; - std::vector tmp(old_bytes); - Read(&tmp[0], old_bytes); - ring_.resize(kInitCapacity); + if (old_bytes != 0) { + Read(&tmp[0], old_bytes); + } + + size_t new_size = kInitCapacity; + new_size = std::max(new_size, bytes_available_); + new_size = std::max(new_size, n); + + ring_.resize(new_size); ring_.shrink_to_fit(); - memcpy(&ring_[0], &tmp[0], old_bytes); + if (old_bytes != 0) { + memcpy(&ring_[0], &tmp[0], old_bytes); + } + Review comment: Not performance critical but I think it could be better recast as below. if (bytes_available != 0) { ... existing code } else { ring_.resize(new_size); ring_.shrink_to_fit(); } Avoids unnecessary runtime calls to std::max with 0 and additional checks that can go. And for bonus points, encapsulate ring_.resize(new_size) and ring_.shrink_to_fit into a small helper function if you so choose. ---------------------------------------------------------------- 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