diff --git a/src/lib/utils/SingleWriterConcurrentVector.h b/src/lib/utils/SingleWriterConcurrentVector.h index 35ef90cb7..a41928cb0 100644 --- a/src/lib/utils/SingleWriterConcurrentVector.h +++ b/src/lib/utils/SingleWriterConcurrentVector.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace bts { @@ -14,84 +15,69 @@ class SingleWriterConcurrentVectorIterator; template class SingleWriterConcurrentVector { friend class SingleWriterConcurrentVectorIterator; - struct Position { - int32_t chunk; - uint32_t index; - }; - const std::thread::id _tid; + std::optional _tid; std::vector>> _data; - std::atomic _end; + std::atomic _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 begin() const { - return SingleWriterConcurrentVectorIterator(*this, {0,0}); + return SingleWriterConcurrentVectorIterator(*this, 0); } SingleWriterConcurrentVectorIterator end() const { return SingleWriterConcurrentVectorIterator(*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>()); - auto end = _end.load(); - _end = { end.chunk + 1, 0 }; } }; template // forward iterator class SingleWriterConcurrentVectorIterator { - typedef T value_type; - typedef T& reference; - typedef T* pointer; - typedef std::bidirectional_iterator_tag iterator_category; - const SingleWriterConcurrentVector& _owner; - typename SingleWriterConcurrentVector::Position _pos; + size_t _pos; + mutable T const* _cache; public: + typedef std::bidirectional_iterator_tag iterator_category; SingleWriterConcurrentVectorIterator( const SingleWriterConcurrentVector& owner, - const typename SingleWriterConcurrentVector::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 friend bool operator==(SingleWriterConcurrentVectorIterator& left, SingleWriterConcurrentVectorIterator& right); @@ -165,11 +138,11 @@ void swap(SingleWriterConcurrentVectorIterator& left, SingleWriterConcu } template bool operator==(SingleWriterConcurrentVectorIterator& left, SingleWriterConcurrentVectorIterator& 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 bool operator!=(SingleWriterConcurrentVectorIterator& left, SingleWriterConcurrentVectorIterator& 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; }