Improve SingleWriterConcurrentVector perf (push_back 30-21ns, iterate(2us to 1.3us). Now iterates faster than tbb_concurrent_vector

This commit is contained in:
Eric Harding committed 2019-05-21 14:21:51 -04:00
1 parent 5ae8a66558
commit e815dade3a
1 file changed
+46 -73
+46 -73
View File
@@ -5,6 +5,7 @@
#include <vector>
#include <list>
#include <thread>
#include <optional>
namespace bts {
@@ -14,84 +15,69 @@ class SingleWriterConcurrentVectorIterator;
template <class T, uint32_t CHUNK=1024>
class SingleWriterConcurrentVector {
friend class SingleWriterConcurrentVectorIterator<T,CHUNK>;
struct Position {
int32_t chunk;
uint32_t index;
};
const std::thread::id _tid;
std::optional<std::thread::id> _tid;
std::vector<std::unique_ptr<std::array<T,CHUNK>>> _data;
std::atomic<Position> _end;
std::atomic<size_t> _end;
public:
SingleWriterConcurrentVector():
_tid(std::this_thread::get_id()),
_tid(std::nullopt),
_data(),
_end({-1,0}) {
_end(0) {
add_chunk();
}
void push_back(const T& item) {
bts_assert(std::this_thread::get_id() == _tid);
if (_tid == std::nullopt) { _tid = std::this_thread::get_id(); }
else { bts_assert(std::this_thread::get_id() == _tid); }
auto end = _end.load(std::memory_order_acquire);
if (end.index < CHUNK) {
(*_data[end.chunk])[end.index] = item;
auto c = chunk(end);
if (c < _data.size()) {
(*_data[c])[offset(end)] = item;
// advance pointer only after write is complete
advance_within_chunk();
_end += 1;
}
else {
add_chunk();
return push_back(item);
(*_data[c])[offset(end)] = item;
// advance pointer only after write is complete
_end += 1;
}
}
const T& operator[](uint32_t index) const {
Position p = { (int32_t)(index / CHUNK), index % CHUNK };
return (*this)[p];
}
const T& operator[](Position p) const {
return (*_data[p.chunk])[p.index];
return (*_data[chunk(index)])[offset(index)];
}
SingleWriterConcurrentVectorIterator<T,CHUNK> begin() const {
return SingleWriterConcurrentVectorIterator<T,CHUNK>(*this, {0,0});
return SingleWriterConcurrentVectorIterator<T,CHUNK>(*this, 0);
}
SingleWriterConcurrentVectorIterator<T,CHUNK> end() const {
return SingleWriterConcurrentVectorIterator<T,CHUNK>(*this, _end);
}
size_t size() const {
auto p = _end.load(std::memory_order_acquire);
return p.chunk*CHUNK + p.index;
}
size_t size() const { return _end; }
private:
void advance_within_chunk() {
bts_assert_d(std::this_thread::get_id() == _tid);
auto end = _end.load(std::memory_order_acquire);
_end = { end.chunk, end.index + 1 };
}
size_t chunk(size_t i) const { return i/CHUNK; }
size_t offset(size_t i) const { return i%CHUNK; }
void add_chunk() {
bts_assert_d(std::this_thread::get_id() == _tid);
_data.push_back(std::make_unique<std::array<T,CHUNK>>());
auto end = _end.load();
_end = { end.chunk + 1, 0 };
}
};
template <class T, uint32_t CHUNK>
// forward iterator
class SingleWriterConcurrentVectorIterator {
typedef T value_type;
typedef T& reference;
typedef T* pointer;
typedef std::bidirectional_iterator_tag iterator_category;
const SingleWriterConcurrentVector<T,CHUNK>& _owner;
typename SingleWriterConcurrentVector<T,CHUNK>::Position _pos;
size_t _pos;
mutable T const* _cache;
public:
typedef std::bidirectional_iterator_tag iterator_category;
SingleWriterConcurrentVectorIterator(
const SingleWriterConcurrentVector<T,CHUNK>& owner,
const typename SingleWriterConcurrentVector<T,CHUNK>::Position& pos)
:_owner(owner), _pos(pos)
size_t pos)
: _owner(owner), _pos(pos), _cache(nullptr)
{ }
@@ -100,32 +86,17 @@ public:
_pos = other._pos;
return *this;
}
/*
vector_iterator& operator++() {
size_t element_index = ++my_index;
if( my_item ) {
//TODO: consider using of knowledge about "first_block optimization" here as well?
if( concurrent_vector_base::is_first_element_in_segment(element_index)) {
//if the iterator crosses a segment boundary, the pointer become invalid
//as possibly next segment is in another memory location
my_item= NULL;
} else {
++my_item;
}
}
return *this;
}
todo:
cache value pointer
on increment test if element_index == first element in a segment
if it is null item
*/
SingleWriterConcurrentVectorIterator& operator++() {
++_pos.index;
if (_pos.index >= CHUNK) {
_pos = { _pos.chunk + 1, 0 };
++_pos;
if(_cache) {
if (_pos%CHUNK == 0) {
// If the position passes the chunk boundary we invalidate the pointer
_cache = nullptr;
}
else {
_cache++;
}
}
return *this;
}
@@ -133,22 +104,24 @@ public:
return ++(*this);
}
SingleWriterConcurrentVectorIterator& operator--() {
if (_pos.index == 0) {
_pos = { _pos.chunk - 1, CHUNK-1 };
}
else {
--_pos.index;
}
--_pos;
_cache=nullptr;
return *this;
}
SingleWriterConcurrentVectorIterator operator--(int) {
return --(*this);
}
const T& operator*() const {
return _owner[_pos];
if (_cache) {
return *_cache;
}
else {
_cache = &(_owner[_pos]);
return *_cache;
}
}
const T* operator->() const {
return &_owner[_pos];
return &(**this);
}
template <class A, uint32_t B>
friend bool operator==(SingleWriterConcurrentVectorIterator<A,B>& left, SingleWriterConcurrentVectorIterator<A,B>& right);
@@ -165,11 +138,11 @@ void swap(SingleWriterConcurrentVectorIterator<T,CHUNK>& left, SingleWriterConcu
}
template <class T, uint32_t CHUNK>
bool operator==(SingleWriterConcurrentVectorIterator<T,CHUNK>& left, SingleWriterConcurrentVectorIterator<T,CHUNK>& right) {
return &left._owner == &right._owner && left._pos.chunk == right._pos.chunk && left._pos.index == right._pos.index;
return &left._owner == &right._owner && left._pos == right._pos;
}
template <class T, uint32_t CHUNK>
bool operator!=(SingleWriterConcurrentVectorIterator<T,CHUNK>& left, SingleWriterConcurrentVectorIterator<T,CHUNK>& right) {
return &left._owner != &right._owner || left._pos.chunk != right._pos.chunk || left._pos.index != right._pos.index;
return &left._owner != &right._owner || left._pos != right._pos;
}