Skip to content

Commit

Permalink
avoid memory allocation if it is not required
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbachmann committed Jul 29, 2022
1 parent 2278c3d commit 3db25cc
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions rapidfuzz/details/PatternMatchVector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ struct BlockPatternMatchVector {
BlockPatternMatchVector() = delete;

BlockPatternMatchVector(size_t str_len)
: m_block_count(ceil_div(str_len, 64)), m_extendedAscii(256, m_block_count, 0)
: m_block_count(ceil_div(str_len, 64)), m_map(nullptr), m_extendedAscii(256, m_block_count, 0)
{
m_map = new BitvectorHashmap[m_block_count];
}

template <typename InputIt>
Expand Down Expand Up @@ -194,7 +193,11 @@ struct BlockPatternMatchVector {
if (key >= 0 && key <= 255)
m_extendedAscii[static_cast<uint8_t>(key)][block] |= mask;
else
{
if (!m_map)
m_map = new BitvectorHashmap[m_block_count];
m_map[block].insert_mask(key, mask);
}
}

void insert_mask(size_t block, char key, uint64_t mask) noexcept
Expand All @@ -207,8 +210,10 @@ struct BlockPatternMatchVector {
{
if (key >= 0 && key <= 255)
return m_extendedAscii[static_cast<uint8_t>(key)][block];
else
else if(m_map)
return m_map[block].get(key);
else
return 0;
}

uint64_t get(size_t block, char ch) const noexcept
Expand Down

0 comments on commit 3db25cc

Please sign in to comment.