1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* insns.def (setinstancevariable), vm_insnhelper.c (vm_setivar):

fix to use inline cache (trivial optimization).



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24768 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2009-09-06 07:40:24 +00:00
parent 33ff311494
commit bd8da69e9b
3 changed files with 55 additions and 4 deletions

View file

@ -1,3 +1,8 @@
Sun Sep 6 16:13:06 2009 Koichi Sasada <ko1@atdot.net>
* insns.def (setinstancevariable), vm_insnhelper.c (vm_setivar):
fix to use inline cache (trivial optimization).
Sun Sep 6 10:34:19 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> Sun Sep 6 10:34:19 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* io.c: fixed rdoc, a patch from Nobuhiro IMAI at [ruby-core:25433]. * io.c: fixed rdoc, a patch from Nobuhiro IMAI at [ruby-core:25433].

View file

@ -165,11 +165,11 @@ getinstancevariable
*/ */
DEFINE_INSN DEFINE_INSN
setinstancevariable setinstancevariable
(ID id) (ID id, IC ic)
(VALUE val) (VALUE val)
() ()
{ {
rb_ivar_set(GET_SELF(), id, val); vm_setivar(GET_SELF(), id, val, ic);
} }
/** /**

View file

@ -1182,10 +1182,15 @@ vm_get_cvar_base(NODE *cref)
return klass; return klass;
} }
#ifndef ENABLE_IC_FOR_IVAR
#define ENABLE_IC_FOR_IVAR 1
#endif
static VALUE static VALUE
vm_getivar(VALUE obj, ID id, IC ic) vm_getivar(VALUE obj, ID id, IC ic)
{ {
#if 1 #if ENABLE_IC_FOR_IVAR
if (TYPE(obj) == T_OBJECT) { if (TYPE(obj) == T_OBJECT) {
VALUE val = Qundef; VALUE val = Qundef;
VALUE klass = RBASIC(obj)->klass; VALUE klass = RBASIC(obj)->klass;
@ -1210,7 +1215,7 @@ vm_getivar(VALUE obj, ID id, IC ic)
if (index < len) { if (index < len) {
val = ptr[index]; val = ptr[index];
} }
ic->ic_class = RBASIC(obj)->klass; ic->ic_class = klass;
ic->ic_index = index; ic->ic_index = index;
} }
} }
@ -1229,6 +1234,47 @@ vm_getivar(VALUE obj, ID id, IC ic)
#endif #endif
} }
static void
vm_setivar(VALUE obj, ID id, VALUE val, IC ic)
{
#if ENABLE_IC_FOR_IVAR
if (!OBJ_UNTRUSTED(obj) && rb_safe_level() >= 4) {
rb_raise(rb_eSecurityError, "Insecure: can't modify instance variable");
}
if (OBJ_FROZEN(obj)) {
rb_error_frozen("object");
}
if (TYPE(obj) == T_OBJECT) {
VALUE klass = RBASIC(obj)->klass;
st_data_t index;
if (ic->ic_class == klass) {
long index = ic->ic_index;
long len = ROBJECT_NUMIV(obj);
VALUE *ptr = ROBJECT_IVPTR(obj);
if (index < len) {
ptr[index] = val;
return; /* inline cache hit */
}
}
else {
struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
if (iv_index_tbl && st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
ic->ic_class = klass;
ic->ic_index = index;
}
/* fall through */
}
}
rb_ivar_set(obj, id, val);
#else
rb_ivar_set(obj, id, val);
#endif
}
static inline const rb_method_entry_t * static inline const rb_method_entry_t *
vm_method_search(VALUE id, VALUE klass, IC ic) vm_method_search(VALUE id, VALUE klass, IC ic)
{ {