Permalink
Browse files

Optimize #to_s for String, Symbol, Integer

  • Loading branch information...
k0kubun committed May 13, 2017
1 parent b7ff46b commit a234de94745425dcf5fcabceef971d79ffad0584
Showing with 45 additions and 0 deletions.
  1. +1 −0 compile.c
  2. +19 −0 insns.def
  3. +1 −0 vm.c
  4. +1 −0 vm_core.h
  5. +23 −0 vm_insnhelper.c
View
@@ -2438,6 +2438,7 @@ iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
case idEmptyP: SP_INSN(empty_p);return COMPILE_OK;
case idSucc: SP_INSN(succ); return COMPILE_OK;
case idNot: SP_INSN(not); return COMPILE_OK;
case idTo_s: SP_INSN(tos); return COMPILE_OK;
}
break;
case 1:
View
@@ -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
View
1 vm.c
@@ -1577,6 +1577,7 @@ vm_init_redefined_flag(void)
OP(UMinus, UMINUS), (C(String));
OP(Max, MAX), (C(Array));
OP(Min, MIN), (C(Array));
OP(To_s, TOS), (C(String), C(Symbol), C(Integer));
#undef C
#undef OP
}
View
@@ -456,6 +456,7 @@ enum ruby_basic_operators {
BOP_UMINUS,
BOP_MAX,
BOP_MIN,
BOP_TOS,
BOP_LAST_
};
View
@@ -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
vm_opt_regexpmatch1(VALUE recv, VALUE obj)
{

0 comments on commit a234de9

Please sign in to comment.