Avoid un necessary move when popping circular_q

This commit is contained in:
gabime 2019-09-17 11:56:17 +03:00
parent 2ddd6895e1
commit d52cf87d71
4 changed files with 21 additions and 13 deletions

View File

@ -65,9 +65,9 @@ SPDLOG_INLINE void backtracer::foreach_pop(std::function<void(const details::log
std::lock_guard<std::mutex> lock{mutex_}; std::lock_guard<std::mutex> lock{mutex_};
while (!messages_.empty()) while (!messages_.empty())
{ {
log_msg_buffer popped; auto &front_msg = messages_.front();
messages_.pop_front(popped); messages_.pop_front();
fun(popped); fun(front_msg);
} }
} }
} // namespace details } // namespace details

View File

@ -60,16 +60,19 @@ 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. // Pop item from front.
// If there are no elements in the container, the behavior is undefined. // 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 bool empty() const
{ {
@ -79,8 +82,12 @@ public:
bool full() const bool full() const
{ {
// head is ahead of the tail by 1 // head is ahead of the tail by 1
if(max_items_ > 0)
{
return ((tail_ + 1) % max_items_) == head_; return ((tail_ + 1) % max_items_) == head_;
} }
return true;
}
size_t overrun_counter() const size_t overrun_counter() const
{ {

View File

@ -59,7 +59,8 @@ public:
{ {
return false; return false;
} }
q_.pop_front(popped_item); popped_item = std::move(q_.front());
q_.pop_front();
} }
pop_cv_.notify_one(); pop_cv_.notify_one();
return true; return true;

View File

@ -139,8 +139,8 @@ private:
filename_t current_file = filename(); filename_t current_file = filename();
if (filenames_q_.full()) if (filenames_q_.full())
{ {
filename_t old_filename; auto &old_filename = filenames_q_.front();
filenames_q_.pop_front(old_filename); filenames_q_.pop_front();
bool ok = remove_if_exists(old_filename) == 0; bool ok = remove_if_exists(old_filename) == 0;
if (!ok) if (!ok)
{ {