From 9d1a84a59213de308881c84691bfc91148ed618e Mon Sep 17 00:00:00 2001 From: Radoslaw Zarzynski Date: Thu, 5 Aug 2021 00:04:57 +0000 Subject: [PATCH] common/bl: abstract from _carriage checking in c_str(). This is intended to enchance readability and sligthly generalize the previous, `_carriage`-aware optimization. Signed-off-by: Radoslaw Zarzynski --- src/common/buffer.cc | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/common/buffer.cc b/src/common/buffer.cc index 2b534402f6dca..1b36b0543c750 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -1523,18 +1523,17 @@ static ceph::spinlock debug_lock; */ char *buffer::list::c_str() { - if (length() == 0) { + if (const auto len = length(); len == 0) { return nullptr; // no non-empty buffers - } - - const auto second = std::next(std::cbegin(_buffers)); - // splice() tries to not waste our appendable space; to carry - // it an empty bptr is added at the end. we account for that. - const auto end = _carriage->length() == 0 && \ - _carriage == &_buffers.back() ? buffers_t::const_iterator(_carriage) - : std::cend(_buffers); - if (second != end) { + } else if (len != _buffers.front().length()) { rebuild(); + } else { + // there are two *main* scenarios that hit this branch: + // 1. bufferlist with single, non-empty buffer; + // 2. bufferlist with single, non-empty buffer followed by + // empty buffer. splice() tries to not waste our appendable + // space; to carry it an empty bptr is added at the end. + // we account for these and don't rebuild unnecessarily } return _buffers.front().c_str(); } -- 2.39.5