浏览代码

LibRegex: Replace the checkpoint backing store with a Vector

This makes repeated lookups and insertions much faster, yielding a
general performance improvement of about 10% in the regex test suite.
Ali Mohammad Pur 2 年之前
父节点
当前提交
4bff4219ff
共有 2 个文件被更改,包括 8 次插入4 次删除
  1. 7 3
      Userland/Libraries/LibRegex/RegexByteCode.cpp
  2. 1 1
      Userland/Libraries/LibRegex/RegexMatch.h

+ 7 - 3
Userland/Libraries/LibRegex/RegexByteCode.cpp

@@ -1064,16 +1064,20 @@ ALWAYS_INLINE ExecutionResult OpCode_ResetRepeat::execute(MatchInput const&, Mat
 
 ALWAYS_INLINE ExecutionResult OpCode_Checkpoint::execute(MatchInput const& input, MatchState& state) const
 {
-    input.checkpoints.set(id(), state.string_position);
+    auto id = this->id();
+    if (id >= input.checkpoints.size())
+        input.checkpoints.resize(id + 1);
+
+    input.checkpoints[id] = state.string_position + 1;
     return ExecutionResult::Continue;
 }
 
 ALWAYS_INLINE ExecutionResult OpCode_JumpNonEmpty::execute(MatchInput const& input, MatchState& state) const
 {
     u64 current_position = state.string_position;
-    auto checkpoint_position = input.checkpoints.find(checkpoint());
+    auto checkpoint_position = input.checkpoints[checkpoint()];
 
-    if (checkpoint_position != input.checkpoints.end() && checkpoint_position->value != current_position) {
+    if (checkpoint_position != 0 && checkpoint_position != current_position + 1) {
         auto form = this->form();
 
         if (form == OpCodeId::Jump) {

+ 1 - 1
Userland/Libraries/LibRegex/RegexMatch.h

@@ -647,7 +647,7 @@ struct MatchInput {
     mutable Vector<size_t> saved_positions;
     mutable Vector<size_t> saved_code_unit_positions;
     mutable Vector<size_t> saved_forks_since_last_save;
-    mutable HashMap<u64, u64> checkpoints;
+    mutable Vector<u64, 64> checkpoints;
     mutable Optional<size_t> fork_to_replace;
 };