]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
buffer: make contents_equal() more efficient
authorSage Weil <sage@newdream.net>
Wed, 25 Apr 2012 23:10:30 +0000 (16:10 -0700)
committerSage Weil <sage@newdream.net>
Fri, 27 Apr 2012 01:49:19 +0000 (18:49 -0700)
Iterate both lists in parallel in terms of buffers, and use memcmp() to
do the comparison.

Signed-off-by: Sage Weil <sage@newdream.net>
src/common/buffer.cc

index 64c4d54008c08942e68d4371d87bcf4e08da961f..b990189a1cf33e0a0770f4c23c59683afada792b 100644 (file)
@@ -634,15 +634,45 @@ bool buffer_track_alloc = get_env_bool("CEPH_BUFFER_TRACK");
   {
     if (length() != other.length())
       return false;
-    bufferlist::iterator me = begin();
-    bufferlist::iterator him = other.begin();
-    while (!me.end()) {
-      if (*me != *him)
-        return false;
-      ++me;
-      ++him;
+
+    // buffer-wise comparison
+    if (true) {
+      std::list<ptr>::const_iterator a = _buffers.begin();
+      std::list<ptr>::const_iterator b = other._buffers.begin();
+      unsigned aoff = 0, boff = 0;
+      while (a != _buffers.end()) {
+       unsigned len = a->length() - aoff;
+       if (len > b->length() - boff)
+         len = b->length() - boff;
+       if (memcmp(a->c_str() + aoff, b->c_str() + boff, len) != 0)
+         return false;
+       aoff += len;
+       if (aoff == a->length()) {
+         aoff = 0;
+         a++;
+       }
+       boff += len;
+       if (boff == b->length()) {
+         boff = 0;
+         b++;
+       }
+      }
+      assert(b == other._buffers.end());
+      return true;
+    }
+
+    // byte-wise comparison
+    if (false) {
+      bufferlist::iterator me = begin();
+      bufferlist::iterator him = other.begin();
+      while (!me.end()) {
+       if (*me != *him)
+         return false;
+       ++me;
+       ++him;
+      }
+      return true;
     }
-    return true;
   }
 
   bool buffer::list::is_page_aligned() const