From: David Reiss Date: Wed, 6 Oct 2010 17:09:46 +0000 (+0000) Subject: THRIFT-922. cpp: When reading strings, borrow first X-Git-Tag: 0.6.0~128 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=ea051ca793bfe68b554d4dd39b48cc237cae53d9;p=common%2Fthrift.git THRIFT-922. cpp: When reading strings, borrow first Attempt to get a pointer to the internal transport buffer before copying onto the heap. This improves performance TFramedTransport and TMemoryBuffer, and with TBufferedTransport if the string fits within the buffer. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1005132 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/protocol/TBinaryProtocol.cpp b/lib/cpp/src/protocol/TBinaryProtocol.cpp index 6a4838b4..39c189ca 100644 --- a/lib/cpp/src/protocol/TBinaryProtocol.cpp +++ b/lib/cpp/src/protocol/TBinaryProtocol.cpp @@ -377,6 +377,15 @@ uint32_t TBinaryProtocol::readStringBody(string& str, int32_t size) { return result; } + // Try to borrow first + const uint8_t* borrow_buf; + uint32_t got = size; + if ((borrow_buf = trans_->borrow(NULL, &got))) { + str.assign((const char*)borrow_buf, size); + trans_->consume(size); + return size; + } + // Use the heap here to prevent stack overflow for v. large strings if (size > string_buf_size_ || string_buf_ == NULL) { void* new_string_buf = std::realloc(string_buf_, (uint32_t)size); diff --git a/lib/cpp/src/transport/TTransport.h b/lib/cpp/src/transport/TTransport.h index b9c35f0b..7417c879 100644 --- a/lib/cpp/src/transport/TTransport.h +++ b/lib/cpp/src/transport/TTransport.h @@ -167,7 +167,10 @@ class TTransport { * * @oaram buf A buffer where the data can be stored if needed. * If borrow doesn't return buf, then the contents of - * buf after the call are undefined. + * buf after the call are undefined. This parameter may be + * NULL to indicate that the caller is not supplying storage, + * but would like a pointer into an internal buffer, if + * available. * @param len *len should initially contain the number of bytes to borrow. * If borrow succeeds, *len will contain the number of bytes * available in the returned pointer. This will be at least