diff --git a/flow/Arena.h b/flow/Arena.h index 2758ae5925..f851c72f94 100644 --- a/flow/Arena.h +++ b/flow/Arena.h @@ -1010,21 +1010,21 @@ public: using pointer = value_type*; using reference = value_type&; friend class SmallVectorRef; - template - friend bool operator<(const iterator_impl&, const iterator_impl&); - template - friend bool operator>(const iterator_impl&, const iterator_impl&); - template - friend bool operator<=(const iterator_impl&, const iterator_impl&); - template - friend bool operator>=(const iterator_impl&, const iterator_impl&); - template + template + friend bool operator<(const iterator_impl&, const iterator_impl&); + template + friend bool operator>(const iterator_impl&, const iterator_impl&); + template + friend bool operator<=(const iterator_impl&, const iterator_impl&); + template + friend bool operator>=(const iterator_impl&, const iterator_impl&); + template friend self_t operator+(const iterator_impl&, difference_type); - template + template friend self_t operator+(difference_type, const self_t&); - template + template friend self_t operator-(const iterator_impl&, difference_type); - template + template friend difference_type operator-(iterator_impl, self_t); self_t& operator++() { @@ -1033,7 +1033,7 @@ public: } self_t operator++(int) { auto res = *this; - ++res; + ++(*this); return res; } self_t& operator--() { @@ -1042,7 +1042,7 @@ public: } self_t operator--(int) { auto res = *this; - --res; + --(*this); return res; } self_t& operator+=(difference_type diff) { @@ -1076,10 +1076,9 @@ public: // Construction static_assert(std::is_trivially_destructible_v); SmallVectorRef() {} SmallVectorRef(const SmallVectorRef& other) - : m_size(other.m_size), m_capacity(std::max(other.m_capacity, InlineMembers)), arr(other.arr), data(other.data) {} + : m_size(other.m_size), arr(other.arr), data(other.data) {} SmallVectorRef& operator=(const SmallVectorRef& other) { m_size = other.m_size; - m_capacity = other.m_capacity; arr = other.arr; data = other.data; return *this; @@ -1087,52 +1086,46 @@ public: // Construction template SmallVectorRef(Arena& arena, const SmallVectorRef& toCopy, - typename std::enable_if::value, int>::type = 0) - : m_size(toCopy.m_size), m_capacity(std::max(InlineMembers, toCopy.m_capacity)), - data(toCopy.m_size <= InlineMembers ? nullptr - : (T*)new (arena) uint8_t[sizeof(T) * (toCopy.m_size - InlineMembers)]) { + typename std::enable_if::value, int>::type = 0) + : m_size(toCopy.m_size) { + if (toCopy.size() > InlineMembers) { + data.resize(arena, toCopy.size() - InlineMembers); + } std::copy(toCopy.cbegin(), toCopy.cend(), begin()); } template SmallVectorRef(Arena& arena, const SmallVectorRef& toCopy, typename std::enable_if::value, int>::type = 0) - : m_size(toCopy.m_size), m_capacity(std::max(toCopy.m_capacity, InlineMembers)), - data(toCopy.m_size <= InlineMembers ? nullptr - : (T*)new (arena) uint8_t[sizeof(T) * (toCopy.m_size - InlineMembers)]) { - for (int i = 0; i < toCopy.m_size; ++i) { + : m_size(toCopy.m_size) { + for (int i = 0; i < toCopy.size(); ++i) { if (i < arr.size()) { new (&arr[i]) T(arena, toCopy[i]); } else { - new (&data[i - InlineMembers]) T(arena, toCopy[i]); + data.push_back_deep(arena, toCopy[i]); } } - std::copy(toCopy.cbegin(), toCopy.cend(), begin()); } template SmallVectorRef(Arena& arena, It first, It last) - : m_size(0), m_capacity(std::max(int(std::distance(first, last)), InlineMembers)), - data(m_capacity <= InlineMembers ? nullptr - : (T*)new (arena) uint8_t[sizeof(T) * (m_capacity - InlineMembers)]) { + : m_size(0) { while (first != last && m_size < InlineMembers) { new (&arr[m_size++]) T(*(first++)); } while (first != last) { - new (&arr[m_size++ - InlineMembers]) T(*(first++)); + data.push_back(arena, *(first++)); } } SmallVectorRef(SmallVectorRef&& o) - : m_size(o.m_size), m_capacity(o.m_capacity), arr(std::move(o.arr)), data(o.data) { + : m_size(o.m_size), arr(std::move(o.arr)), data(std::move(o.data)) { o.m_size = 0; - o.m_capacity = InlineMembers; - o.data = nullptr; } public: // information int size() const { return m_size; } - int capacity() const { return m_capacity; } + int capacity() const { return InlineMembers + data.capacity(); } bool empty() const { return m_size == 0; } public: // element access @@ -1150,15 +1143,12 @@ public: // element access public: // Modification void push_back(Arena& arena, T const& value) { - UNSTOPPABLE_ASSERT(m_capacity >= m_size && m_capacity >= InlineMembers); if (m_size < InlineMembers) { new (&arr[m_size++]) T(value); return; } - if (m_size == m_capacity) { - reallocate(arena, m_capacity + 1); - } - new (&data[m_size++ - InlineMembers]) T(value); + ++m_size; + data.push_back(arena, value); } void push_back_deep(Arena& arena, T const& value) { @@ -1166,13 +1156,11 @@ public: // Modification new (&arr[m_size++]) T(arena, value); return; } - if (m_size == m_capacity) { - reallocate(arena, m_capacity + 1); - } - new (&data[m_size++ - InlineMembers]) T(arena, value); + ++m_size; + data.push_back_deep(arena, value); } - void pop_back() { --m_size; } + void pop_back() {--m_size; } template void append(Arena& arena, It first, It last) { @@ -1180,14 +1168,14 @@ public: // Modification return; } auto d = std::distance(first, last); - if (m_size + d < m_capacity) { - reallocate(arena, m_capacity); + if (m_size + d > InlineMembers) { + data.reserve(arena, m_size + d - InlineMembers); } while (first != last && m_size < InlineMembers) { new (&(arr[m_size++])) T(*(first++)); } while (first != last) { - new (&data[m_size++ - InlineMembers]) T(*(first++)); + data.push_back(arena, *(first++)); } } @@ -1197,14 +1185,14 @@ public: // Modification return; } auto d = std::distance(first, last); - if (m_size + d < m_capacity) { - reallocate(arena, m_capacity); + if (m_size + d > InlineMembers) { + data.reserve(arena, m_size + d - InlineMembers); } while (first != last && m_size < InlineMembers) { new (&(arr[m_size++])) T(arena, *(first++)); } while (first != last) { - new (&data[m_size++ - InlineMembers]) T(arena, *(first++)); + data.push_back_deep(arena, *(first++)); } } @@ -1253,22 +1241,10 @@ public: // iterator access const_reverse_iterator rend() const { return crend(); } -private: // memory management - void reallocate(Arena& p, int requiredCapacity) { - requiredCapacity = std::max(m_capacity * 2, requiredCapacity); - // SOMEDAY: Maybe we are right at the end of the arena and can expand cheaply - T* newData = new (p) T[requiredCapacity - InlineMembers]; - if (m_size > InlineMembers) { - std::move(data, data + m_size - InlineMembers, newData); - } - data = newData; - m_capacity = requiredCapacity; - } - private: - int m_size = 0, m_capacity = InlineMembers; + int m_size = 0; std::array arr; - T* data = nullptr; + VectorRef data; }; template diff --git a/flow/IThreadPool.h b/flow/IThreadPool.h index 54eb518432..79249d7c57 100644 --- a/flow/IThreadPool.h +++ b/flow/IThreadPool.h @@ -112,7 +112,7 @@ Reference createGenericThreadPool(); class DummyThreadPool : public IThreadPool, ReferenceCounted { public: ~DummyThreadPool() {} - DummyThreadPool() : thread(NULL) {} + DummyThreadPool() : thread(nullptr) {} Future getError() { return errors.getFuture(); }