Modifications for IO performance.
authorBrian Aker <brian@tangent.org>
Wed, 23 Jan 2008 04:18:54 +0000 (20:18 -0800)
committerBrian Aker <brian@tangent.org>
Wed, 23 Jan 2008 04:18:54 +0000 (20:18 -0800)
ChangeLog
lib/memcached_io.c

index 208aa0a9298592823a0bffc6ba8599713e88139e..cfbf0cdc6df14007ac9b2fe131e596f0daa7b1e5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,5 @@
+  * Slight performance increase in both read() and write().
+
 0.14 Tue Jan 22 06:21:49 PST 2008
   * For for bug found by Evan Weaver where increment() was not returning
     propper error of value was not found.
index 52d912975e0023c1d562d1891bd5b18513398a06..9e63fe73f1c45c609894f0761ff2dde343c8ce42 100644 (file)
@@ -93,8 +93,11 @@ ssize_t memcached_io_read(memcached_server_st *ptr,
         data_read= read(ptr->fd, 
                         ptr->read_buffer, 
                         MEMCACHED_MAX_BUFFER);
-        if (data_read == -1)
+        if (data_read)
+          break;
+        else if (data_read == -1)
         {
+          ptr->cached_errno= errno;
           switch (errno)
           {
           case EAGAIN:
@@ -105,21 +108,15 @@ ssize_t memcached_io_read(memcached_server_st *ptr,
 
               if (rc == MEMCACHED_SUCCESS)
                 continue;
-
-              memcached_quit_server(ptr, 1);
-              return -1;
             }
+          /* fall trough */
           default:
             {
               memcached_quit_server(ptr, 1);
-              ptr->cached_errno= errno;
               return -1;
             }
           }
         }
-        else if (data_read)
-          break;
-        /* If zero, just keep looping unless testing, then assert() */
         else
         {
           WATCHPOINT_ASSERT(0);
@@ -148,10 +145,10 @@ ssize_t memcached_io_read(memcached_server_st *ptr,
     else
     {
       *buffer_ptr= *ptr->read_ptr;
-      length--;
       ptr->read_ptr++;
       ptr->read_buffer_length--;
       buffer_ptr++;
+      break;
     }
 
     if (found_eof)
@@ -164,35 +161,34 @@ ssize_t memcached_io_read(memcached_server_st *ptr,
 ssize_t memcached_io_write(memcached_server_st *ptr,
                            char *buffer, size_t length, char with_flush)
 {
-  unsigned long long x;
+  size_t original_length;
+
+  original_length= length;
 
-  if (length < (MEMCACHED_MAX_BUFFER - ptr->write_buffer_offset ))
+  while (length)
   {
     char *write_ptr;
+    size_t should_write;
 
+    should_write= MEMCACHED_MAX_BUFFER - ptr->write_buffer_offset;
     write_ptr= ptr->write_buffer + ptr->write_buffer_offset;
-    memcpy(write_ptr, buffer, length);
-    ptr->write_buffer_offset+= length;
-  }
-  else
-  {
-    for (x= 0; x < length; x++)
-    {
-      ptr->write_buffer[ptr->write_buffer_offset]= buffer[x];
-      ptr->write_buffer_offset++;
-      WATCHPOINT_ASSERT(ptr->write_buffer_offset <= MEMCACHED_MAX_BUFFER);
 
-      if (ptr->write_buffer_offset == MEMCACHED_MAX_BUFFER)
-      {
-        memcached_return rc;
-        ssize_t sent_length;
+    should_write= (should_write < length) ? should_write : length;
 
-        sent_length= io_flush(ptr, &rc);
-        if (sent_length == -1)
-          return -1;
+    memcpy(write_ptr, buffer, should_write);
+    ptr->write_buffer_offset+= should_write;
+    length-= should_write;
 
-        WATCHPOINT_ASSERT(sent_length == MEMCACHED_MAX_BUFFER);
-      }
+    if (ptr->write_buffer_offset == MEMCACHED_MAX_BUFFER)
+    {
+      memcached_return rc;
+      ssize_t sent_length;
+
+      sent_length= io_flush(ptr, &rc);
+      if (sent_length == -1)
+        return -1;
+
+      WATCHPOINT_ASSERT(sent_length == MEMCACHED_MAX_BUFFER);
     }
   }
 
@@ -203,7 +199,7 @@ ssize_t memcached_io_write(memcached_server_st *ptr,
       return -1;
   }
 
-  return length;
+  return original_length;
 }
 
 memcached_return memcached_io_close(memcached_server_st *ptr)