From 24d8b056c7482425a403b3a84f4f2aeaff898d43 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 6 May 2024 09:33:53 +0200 Subject: [PATCH] LibJS/Bytecode: Elide jumps that land in the very next block Jumping to the next block is effectively a no-op, so let's save time and space by just not emitting those jumps. --- Userland/Libraries/LibJS/Bytecode/Generator.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.cpp b/Userland/Libraries/LibJS/Bytecode/Generator.cpp index 41b84e99286..34d9f79205e 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Generator.cpp @@ -118,6 +118,20 @@ CodeGenerationErrorOr> Generator::generate(VM& vm, ASTN Bytecode::InstructionStreamIterator it(block->instruction_stream()); while (!it.at_end()) { auto& instruction = const_cast(*it); + + // OPTIMIZATION: Don't emit jumps that just jump to the next block. + if (instruction.type() == Instruction::Type::Jump) { + auto& jump = static_cast(instruction); + if (jump.target().basic_block_index() == block->index() + 1) { + if (basic_block_start_offsets.last() == bytecode.size()) { + // This block is empty, just skip it. + basic_block_start_offsets.take_last(); + } + ++it; + continue; + } + } + instruction.visit_labels([&](Label& label) { size_t label_offset = bytecode.size() + (bit_cast(&label) - bit_cast(&instruction)); label_offsets.append(label_offset);