You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@thrift.apache.org by GitBox <gi...@apache.org> on 2020/05/30 13:13:50 UTC

[GitHub] [thrift] Jens-G commented on a change in pull request #2030: THRIFT-5114: Simplified reallocation of TMemoryBuffer

Jens-G commented on a change in pull request #2030:
URL: https://github.com/apache/thrift/pull/2030#discussion_r432840771



##########
File path: lib/cpp/src/thrift/transport/TBufferTransports.cpp
##########
@@ -360,15 +360,14 @@ void TMemoryBuffer::ensureCanWrite(uint32_t len) {
     throw TTransportException("Insufficient space in external MemoryBuffer");
   }
 
+  const uint32_t current_used = bufferSize_ - avail;
+  const uint32_t required_buffer_size = len + current_used;
+  const uint64_t new_size = static_cast<uint64_t>(std::exp2(std::ceil(std::log2(static_cast<double>(required_buffer_size)))));
+
   // Grow the buffer as necessary.
-  uint64_t new_size = bufferSize_;
-  while (len > avail) {
-    new_size = new_size > 0 ? new_size * 2 : 1;
-    if (new_size > maxBufferSize_) {
-      throw TTransportException(TTransportException::BAD_ARGS,
-                                "Internal buffer size overflow");
-    }
-    avail = available_write() + (static_cast<uint32_t>(new_size) - bufferSize_);
+  if (new_size > maxBufferSize_) {
+    throw TTransportException(TTransportException::BAD_ARGS,

Review comment:
       Scenario: the caluclated new_size is above maxBufferSize, but requured_buffer_size would be in the limit. Now this error is thrown even though it should not, only because of the calculations above.
   
   Questions:
    * is that a possible scenario or may I overlook sth?
    * if yes, would it make sense to simply limit the calculated new_size to maxBufferSize in such a case and move on?
   




----------------------------------------------------------------
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