From 88d148b46a2ff1093a3ece5db835f2c51fc03bd7 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Mon, 6 Sep 2021 16:53:06 +0430 Subject: [PATCH] LibRegex: Avoid keeping track of checkpoints across forks Doing so would increase memory consumption by quite a bit, since many useless copies of the checkpoints hashmap would be created and later thrown away. --- Userland/Libraries/LibRegex/RegexByteCode.cpp | 8 ++++---- Userland/Libraries/LibRegex/RegexMatch.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibRegex/RegexByteCode.cpp b/Userland/Libraries/LibRegex/RegexByteCode.cpp index 5d2b1eaa94d..ded6ff79ea5 100644 --- a/Userland/Libraries/LibRegex/RegexByteCode.cpp +++ b/Userland/Libraries/LibRegex/RegexByteCode.cpp @@ -871,17 +871,17 @@ ALWAYS_INLINE ExecutionResult OpCode_ResetRepeat::execute(MatchInput const&, Mat return ExecutionResult::Continue; } -ALWAYS_INLINE ExecutionResult OpCode_Checkpoint::execute(MatchInput const&, MatchState& state) const +ALWAYS_INLINE ExecutionResult OpCode_Checkpoint::execute(MatchInput const& input, MatchState& state) const { - state.checkpoints.set(state.instruction_position, state.string_position); + input.checkpoints.set(state.instruction_position, state.string_position); return ExecutionResult::Continue; } -ALWAYS_INLINE ExecutionResult OpCode_JumpNonEmpty::execute(MatchInput const&, MatchState& state) const +ALWAYS_INLINE ExecutionResult OpCode_JumpNonEmpty::execute(MatchInput const& input, MatchState& state) const { auto current_position = state.string_position; auto checkpoint_ip = state.instruction_position + size() + checkpoint(); - if (state.checkpoints.get(checkpoint_ip).value_or(current_position) != current_position) { + if (input.checkpoints.get(checkpoint_ip).value_or(current_position) != current_position) { auto form = this->form(); if (form == OpCodeId::Jump) { diff --git a/Userland/Libraries/LibRegex/RegexMatch.h b/Userland/Libraries/LibRegex/RegexMatch.h index 035a988234d..efd5bd0e4ae 100644 --- a/Userland/Libraries/LibRegex/RegexMatch.h +++ b/Userland/Libraries/LibRegex/RegexMatch.h @@ -513,6 +513,7 @@ struct MatchInput { mutable size_t fail_counter { 0 }; mutable Vector saved_positions; mutable Vector saved_code_unit_positions; + mutable HashMap checkpoints; }; struct MatchState { @@ -524,7 +525,6 @@ struct MatchState { Vector matches; Vector> capture_group_matches; Vector repetition_marks; - HashMap checkpoints; }; }