1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:17:44 +00:00

LibRegex+LibJS: Combine named and unnamed capture groups in MatchState

Combining these into one list helps reduce the size of MatchState, and
as a result, reduces the amount of memory consumed during execution of
very large regex matches.

Doing this also allows us to remove a few regex byte code instructions:
ClearNamedCaptureGroup, SaveLeftNamedCaptureGroup, and NamedReference.
Named groups now behave the same as unnamed groups for these operations.
Note that SaveRightNamedCaptureGroup still exists to cache the matched
group name.

This also removes the recursion level from the MatchState, as it can
exist as a local variable in Matcher::execute instead.
This commit is contained in:
Timothy Flynn 2021-08-14 16:28:54 -04:00 committed by Linus Groh
parent fea181bde3
commit f1ce998d73
9 changed files with 69 additions and 202 deletions

View file

@ -442,11 +442,20 @@ public:
}
Match(String const string_, size_t const line_, size_t const column_, size_t const global_offset_)
: string(string_)
: string(move(string_))
, view(string.value().view())
, line(line_)
, column(column_)
, global_offset(global_offset_)
{
}
Match(RegexStringView const view_, StringView capture_group_name_, size_t const line_, size_t const column_, size_t const global_offset_)
: view(view_)
, capture_group_name(capture_group_name_)
, line(line_)
, column(column_)
, global_offset(global_offset_)
, left_column(column_)
{
}
@ -454,6 +463,7 @@ public:
void reset()
{
view = view.typed_null_view();
capture_group_name.clear();
line = 0;
column = 0;
global_offset = 0;
@ -461,6 +471,7 @@ public:
}
RegexStringView view { nullptr };
Optional<StringView> capture_group_name {};
size_t line { 0 };
size_t column { 0 };
size_t global_offset { 0 };
@ -494,8 +505,6 @@ struct MatchState {
size_t fork_at_position { 0 };
Vector<Match> matches;
Vector<Vector<Match>> capture_group_matches;
Vector<HashMap<String, Match>> named_capture_group_matches;
size_t recursion_level { 0 };
};
struct MatchOutput {