1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:48:11 +00:00

LibRegex: Make codegen+optimisation for alternatives much faster

Just a little thinking outside the box, and we can now parse and
optimise a million copies of "a|" chained together in just a second :^)
This commit is contained in:
Ali Mohammad Pur 2022-02-20 03:25:21 +03:30 committed by Andreas Kling
parent 4be7239626
commit 97a333608e
5 changed files with 150 additions and 65 deletions

View file

@ -958,7 +958,7 @@ bool ECMA262Parser::parse_disjunction(ByteCode& stack, size_t& match_length_mini
{
size_t total_match_length_minimum = NumericLimits<size_t>::max();
Vector<ByteCode> alternatives;
do {
while (true) {
ByteCode alternative_stack;
size_t alternative_minimum_length = 0;
auto alt_ok = parse_alternative(alternative_stack, alternative_minimum_length, unicode, named);
@ -971,20 +971,9 @@ bool ECMA262Parser::parse_disjunction(ByteCode& stack, size_t& match_length_mini
if (!match(TokenType::Pipe))
break;
consume();
} while (true);
Optional<ByteCode> alternative_stack {};
for (auto& alternative : alternatives) {
if (alternative_stack.has_value()) {
ByteCode target_stack;
target_stack.insert_bytecode_alternation(alternative_stack.release_value(), move(alternative));
alternative_stack = move(target_stack);
} else {
alternative_stack = move(alternative);
}
}
stack.extend(alternative_stack.release_value());
Optimizer::append_alternation(stack, alternatives.span());
match_length_minimum = total_match_length_minimum;
return true;
}