Skip to content

Commit

Permalink
Small tweaks to circular caches to make small performance improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Hammond authored and Simon Hammond committed Nov 22, 2022
1 parent eb1e4b6 commit a5f8a04
Showing 1 changed file with 57 additions and 21 deletions.
78 changes: 57 additions & 21 deletions src/sst/elements/vanadis/datastruct/cqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <cassert>
#include <cstddef>
#include <deque>
#include <bitset>

namespace SST {
namespace Vanadis {
Expand All @@ -27,45 +28,80 @@ template <typename T>
class VanadisCircularQueue
{
public:
VanadisCircularQueue(const size_t size) : max_capacity(size) {}
VanadisCircularQueue(const int size) : max_capacity(size) {
data = new T[size];
clear();

~VanadisCircularQueue() {}
std::bitset<32> size_bits(size);
max_power_two = (size_bits.count() == 1);
bit_mask = max_power_two ? size - 1 : 0;
}

bool empty() { return 0 == data.size(); }
~VanadisCircularQueue() {
delete[] data;
}

bool full() { return max_capacity == data.size(); }
bool empty() { return 0 == count; }
bool full() { return max_capacity == count; }

void push(T item) { data.push_back(item); }
void push(T item) {
assert(count < max_capacity);

T peek() { return data.front(); }
data[tail] = item;
tail = incrementIndex(tail);
count++;
}

T peekAt(const size_t index) { return data.at(index); }
T peek() {
assert(count > 0);

T pop()
{
T tmp = data.front();
data.pop_front();
return tmp;
T peek_me = data[head];
return peek_me;
}

size_t size() const { return data.size(); }
T peekAt(const size_t index) {
assert(index < count);
return data[calculateIndex(index)];
}

size_t capacity() const { return max_capacity; }
T pop()
{
assert(count > 0);

void clear() { data.clear(); }
T pop_me = data[head];
head = incrementIndex(head);
count--;

void removeAt(const size_t index)
{
auto remove_itr = data.begin();
return pop_me;
}

for ( size_t i = 0; i < index; ++i, remove_itr++ ) {}
size_t size() const { return count; }
size_t capacity() const { return max_capacity; }

data.erase(remove_itr);
void clear() {
head = 0;
tail = 0;
count = 0;
}

private:
int calculateIndex(const int index) const {
return max_power_two ? (head + index) & bit_mask : (head + index) % max_capacity;
}

int incrementIndex(const int index) const {
return max_power_two ? (index + 1) & bit_mask : (index + 1) % max_capacity;
}

const size_t max_capacity;
std::deque<T> data;
int head;
int tail;
int count;

T* data;

bool max_power_two;
int bit_mask;
};

} // namespace Vanadis
Expand Down

0 comments on commit a5f8a04

Please sign in to comment.