summaryrefslogtreecommitdiffstats
path: root/vm_insnhelper.c
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-09-06 07:40:24 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-09-06 07:40:24 +0000
commit228994868ff2f9584ea499f06cffa53cb3c2723a (patch)
tree466707aea201a1aa3c26e697156932bd38f325d4 /vm_insnhelper.c
parent2ed4f52d1afe641c5d0b7d3ddd153bedbc178425 (diff)
downloadruby-228994868ff2f9584ea499f06cffa53cb3c2723a.tar.gz
ruby-228994868ff2f9584ea499f06cffa53cb3c2723a.tar.xz
ruby-228994868ff2f9584ea499f06cffa53cb3c2723a.zip
* insns.def (setinstancevariable), vm_insnhelper.c (vm_setivar):
fix to use inline cache (trivial optimization). git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@24768 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r--vm_insnhelper.c50
1 files changed, 48 insertions, 2 deletions
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 117bf9579..8cec18d40 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -1182,10 +1182,15 @@ vm_get_cvar_base(NODE *cref)
return klass;
}
+
+#ifndef ENABLE_IC_FOR_IVAR
+#define ENABLE_IC_FOR_IVAR 1
+#endif
+
static VALUE
vm_getivar(VALUE obj, ID id, IC ic)
{
-#if 1
+#if ENABLE_IC_FOR_IVAR
if (TYPE(obj) == T_OBJECT) {
VALUE val = Qundef;
VALUE klass = RBASIC(obj)->klass;
@@ -1210,7 +1215,7 @@ vm_getivar(VALUE obj, ID id, IC ic)
if (index < len) {
val = ptr[index];
}
- ic->ic_class = RBASIC(obj)->klass;
+ ic->ic_class = klass;
ic->ic_index = index;
}
}
@@ -1229,6 +1234,47 @@ vm_getivar(VALUE obj, ID id, IC ic)
#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 *
vm_method_search(VALUE id, VALUE klass, IC ic)
{