Skip to content

Commit

Permalink
avoid extra dup and pop in compile_op_asgn2
Browse files Browse the repository at this point in the history
Co-authored-by: John Hawthorn <jhawthorn@github.com>
  • Loading branch information
HParker and jhawthorn committed Sep 22, 2022
1 parent aafbc90 commit fbaac83
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
8 changes: 8 additions & 0 deletions benchmark/vm_lvar_cond_set.yml
@@ -0,0 +1,8 @@
benchmark:
vm_lvar_cond_set: |
a ||= 1
b ||= 1
c ||= 1
d ||= 1
nil
loop_count: 30000000
35 changes: 24 additions & 11 deletions compile.c
Expand Up @@ -8663,6 +8663,17 @@ compile_op_asgn2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node
lfin: # o ?
pop # o
# or (popped)
if lcfin # r
eval v # r v
send a= # ?
jump lfin # ?
lcfin: # r
lfin: # ?
pop #
# and
dup # r o o
unless lcfin
Expand Down Expand Up @@ -8691,32 +8702,36 @@ compile_op_asgn2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node
ADD_SEND_WITH_FLAG(ret, node, vid, INT2FIX(0), INT2FIX(asgnflag));

if (atype == idOROP || atype == idANDOP) {
ADD_INSN(ret, node, dup);
if (!popped) {
ADD_INSN(ret, node, dup);
}
if (atype == idOROP) {
ADD_INSNL(ret, node, branchif, lcfin);
}
else { /* idANDOP */
ADD_INSNL(ret, node, branchunless, lcfin);
}
ADD_INSN(ret, node, pop);
if (!popped) {
ADD_INSN(ret, node, pop);
}
CHECK(COMPILE(ret, "NODE_OP_ASGN2 val", node->nd_value));
ADD_INSN(ret, node, swap);
ADD_INSN1(ret, node, topn, INT2FIX(1));
if (!popped) {
ADD_INSN(ret, node, swap);
ADD_INSN1(ret, node, topn, INT2FIX(1));
}
ADD_SEND_WITH_FLAG(ret, node, aid, INT2FIX(1), INT2FIX(asgnflag));
ADD_INSNL(ret, node, jump, lfin);

ADD_LABEL(ret, lcfin);
ADD_INSN(ret, node, swap);
if (!popped) {
ADD_INSN(ret, node, swap);
}

ADD_LABEL(ret, lfin);
ADD_INSN(ret, node, pop);
if (lskip) {
ADD_LABEL(ret, lskip);
}
if (popped) {
/* we can apply more optimize */
ADD_INSN(ret, node, pop);
}
}
else {
CHECK(COMPILE(ret, "NODE_OP_ASGN2 val", node->nd_value));
Expand Down Expand Up @@ -8848,9 +8863,7 @@ compile_op_log(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node,
}

ADD_LABEL(ret, lassign);

CHECK(COMPILE_(ret, "NODE_OP_ASGN_AND/OR#nd_value", node->nd_value, popped));

ADD_LABEL(ret, lfin);
return COMPILE_OK;
}
Expand Down

0 comments on commit fbaac83

Please sign in to comment.