Slight optimization in write() call
authorBrian Aker <brian@tangent.org>
Wed, 23 Jan 2008 03:19:52 +0000 (19:19 -0800)
committerBrian Aker <brian@tangent.org>
Wed, 23 Jan 2008 03:19:52 +0000 (19:19 -0800)
lib/memcached_io.c

index be186c4b0985d0ddbd74d935dab8d1f70993f267..52d912975e0023c1d562d1891bd5b18513398a06 100644 (file)
@@ -166,22 +166,33 @@ ssize_t memcached_io_write(memcached_server_st *ptr,
 {
   unsigned long long x;
 
-  for (x= 0; x < length; x++)
+  if (length < (MEMCACHED_MAX_BUFFER - ptr->write_buffer_offset ))
   {
-    ptr->write_buffer[ptr->write_buffer_offset]= buffer[x];
-    ptr->write_buffer_offset++;
-    WATCHPOINT_ASSERT(ptr->write_buffer_offset <= MEMCACHED_MAX_BUFFER);
+    char *write_ptr;
 
-    if (ptr->write_buffer_offset == MEMCACHED_MAX_BUFFER)
+    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++)
     {
-      memcached_return rc;
-      ssize_t sent_length;
+      ptr->write_buffer[ptr->write_buffer_offset]= buffer[x];
+      ptr->write_buffer_offset++;
+      WATCHPOINT_ASSERT(ptr->write_buffer_offset <= MEMCACHED_MAX_BUFFER);
 
-      sent_length= io_flush(ptr, &rc);
-      if (sent_length == -1)
-        return -1;
+      if (ptr->write_buffer_offset == MEMCACHED_MAX_BUFFER)
+      {
+        memcached_return rc;
+        ssize_t sent_length;
 
-      WATCHPOINT_ASSERT(sent_length == MEMCACHED_MAX_BUFFER);
+        sent_length= io_flush(ptr, &rc);
+        if (sent_length == -1)
+          return -1;
+
+        WATCHPOINT_ASSERT(sent_length == MEMCACHED_MAX_BUFFER);
+      }
     }
   }