Author: dreiss
Date: Wed Oct  6 17:09:47 2010
New Revision: 1005133

URL: http://svn.apache.org/viewvc?rev=1005133&view=rev
Log:
THRIFT-922. cpp: Add shortcutted version of readAll() in TBufferBase

Just perform a memcpy() if all of the requested data is available in the
buffer.  This improves performance a little in the common case.  It has
a bigger impact with the upcoming template changes.

Modified:
    incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.h
    incubator/thrift/trunk/lib/cpp/src/transport/TTransport.h

Modified: incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.h
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.h?rev=1005133&r1=1005132&r2=1005133&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.h (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TBufferTransports.h Wed Oct  6 
17:09:47 2010
@@ -69,6 +69,19 @@ class TBufferBase : public TTransport {
   }
 
   /**
+   * Shortcutted version of readAll.
+   */
+  uint32_t readAll(uint8_t* buf, uint32_t len) {
+    uint8_t* new_rBase = rBase_ + len;
+    if (TDB_LIKELY(new_rBase <= rBound_)) {
+      std::memcpy(buf, rBase_, len);
+      rBase_ = new_rBase;
+      return len;
+    }
+    return facebook::thrift::transport::readAll(*this, buf, len);
+  }
+
+  /**
    * Fast-path write.
    *
    * When we have enough empty space in our buffer to accomodate the write, we

Modified: incubator/thrift/trunk/lib/cpp/src/transport/TTransport.h
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/transport/TTransport.h?rev=1005133&r1=1005132&r2=1005133&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/transport/TTransport.h (original)
+++ incubator/thrift/trunk/lib/cpp/src/transport/TTransport.h Wed Oct  6 
17:09:47 2010
@@ -28,6 +28,27 @@
 namespace apache { namespace thrift { namespace transport {
 
 /**
+ * Helper template to hoist readAll implementation out of TTransport
+ */
+template <class Transport_>
+uint32_t readAll(Transport_ &trans, uint8_t* buf, uint32_t len) {
+  uint32_t have = 0;
+  uint32_t get = 0;
+
+  while (have < len) {
+    get = trans.read(buf+have, len-have);
+    if (get <= 0) {
+      throw TTransportException(TTransportException::END_OF_FILE,
+                                "No more data to read.");
+    }
+    have += get;
+  }
+
+  return have;
+}
+
+
+/**
  * Generic interface for a method of transporting data. A TTransport may be
  * capable of either reading or writing, but not necessarily both.
  *
@@ -96,19 +117,7 @@ class TTransport {
    * @throws TTransportException If insufficient data was read
    */
   virtual uint32_t readAll(uint8_t* buf, uint32_t len) {
-    uint32_t have = 0;
-    uint32_t get = 0;
-
-    while (have < len) {
-      get = read(buf+have, len-have);
-      if (get <= 0) {
-        throw TTransportException(TTransportException::END_OF_FILE,
-                                 "No more data to read.");
-      }
-      have += get;
-    }
-
-    return have;
+    return apache::thrift::transport::readAll(*this, buf, len);
   }
 
   /**


Reply via email to