From: Jens Geyer Date: Mon, 14 Jul 2014 17:50:45 +0000 (+0200) Subject: THRIFT-2541 reclaim TFramedTransport's read and write buffers for thrift cpp X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=1a3632351ee2e16635962fb133e65a3816ce4c93;p=common%2Fthrift.git THRIFT-2541 reclaim TFramedTransport's read and write buffers for thrift cpp Client: C++ Patch: Huabin <4130944@qq.com> --- diff --git a/lib/cpp/src/thrift/transport/TBufferTransports.cpp b/lib/cpp/src/thrift/transport/TBufferTransports.cpp index d819868a..69077f71 100644 --- a/lib/cpp/src/thrift/transport/TBufferTransports.cpp +++ b/lib/cpp/src/thrift/transport/TBufferTransports.cpp @@ -268,6 +268,17 @@ void TFramedTransport::flush() { // Flush the underlying transport. transport_->flush(); + + // reclaim write buffer + if (wBufSize_ > bufReclaimThresh_) { + wBufSize_ = DEFAULT_BUFFER_SIZE; + wBuf_.reset(new uint8_t[wBufSize_]); + setWriteBuffer(wBuf_.get(), wBufSize_); + + // reset wBase_ with a pad for the frame size + int32_t pad = 0; + wBase_ = wBuf_.get() + sizeof(pad); + } } uint32_t TFramedTransport::writeEnd() { @@ -285,7 +296,15 @@ const uint8_t* TFramedTransport::borrowSlow(uint8_t* buf, uint32_t* len) { uint32_t TFramedTransport::readEnd() { // include framing bytes - return static_cast(rBound_ - rBuf_.get() + sizeof(uint32_t)); + uint32_t bytes_read = static_cast(rBound_ - rBuf_.get() + sizeof(uint32_t)); + + if (rBufSize_ > bufReclaimThresh_) { + rBufSize_ = 0; + rBuf_.reset(); + setReadBuffer(rBuf_.get(), rBufSize_); + } + + return bytes_read; } void TMemoryBuffer::computeRead(uint32_t len, uint8_t** out_start, uint32_t* out_give) { diff --git a/lib/cpp/src/thrift/transport/TBufferTransports.h b/lib/cpp/src/thrift/transport/TBufferTransports.h index cd6ecea7..71bdd845 100644 --- a/lib/cpp/src/thrift/transport/TBufferTransports.h +++ b/lib/cpp/src/thrift/transport/TBufferTransports.h @@ -335,16 +335,19 @@ class TFramedTransport , wBufSize_(DEFAULT_BUFFER_SIZE) , rBuf_() , wBuf_(new uint8_t[wBufSize_]) + , bufReclaimThresh_(std::numeric_limits::max()) { initPointers(); } - TFramedTransport(boost::shared_ptr transport, uint32_t sz) + TFramedTransport(boost::shared_ptr transport, uint32_t sz, + uint32_t bufReclaimThresh = std::numeric_limits::max()) : transport_(transport) , rBufSize_(0) , wBufSize_(sz) , rBuf_() , wBuf_(new uint8_t[wBufSize_]) + , bufReclaimThresh_(bufReclaimThresh) { initPointers(); } @@ -414,6 +417,7 @@ class TFramedTransport uint32_t wBufSize_; boost::scoped_array rBuf_; boost::scoped_array wBuf_; + uint32_t bufReclaimThresh_; }; /**