From d52cf87d7127b3dad44f5ef1f888f8f006ab0e4e Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 17 Sep 2019 11:56:17 +0300 Subject: [PATCH] Avoid un necessary move when popping circular_q --- include/spdlog/details/backtracer-inl.h | 6 +++--- include/spdlog/details/circular_q.h | 21 ++++++++++++++------- include/spdlog/details/mpmc_blocking_q.h | 3 ++- include/spdlog/sinks/daily_file_sink.h | 4 ++-- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/include/spdlog/details/backtracer-inl.h b/include/spdlog/details/backtracer-inl.h index 910a7192..5d6de81c 100644 --- a/include/spdlog/details/backtracer-inl.h +++ b/include/spdlog/details/backtracer-inl.h @@ -65,9 +65,9 @@ SPDLOG_INLINE void backtracer::foreach_pop(std::function lock{mutex_}; while (!messages_.empty()) { - log_msg_buffer popped; - messages_.pop_front(popped); - fun(popped); + auto &front_msg = messages_.front(); + messages_.pop_front(); + fun(front_msg); } } } // namespace details diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index c5e3d20b..5d15c8ab 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -60,15 +60,18 @@ public: } } + // Return reference to the front item. + // If there are no elements in the container, the behavior is undefined. + T& front() + { + return v_[head_]; + } + // Pop item from front. // If there are no elements in the container, the behavior is undefined. - void pop_front(T &popped_item) + void pop_front() { - if (max_items_ > 0) - { - popped_item = std::move(v_[head_]); - head_ = (head_ + 1) % max_items_; - } + head_ = (head_ + 1) % max_items_; } bool empty() const @@ -79,7 +82,11 @@ public: bool full() const { // head is ahead of the tail by 1 - return ((tail_ + 1) % max_items_) == head_; + if(max_items_ > 0) + { + return ((tail_ + 1) % max_items_) == head_; + } + return true; } size_t overrun_counter() const diff --git a/include/spdlog/details/mpmc_blocking_q.h b/include/spdlog/details/mpmc_blocking_q.h index d8a1efe2..1a0a303c 100644 --- a/include/spdlog/details/mpmc_blocking_q.h +++ b/include/spdlog/details/mpmc_blocking_q.h @@ -59,7 +59,8 @@ public: { return false; } - q_.pop_front(popped_item); + popped_item = std::move(q_.front()); + q_.pop_front(); } pop_cv_.notify_one(); return true; diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 7dbc9916..59cf52d4 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -139,8 +139,8 @@ private: filename_t current_file = filename(); if (filenames_q_.full()) { - filename_t old_filename; - filenames_q_.pop_front(old_filename); + auto &old_filename = filenames_q_.front(); + filenames_q_.pop_front(); bool ok = remove_if_exists(old_filename) == 0; if (!ok) {