From 66249612d6721dae7a9b8de767d86bfed36dd9f8 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Tue, 21 Dec 2021 18:07:04 +0330 Subject: [PATCH] LibRegex: Avoid calling DisjointChunks::size() in get_opcode() That method is O(n), and should generally be avoided. --- Userland/Libraries/LibRegex/RegexByteCode.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibRegex/RegexByteCode.cpp b/Userland/Libraries/LibRegex/RegexByteCode.cpp index 07df3077d3..9a0c9f3818 100644 --- a/Userland/Libraries/LibRegex/RegexByteCode.cpp +++ b/Userland/Libraries/LibRegex/RegexByteCode.cpp @@ -186,10 +186,10 @@ ALWAYS_INLINE OpCode& ByteCode::get_opcode_by_id(OpCodeId id) const OpCode& ByteCode::get_opcode(MatchState& state) const { OpCodeId opcode_id; - if (state.instruction_position >= size()) - opcode_id = OpCodeId::Exit; + if (auto opcode_ptr = static_cast const&>(*this).find(state.instruction_position)) + opcode_id = (OpCodeId)*opcode_ptr; else - opcode_id = (OpCodeId)at(state.instruction_position); + opcode_id = OpCodeId::Exit; auto& opcode = get_opcode_by_id(opcode_id); opcode.set_state(state);