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 6a4838b..39c189c 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.cpp
+++ b/lib/cpp/src/protocol/TBinaryProtocol.cpp
@@ -377,6 +377,15 @@
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 b9c35f0..7417c87 100644
--- a/lib/cpp/src/transport/TTransport.h
+++ b/lib/cpp/src/transport/TTransport.h
@@ -167,7 +167,10 @@
*
* @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