Skip to content

Commit d4a3f9b

Browse files
authored
Merge pull request #19 from bugparty/codex/analyze-bm_bowen_pushback-vs-bm_bowen_qsetbittrue6v2-perform
Optimize push_back performance
2 parents ca2f38c + f742d8b commit d4a3f9b

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

bitvector.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace bowen
2929
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T)) {
3030
throw std::bad_alloc();
3131
}
32-
32+
3333
void *ptr = _mm_malloc(n * sizeof(T), ALIGN_SIZE);
3434
if (!ptr) {
3535
throw std::bad_alloc();
@@ -358,15 +358,23 @@ namespace bowen
358358
{
359359
reserve(m_capacity ? m_capacity * WORD_BITS * 2 : WORD_BITS);
360360
}
361-
(*this)[m_size++] = value;
361+
362+
size_t word_index = m_size >> WORD_SHIFT;
363+
BitType mask = static_cast<BitType>(1)
364+
<< (m_size & (WORD_BITS - 1));
365+
if (value)
366+
m_data[word_index] |= mask;
367+
else
368+
m_data[word_index] &= ~mask;
369+
++m_size;
362370
}
363371

364372
void reserve(size_t new_capacity)
365373
{
366374
if (new_capacity > m_capacity * WORD_BITS)
367375
{
368376
size_t new_word_count = num_words(new_capacity);
369-
377+
370378
BitType *new_data = m_allocator.allocate(new_word_count);
371379
std::copy(m_data, m_data + m_capacity, new_data);
372380
deallocate_memory();

0 commit comments

Comments
 (0)