From 5fbea87fe592da5de15df1a518c6595d844527c1 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 10 Sep 2021 07:35:44 +0900 Subject: codegen.c: resurrect `s->lastpc` to reduce `iseq` scans. When parsing scripts frequent scans could cost too much. --- mrbgems/mruby-compiler/core/codegen.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'mrbgems/mruby-compiler/core/codegen.c') diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index 693bdb4a0..752cb5494 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -55,6 +55,7 @@ typedef struct scope { uint16_t sp; uint32_t pc; + uint32_t lastpc; uint32_t lastlabel; int ainfo:15; mrb_bool mscope:1; @@ -226,12 +227,14 @@ gen_S(codegen_scope *s, uint16_t i) static void genop_0(codegen_scope *s, mrb_code i) { + s->lastpc = s->pc; gen_B(s, i); } static void genop_1(codegen_scope *s, mrb_code i, uint16_t a) { + s->lastpc = s->pc; if (a > 0xff) { gen_B(s, OP_EXT1); gen_B(s, i); @@ -246,6 +249,7 @@ genop_1(codegen_scope *s, mrb_code i, uint16_t a) static void genop_2(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b) { + s->lastpc = s->pc; if (a > 0xff && b > 0xff) { gen_B(s, OP_EXT3); gen_B(s, i); @@ -300,6 +304,7 @@ genop_W(codegen_scope *s, mrb_code i, uint32_t a) uint8_t a2 = (a>>8) & 0xff; uint8_t a3 = a & 0xff; + s->lastpc = s->pc; gen_B(s, i); gen_B(s, a1); gen_B(s, a2); @@ -468,13 +473,13 @@ rewind_pc(codegen_scope *s) static struct mrb_insn_data mrb_last_insn(codegen_scope *s) { - return mrb_decode_insn(mrb_prev_pc(s, pc_addr(s))); + return mrb_decode_insn(&s->iseq[s->lastpc]); } static mrb_bool no_peephole(codegen_scope *s) { - return no_optimize(s) || s->lastlabel == s->pc || s->pc == 0; + return no_optimize(s) || s->lastlabel == s->pc || s->pc == 0 || s->pc == s->lastpc; } #define JMPLINK_START UINT32_MAX @@ -500,7 +505,7 @@ genjmp(codegen_scope *s, mrb_code i, uint32_t pc) { uint32_t pos; - gen_B(s, i); + genop_0(s, i); pos = s->pc; gen_jmpdst(s, pc); return pos; @@ -553,11 +558,11 @@ genjmp2(codegen_scope *s, mrb_code i, uint16_t a, uint32_t pc, int val) if (a > 0xff) { gen_B(s, OP_EXT1); - gen_B(s, i); + genop_0(s, i); gen_S(s, a); } else { - gen_B(s, i); + genop_0(s, i); gen_B(s, (uint8_t)a); } pos = s->pc; -- cgit v1.2.3