From 430bee2755c147679e1f6766f9c45a178d40bdab Mon Sep 17 00:00:00 2001 From: Bowen Han Date: Sat, 7 Jun 2025 08:17:29 +0200 Subject: [PATCH 1/2] Optimize push_back by avoiding BitReference --- .gitignore | 1 + bitvector.hpp | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/bitvector.hpp b/bitvector.hpp index 80b5d35..12d3cbe 100644 --- a/bitvector.hpp +++ b/bitvector.hpp @@ -358,7 +358,15 @@ namespace bowen { reserve(m_capacity ? m_capacity * WORD_BITS * 2 : WORD_BITS); } - (*this)[m_size++] = value; + + size_t word_index = m_size >> WORD_SHIFT; + BitType mask = static_cast(1) + << (m_size & (WORD_BITS - 1)); + if (value) + m_data[word_index] |= mask; + else + m_data[word_index] &= ~mask; + ++m_size; } void reserve(size_t new_capacity) From f742d8bf8f946aeb98bd0f4992457e8716b7847f Mon Sep 17 00:00:00 2001 From: Bowen Han Date: Sat, 7 Jun 2025 08:26:11 +0200 Subject: [PATCH 2/2] Remove stray whitespace --- bitvector.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bitvector.hpp b/bitvector.hpp index 12d3cbe..1438019 100644 --- a/bitvector.hpp +++ b/bitvector.hpp @@ -29,7 +29,7 @@ namespace bowen if (n > std::numeric_limits::max() / sizeof(T)) { throw std::bad_alloc(); } - + void *ptr = _mm_malloc(n * sizeof(T), ALIGN_SIZE); if (!ptr) { throw std::bad_alloc(); @@ -374,7 +374,7 @@ namespace bowen if (new_capacity > m_capacity * WORD_BITS) { size_t new_word_count = num_words(new_capacity); - + BitType *new_data = m_allocator.allocate(new_word_count); std::copy(m_data, m_data + m_capacity, new_data); deallocate_memory();