Skip to content

Commit

Permalink
Improve performance for empty queue (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
herrcristi authored Dec 25, 2024
1 parent 0485755 commit 60fa286
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
8 changes: 5 additions & 3 deletions include/base_queue_wait.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,14 @@ namespace small {
//
inline small::WaitFlags test_and_get(T *elem, TimePoint *time_wait_until)
{
*time_wait_until = TimeClock::now() + std::chrono::minutes(60);
auto ret = m_parent_caller.test_and_get(elem, time_wait_until);
*time_wait_until = TimeClock::now() + std::chrono::minutes(60);
bool is_empty_after_get = false;

auto ret = m_parent_caller.test_and_get(elem, time_wait_until, &is_empty_after_get);
auto is_exit_ret = ret == small::WaitFlags::kExit_Force || ret == small::WaitFlags::kExit_When_Done;

// notify condition if q is empty or exit force
if (is_exit_ret || is_empty_queue()) {
if (is_exit_ret || is_empty_after_get) {
m_queues_exit_condition.notify_all();
}

Expand Down
6 changes: 5 additions & 1 deletion include/lock_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,10 @@ namespace small {
//
// check for front element
//
inline small::WaitFlags test_and_get(T *elem, typename BaseQueueWait::TimePoint * /* time_wait_until */)
inline small::WaitFlags test_and_get(T *elem, typename BaseQueueWait::TimePoint * /* time_wait_until */, bool *is_empty_after_get)
{
*is_empty_after_get = true;

if (is_exit_force()) {
return small::WaitFlags::kExit_Force;
}
Expand All @@ -260,6 +262,8 @@ namespace small {
}
m_queue.pop_front();

*is_empty_after_get = m_queue.empty();

return small::WaitFlags::kElement;
}

Expand Down
12 changes: 9 additions & 3 deletions include/prio_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,10 @@ namespace small {
}

// extract from queue
inline small::WaitFlags test_and_get(T *elem, typename BaseQueueWait::TimePoint * /* time_wait_until */)
inline small::WaitFlags test_and_get(T *elem, typename BaseQueueWait::TimePoint * /* time_wait_until */, bool *is_empty_after_get)
{
*is_empty_after_get = true;

if (is_exit_force()) {
return small::WaitFlags::kExit_Force;
}
Expand Down Expand Up @@ -470,7 +472,9 @@ namespace small {
}

// get elem
return pop_front(*queue, elem);
auto ret = pop_front(*queue, elem);
*is_empty_after_get = empty();
return ret;
}

// reset all stats
Expand All @@ -482,7 +486,9 @@ namespace small {
++prev_stats.m_count_executed;

// get elem
return pop_front(queue, elem);
auto ret = pop_front(queue, elem);
*is_empty_after_get = empty();
return ret;
}

// here all queues are empty
Expand Down
7 changes: 6 additions & 1 deletion include/time_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ namespace small {
//
friend BaseQueueWait;

inline small::WaitFlags test_and_get(T *elem, typename BaseQueueWait::TimePoint *time_wait_until)
inline small::WaitFlags test_and_get(T *elem, typename BaseQueueWait::TimePoint *time_wait_until, bool *is_empty_after_get)
{
*is_empty_after_get = true;

if (is_exit_force()) {
return small::WaitFlags::kExit_Force;
}
Expand All @@ -357,6 +359,7 @@ namespace small {
// check time
*time_wait_until = get_next_time();
if (*time_wait_until > TimeClock::now()) {
*is_empty_after_get = false;
return small::WaitFlags::kWait;
}

Expand All @@ -366,6 +369,8 @@ namespace small {
}
m_queue.pop();

*is_empty_after_get = m_queue.empty();

return small::WaitFlags::kElement;
}

Expand Down

0 comments on commit 60fa286

Please sign in to comment.