Add a peephole optimization pass to the regex compiler
This commit is contained in:
parent
0364a99827
commit
77b1216ace
|
@ -684,6 +684,7 @@ struct RegexCompiler
|
||||||
{
|
{
|
||||||
m_program.forward_start_desc = compute_start_desc<RegexMode::Forward>();
|
m_program.forward_start_desc = compute_start_desc<RegexMode::Forward>();
|
||||||
compile_node<RegexMode::Forward>(0);
|
compile_node<RegexMode::Forward>(0);
|
||||||
|
peephole_optimize(0, m_program.instructions.size());
|
||||||
push_inst(CompiledRegex::Match);
|
push_inst(CompiledRegex::Match);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -692,6 +693,7 @@ struct RegexCompiler
|
||||||
m_program.first_backward_inst = m_program.instructions.size();
|
m_program.first_backward_inst = m_program.instructions.size();
|
||||||
m_program.backward_start_desc = compute_start_desc<RegexMode::Backward>();
|
m_program.backward_start_desc = compute_start_desc<RegexMode::Backward>();
|
||||||
compile_node<RegexMode::Backward>(0);
|
compile_node<RegexMode::Backward>(0);
|
||||||
|
peephole_optimize(m_program.first_backward_inst, m_program.instructions.size());
|
||||||
push_inst(CompiledRegex::Match);
|
push_inst(CompiledRegex::Match);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -1019,6 +1021,21 @@ private:
|
||||||
return std::make_unique<CompiledRegex::StartDesc>(start_desc);
|
return std::make_unique<CompiledRegex::StartDesc>(start_desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void peephole_optimize(size_t begin, size_t end)
|
||||||
|
{
|
||||||
|
if (not (m_flags & RegexCompileFlags::Optimize))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Move saves after all assertions on the same character
|
||||||
|
auto is_assertion = [](CompiledRegex::Op op) { return op >= CompiledRegex::LineStart; };
|
||||||
|
for (auto i = begin, j = begin + 1; j < end; ++i, ++j)
|
||||||
|
{
|
||||||
|
if (m_program.instructions[i].op == CompiledRegex::Save and
|
||||||
|
is_assertion(m_program.instructions[j].op))
|
||||||
|
std::swap(m_program.instructions[i], m_program.instructions[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const ParsedRegex::Node& get_node(ParsedRegex::NodeIndex index) const
|
const ParsedRegex::Node& get_node(ParsedRegex::NodeIndex index) const
|
||||||
{
|
{
|
||||||
return m_parsed_regex.nodes[index];
|
return m_parsed_regex.nodes[index];
|
||||||
|
|
|
@ -471,7 +471,8 @@ private:
|
||||||
release_saves(m_captures);
|
release_saves(m_captures);
|
||||||
m_captures = -1;
|
m_captures = -1;
|
||||||
m_threads.grow_ifn();
|
m_threads.grow_ifn();
|
||||||
const int16_t first_inst = &config.instructions[0] - &m_program.instructions[0];
|
|
||||||
|
const int16_t first_inst = forward ? 0 : m_program.first_backward_inst;
|
||||||
m_threads.push_current({first_inst, -1});
|
m_threads.push_current({first_inst, -1});
|
||||||
|
|
||||||
const auto& start_desc = forward ? m_program.forward_start_desc : m_program.backward_start_desc;
|
const auto& start_desc = forward ? m_program.forward_start_desc : m_program.backward_start_desc;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user