]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/bl: c_str() examines length instead of no. of bptrs.
authorRadoslaw Zarzynski <rzarzyns@redhat.com>
Mon, 19 Jul 2021 13:55:18 +0000 (13:55 +0000)
committerRadoslaw Zarzynski <rzarzyns@redhat.com>
Tue, 20 Jul 2021 14:29:34 +0000 (14:29 +0000)
This approach has 2 adventages:
* it takes into consideration the specical case of a bufferlist
  that carries solely empty bptrs while
* allowing compilers to optimize-out the load-and-check of `_len`
  we already do in `rebuild()` which is called by `c_str()`.

  ```cpp
  void buffer::list::rebuild()
  {
    if (_len == 0) {
      _carriage = &always_empty_bptr;
      _buffers.clear_and_dispose();
      _num = 0;
      return;
    }
    // ...
  }
  ```

Signed-off-by: Radoslaw Zarzynski <rzarzyns@redhat.com>
src/common/buffer.cc

index a4df5e754b027ed1347bc384bd3a666395b1291a..8ade26dc911789a86e7540e5489ec65e2abe93f1 100644 (file)
@@ -1523,16 +1523,15 @@ static ceph::spinlock debug_lock;
    */
   char *buffer::list::c_str()
   {
-    switch (get_num_buffers()) {
-    case 0:
-      // no buffers
-      return nullptr;
-    case 1:
-      // good, we're already contiguous.
-      break;
-    default:
+    if (length() == 0) {
+      return nullptr;                         // no non-empty buffers
+    }
+
+    auto iter = std::cbegin(_buffers);
+    ++iter;
+
+    if (iter != std::cend(_buffers)) {
       rebuild();
-      break;
     }
     return _buffers.front().c_str();
   }