From 09711e06320b5303f6182e9cb3566a363a1cd6e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 27 Apr 2021 15:36:47 +0200 Subject: [PATCH] Optimize build_jumpdest_map() Optimize the build_jumpdest_map() procedure by comparing opcode as signed integer and eliminating always true op <= OP_PUSH32 (0x7f) check. --- lib/evmone/baseline.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/evmone/baseline.cpp b/lib/evmone/baseline.cpp index fbab49dd02..06ab4252e3 100644 --- a/lib/evmone/baseline.cpp +++ b/lib/evmone/baseline.cpp @@ -12,17 +12,16 @@ namespace evmone { JumpdestMap build_jumpdest_map(const uint8_t* code, size_t code_size) { - // Bitmask for PUSH opcode identification. - // It clears the lower 5 bits of a byte. - // The opcode is PUSH iff remaining byte value is 0x60 (OP_PUSH1). - constexpr auto push_op_mask = 0xE0; - JumpdestMap map(code_size); // Allocate and init bitmap with zeros. for (size_t i = 0; i < code_size; ++i) { const auto op = code[i]; - if ((op & push_op_mask) == OP_PUSH1) // If any PUSH opcode. - i += op - size_t{OP_PUSH1 - 1}; // Skip PUSH data. + + // To find if op is any PUSH opcode (OP_PUSH1 <= op <= OP_PUSH32) + // it can be noticed that OP_PUSH32 is INT8_MAX (0x7f) therefore + // static_cast(op) <= OP_PUSH32 is always true and can be skipped. + if (static_cast(op) >= OP_PUSH1) // If any PUSH opcode . + i += op - size_t{OP_PUSH1 - 1}; // Skip PUSH data. else if (INTX_UNLIKELY(op == OP_JUMPDEST)) map[i] = true; }