Date: Tuesday, January 30, 2007 @ 16:43:55
  Author: marc
    Path: /cvsroot/carob/carob

Modified: include/BufferedSocket.hpp (1.3 -> 1.4) src/BufferedSocket.cpp
          (1.6 -> 1.7)

Optimized and simplified sendToSocket() (CAROB-123)
- no caching for BIG sends()
- no while loop
Replaced WriteCache.getWritePtr() and .forward() by new and only one .append()


----------------------------+
 include/BufferedSocket.hpp |   14 +++++++++-----
 src/BufferedSocket.cpp     |   35 +++++++++++++++++------------------
 2 files changed, 26 insertions(+), 23 deletions(-)


Index: carob/include/BufferedSocket.hpp
diff -u carob/include/BufferedSocket.hpp:1.3 
carob/include/BufferedSocket.hpp:1.4
--- carob/include/BufferedSocket.hpp:1.3        Tue Jan 30 10:55:07 2007
+++ carob/include/BufferedSocket.hpp    Tue Jan 30 16:43:55 2007
@@ -16,7 +16,7 @@
  * limitations under the License.
  *
  * Initial developer(s): Gilles Rayrat
- * Contributor(s):
+ * Contributor(s): Marc Herbert
  */
 
 #ifndef _BUFFERED_SOCKET_H_
@@ -24,6 +24,8 @@
 
 #include "JavaSocket.hpp"
 
+#include <string.h>
+
 namespace CarobNS {
 
 /** Size of the input buffer */
@@ -67,12 +69,14 @@
   WriteCache() : write_index(0) {};
   /** Returns the size of data written until now */
   int               getDataSize() { return write_index; }
-  /** Returns a pointer to the next byte to write to */
-  java_byte*        getWritePtr() { return &(buffer[write_index]); }
   /** Returns a pointer to the first available byte */
   java_byte*        getData() { return buffer; }
-  /** Tells that the given length of data has been written */
-  void              forward(int len) { write_index += len; }
+  /** Add data to cache */
+  void              append(const java_byte* src, int len)
+  {
+    memcpy(&buffer[write_index], src, len);
+    write_index += len;
+  }
   /** Discards all data */
   void              reset() { write_index = 0; }
   /** Returns the size left in the buffer */
Index: carob/src/BufferedSocket.cpp
diff -u carob/src/BufferedSocket.cpp:1.6 carob/src/BufferedSocket.cpp:1.7
--- carob/src/BufferedSocket.cpp:1.6    Tue Jan 30 15:32:27 2007
+++ carob/src/BufferedSocket.cpp        Tue Jan 30 16:43:55 2007
@@ -106,25 +106,24 @@
     const void* buf, int len, int flags) const
     throw (SocketIOException, UnexpectedException)
 {
-  int bytesCopied = 0;
-  int bytesToCopyThisTime = len;
-  while (bytesCopied < len)
-  {
-    if (bytesToCopyThisTime > write_cache.getRemainingSize())
-    {
-      // not enough room: copy only # of bytes available in buffer
-      bytesToCopyThisTime = write_cache.getRemainingSize();
-    }
-    memcpy(write_cache.getWritePtr(),
-        static_cast<const java_byte*>(buf)+bytesCopied,
-        bytesToCopyThisTime);
-    write_cache.forward(bytesToCopyThisTime);
-    bytesCopied += bytesToCopyThisTime;
-    if (bytesCopied < len)
-      // did not copy everything => have to flush and copy the remaining bytes
-      flush();
-    bytesToCopyThisTime = len - bytesCopied;
+  // not enough room in the cache?
+  if (len > write_cache.getRemainingSize())
+    // then make some room!
+    flush();
+
+  // still not enough room in the cache?
+  if (len > WRITE_BUFFER_SIZE-1)
+  { // Caching so big data would cost:
+    // 1. more system calls 2. memcpy() cost
+    // => just don't do cache.
+    JavaSocket::sendToSocket(fctName, objName, buf, len, flags);
+    return;
   }
+
+  // Here we have enough cache room. Put data in the cache (and delay
+  // system send())
+  write_cache.append(static_cast<const java_byte*>(buf), len);
+
 }
 
 void BufferedSocket::flush() const throw (SocketIOException, 
UnexpectedException)

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to