Skip to content

Commit

Permalink
Optimize #to_s for String, Symbol, Integer
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed May 13, 2017
1 parent b7ff46b commit a234de9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions compile.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2438,6 +2438,7 @@ iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
case idEmptyP: SP_INSN(empty_p);return COMPILE_OK; case idEmptyP: SP_INSN(empty_p);return COMPILE_OK;
case idSucc: SP_INSN(succ); return COMPILE_OK; case idSucc: SP_INSN(succ); return COMPILE_OK;
case idNot: SP_INSN(not); return COMPILE_OK; case idNot: SP_INSN(not); return COMPILE_OK;
case idTo_s: SP_INSN(tos); return COMPILE_OK;
} }
break; break;
case 1: case 1:
Expand Down
19 changes: 19 additions & 0 deletions insns.def
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1531,6 +1531,25 @@ opt_not
} }
} }


/**
@c optimize
@e optimized to_s
@j 最適化された recv.to_s。
*/
DEFINE_INSN
opt_tos
(CALL_INFO ci, CALL_CACHE cc)
(VALUE recv)
(VALUE val)
{
val = vm_opt_tos(ci, cc, recv);

if (val == Qundef) {
/* other */
PUSH(recv);
CALL_SIMPLE_METHOD(recv);
}
}


/** /**
@c optimize @c optimize
Expand Down
1 change: 1 addition & 0 deletions vm.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1577,6 +1577,7 @@ vm_init_redefined_flag(void)
OP(UMinus, UMINUS), (C(String)); OP(UMinus, UMINUS), (C(String));
OP(Max, MAX), (C(Array)); OP(Max, MAX), (C(Array));
OP(Min, MIN), (C(Array)); OP(Min, MIN), (C(Array));
OP(To_s, TOS), (C(String), C(Symbol), C(Integer));
#undef C #undef C
#undef OP #undef OP
} }
Expand Down
1 change: 1 addition & 0 deletions vm_core.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ enum ruby_basic_operators {
BOP_UMINUS, BOP_UMINUS,
BOP_MAX, BOP_MAX,
BOP_MIN, BOP_MIN,
BOP_TOS,


BOP_LAST_ BOP_LAST_
}; };
Expand Down
23 changes: 23 additions & 0 deletions vm_insnhelper.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3661,6 +3661,29 @@ vm_opt_not(CALL_INFO ci, CALL_CACHE cc, VALUE recv)
} }
} }


static VALUE
vm_opt_tos(CALL_INFO ci, CALL_CACHE cc, VALUE recv)
{
if (SPECIAL_CONST_P(recv)) {
return Qundef;
}
else if (RBASIC_CLASS(recv) == rb_cString &&
BASIC_OP_UNREDEFINED_P(BOP_TOS, STRING_REDEFINED_OP_FLAG)) {
return recv;
}
else if (RBASIC_CLASS(recv) == rb_cSymbol &&
BASIC_OP_UNREDEFINED_P(BOP_TOS, SYMBOL_REDEFINED_OP_FLAG)) {
return rb_sym_to_s(recv);
}
else if (RBASIC_CLASS(recv) == rb_cInteger &&
BASIC_OP_UNREDEFINED_P(BOP_TOS, INTEGER_REDEFINED_OP_FLAG)) {
return rb_int2str(recv, 10);
}
else {
return Qundef;
}
}

static VALUE static VALUE
vm_opt_regexpmatch1(VALUE recv, VALUE obj) vm_opt_regexpmatch1(VALUE recv, VALUE obj)
{ {
Expand Down

0 comments on commit a234de9

Please sign in to comment.