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<char> 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(kInitCapacity);
     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:
[email protected]


Reply via email to