Skip to content

Commit

Permalink
#1950: utils: use circular buffer in scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
lifflander committed Sep 14, 2022
1 parent 20037c6 commit 57a7911
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/vt/scheduler/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define INCLUDED_VT_SCHEDULER_QUEUE_H

#include "vt/config.h"
#include "vt/utils/container/circular_buffer.h"

#include <queue>

Expand All @@ -56,24 +57,44 @@ struct Queue {
Queue(Queue const&) = default;
Queue(Queue&&) = default;

void push(T elm) { impl_.push(elm); }

void emplace(T&& elm) { impl_.emplace(std::forward<T>(elm)); }
void push(T elm) {
if (buf_.full()) {
impl_.push(elm);
} else {
buf_.push(elm);
}
}

T pop() { auto elm = std::move(impl_.front()); impl_.pop(); return elm; }
void emplace(T&& elm) {
if (buf_.full()) {
impl_.emplace(std::move(elm));
} else {
buf_.push(std::move(elm));
}
}

T const& top() { return impl_.front(); }
T pop() {
if (buf_.empty()) {
auto elm = std::move(impl_.front()); impl_.pop(); return elm;
} else {
return buf_.pop();
}
}

std::size_t size() const { return impl_.size(); }
std::size_t size() const {
return buf_.len() + impl_.size();
}

bool empty() const { return impl_.empty(); }
bool empty() const { return impl_.empty() and buf_.empty(); }

template <typename Serializer>
void serialize(Serializer& s) {
s | impl_;
s | buf_;
}

private:
util::container::CircularBuffer<T, 64> buf_;
std::queue<T, std::deque<T>> impl_;
};

Expand Down

0 comments on commit 57a7911

Please sign in to comment.