]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
buffer.cc: short-circuit copy_in for small lengths 5259/head
authorPiotr Dałek <piotr.dalek@ts.fujitsu.com>
Thu, 16 Jul 2015 09:19:37 +0000 (11:19 +0200)
committerPiotr Dałek <piotr.dalek@ts.fujitsu.com>
Tue, 4 Aug 2015 07:59:13 +0000 (09:59 +0200)
For small lengths (below 64 bytes) use shorter code paths, this
increases performance of function by up to 40%.

Signed-off-by: Piotr Dałek <piotr.dalek@ts.fujitsu.com>
src/common/buffer.cc

index 7e34a03c29ce978e730779bf191b1024f2b6a52b..5eccdfe054987be595f146fa13e8b9cce1594ff4 100644 (file)
@@ -946,9 +946,49 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER;
     assert(_raw);
     assert(o <= _len);
     assert(o+l <= _len);
+    char* dest = _raw->data + _off + o;
     if (crc_reset)
         _raw->invalidate_crc();
-    memcpy(c_str()+o, src, l);
+    if (l < 64) {
+        switch (l) {
+            case 1:
+                *((uint8_t*)(dest)) = *((uint8_t*)(src));
+                return;
+            case 2:
+                *((uint16_t*)(dest)) = *((uint16_t*)(src));
+                return;
+            case 3:
+                *((uint16_t*)(dest)) = *((uint16_t*)(src));
+                *((uint8_t*)(dest+2)) = *((uint8_t*)(src+2));
+                return;
+            case 4:
+                *((uint32_t*)(dest)) = *((uint32_t*)(src));
+                return;
+            case 8:
+                *((uint64_t*)(dest)) = *((uint64_t*)(src));
+                return;
+            default:
+                int cursor = 0;
+                while (l >= sizeof(uint64_t)) {
+                    *((uint64_t*)(dest + cursor)) = *((uint64_t*)(src + cursor));
+                    cursor += sizeof(uint64_t);
+                    l -= sizeof(uint64_t);
+                }
+                while (l >= sizeof(uint32_t)) {
+                    *((uint32_t*)(dest + cursor)) = *((uint32_t*)(src + cursor));
+                    cursor += sizeof(uint32_t);
+                    l -= sizeof(uint32_t);
+                }
+                while (l > 0) {
+                    *(dest + cursor) = *(src + cursor);
+                    cursor++;
+                    l--;
+                }
+                return;
+        }
+    } else {
+        memcpy(dest, src, l);
+    }
   }
 
   void buffer::ptr::zero(bool crc_reset)