Regex: directly store instruction pointer in Thread struct

This commit is contained in:
Maxime Coste 2017-11-11 15:15:13 +08:00
parent add7f7e76b
commit c9b43d3634

View File

@ -158,7 +158,7 @@ public:
if (search) if (search)
to_next_start(start, m_end, m_program.start_chars.get()); to_next_start(start, m_end, m_program.start_chars.get());
return exec_program(start, Thread{search ? (uint16_t)0 : CompiledRegex::search_prefix_size, nullptr}); return exec_program(start, Thread{&m_program.instructions[search ? 0 : CompiledRegex::search_prefix_size], nullptr});
} }
ArrayView<const Iterator> captures() const ArrayView<const Iterator> captures() const
@ -216,7 +216,7 @@ private:
struct Thread struct Thread
{ {
uint32_t inst; const CompiledRegex::Instruction* inst;
Saves* saves; Saves* saves;
}; };
@ -237,9 +237,10 @@ private:
StepResult step(Utf8It& pos, Thread& thread, ExecState& state) StepResult step(Utf8It& pos, Thread& thread, ExecState& state)
{ {
const bool no_saves = (m_flags & RegexExecFlags::NoSaves); const bool no_saves = (m_flags & RegexExecFlags::NoSaves);
auto* instructions = m_program.instructions.data();
while (true) while (true)
{ {
auto& inst = m_program.instructions[thread.inst++]; auto& inst = *thread.inst++;
if (inst.last_step == state.step) if (inst.last_step == state.step)
return StepResult::Failed; return StepResult::Failed;
inst.last_step = state.step; inst.last_step = state.step;
@ -257,13 +258,13 @@ private:
case CompiledRegex::AnyChar: case CompiledRegex::AnyChar:
return StepResult::Consumed; return StepResult::Consumed;
case CompiledRegex::Jump: case CompiledRegex::Jump:
thread.inst = inst.param; thread.inst = instructions + inst.param;
break; break;
case CompiledRegex::Split_PrioritizeParent: case CompiledRegex::Split_PrioritizeParent:
{ {
if (thread.saves) if (thread.saves)
++thread.saves->refcount; ++thread.saves->refcount;
state.current_threads.push_back({inst.param, thread.saves}); state.current_threads.push_back({instructions + inst.param, thread.saves});
break; break;
} }
case CompiledRegex::Split_PrioritizeChild: case CompiledRegex::Split_PrioritizeChild:
@ -271,7 +272,7 @@ private:
if (thread.saves) if (thread.saves)
++thread.saves->refcount; ++thread.saves->refcount;
state.current_threads.push_back({thread.inst, thread.saves}); state.current_threads.push_back({thread.inst, thread.saves});
thread.inst = inst.param; thread.inst = instructions + inst.param;
break; break;
} }
case CompiledRegex::Save: case CompiledRegex::Save:
@ -397,12 +398,12 @@ private:
release_saves(thread.saves); release_saves(thread.saves);
break; break;
case StepResult::Consumed: case StepResult::Consumed:
if (m_program.instructions[thread.inst].scheduled) if (thread.inst->scheduled)
{ {
release_saves(thread.saves); release_saves(thread.saves);
continue; continue;
} }
m_program.instructions[thread.inst].scheduled = true; thread.inst->scheduled = true;
state.next_threads.push_back(thread); state.next_threads.push_back(thread);
break; break;
case StepResult::FindNextStart: case StepResult::FindNextStart:
@ -412,7 +413,7 @@ private:
} }
} }
for (auto& thread : state.next_threads) for (auto& thread : state.next_threads)
m_program.instructions[thread.inst].scheduled = false; thread.inst->scheduled = false;
if (pos == m_end or state.next_threads.empty() or if (pos == m_end or state.next_threads.empty() or
(found_match and (m_flags & RegexExecFlags::AnyMatch))) (found_match and (m_flags & RegexExecFlags::AnyMatch)))