Skip to content

Commit

Permalink
Avoid clearing iterator buffer on saves allocation
Browse files Browse the repository at this point in the history
When creating a new save, we had to clear all iterators to have valid
values. This operation is relatively costly because it gets optimized
to a memset whose call overhead is pretty high (as we usually have
less than 32 bytes to clear). Bypass this by storing a bitmap of
valid iterators.
  • Loading branch information
mawww committed Mar 13, 2024
1 parent c4df0fa commit 83f12fc
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/regex_impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,31 @@ public:
ArrayView<const Iterator> captures() const
{
if (m_captures >= 0)
return { m_saves[m_captures].pos, m_program.save_count };
{
auto& saves = m_saves[m_captures];
for (int i = 0; i < m_program.save_count; ++i)
{
if ((saves.valid_mask & (1 << i)) == 0)
saves.pos[i] = Iterator{};
}
return { saves.pos, m_program.save_count };
}
return {};
}

private:
struct Saves
{
int32_t refcount;
int32_t next_free;
union {
int32_t next_free;
uint32_t valid_mask;
};
Iterator* pos;
};

template<bool copy>
int16_t new_saves(Iterator* pos)
int16_t new_saves(Iterator* pos, uint32_t valid_mask)
{
kak_assert(not copy or pos != nullptr);
const auto count = m_program.save_count;
Expand All @@ -310,18 +321,16 @@ private:
Saves& saves = m_saves[res];
m_first_free = saves.next_free;
kak_assert(saves.refcount == 1);
if (copy)
std::copy_n(pos, count, saves.pos);
else
std::fill_n(saves.pos, count, Iterator{});

if constexpr (copy)
std::copy_n(pos, std::bit_width(valid_mask), saves.pos);
saves.valid_mask = valid_mask;
return res;
}

auto* new_pos = reinterpret_cast<Iterator*>(operator new (count * sizeof(Iterator)));
for (size_t i = 0; i < count; ++i)
new (new_pos+i) Iterator{copy ? pos[i] : Iterator{}};
m_saves.push_back({1, 0, new_pos});
m_saves.push_back({1, {.valid_mask=valid_mask}, new_pos});
return static_cast<int16_t>(m_saves.size() - 1);
}

Expand Down Expand Up @@ -418,16 +427,17 @@ private:
}
break;
case CompiledRegex::Save:
if (mode & RegexMode::NoSaves)
if constexpr (mode & RegexMode::NoSaves)
break;
if (thread.saves < 0)
thread.saves = new_saves<false>(nullptr);
else if (m_saves[thread.saves].refcount > 1)
thread.saves = new_saves<false>(nullptr, 0);
else if (auto& saves = m_saves[thread.saves]; saves.refcount > 1)
{
--m_saves[thread.saves].refcount;
thread.saves = new_saves<true>(m_saves[thread.saves].pos);
--saves.refcount;
thread.saves = new_saves<true>(saves.pos, saves.valid_mask);
}
m_saves[thread.saves].pos[inst.param.save_index] = pos;
m_saves[thread.saves].valid_mask |= (1 << inst.param.save_index);
break;
case CompiledRegex::CharClass:
if (pos == config.end)
Expand Down

0 comments on commit 83f12fc

Please sign in to comment.