aboutsummaryrefslogtreecommitdiffstats
path: root/vm_insnhelper.c
diff options
context:
space:
mode:
authorAaron Patterson <tenderlove@ruby-lang.org>2020-11-06 10:11:20 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2020-11-09 09:44:16 -0800
commiteb229994e5b53e201e776ea103970751d3b1725b (patch)
tree32a14cbd95117defe7aaed2a14b3fe8b8f669f17 /vm_insnhelper.c
parentd14397bcc48b73070953c9fbd410ba435e5f4b8c (diff)
downloadruby-eb229994e5b53e201e776ea103970751d3b1725b.tar.gz
eagerly initialize ivar table when index is small enough
When the inline cache is written, the iv table will contain an entry for the instance variable. If we get an inline cache hit, then we know the iv table must contain a value for the index written to the inline cache. If the index in the inline cache is larger than the list on the object, but *smaller* than the iv index table on the class, then we can just eagerly allocate the iv list to be the same size as the iv index table. This avoids duplicate work of checking frozen as well as looking up the index for the particular instance variable name.
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r--vm_insnhelper.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 51b3c40010..f8b2f707ca 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -1213,11 +1213,21 @@ vm_setivar(VALUE obj, ID id, VALUE val, const rb_iseq_t *iseq, IVC ic, const str
VALUE *ptr = ROBJECT_IVPTR(obj);
index = !is_attr ? ic->entry->index : vm_cc_attr_index(cc)-1;
- if (RB_DEBUG_COUNTER_INC_UNLESS(ivar_set_ic_miss_oorange, index < ROBJECT_NUMIV(obj))) {
+ if (index < ROBJECT_NUMIV(obj)) {
RB_OBJ_WRITE(obj, &ptr[index], val);
RB_DEBUG_COUNTER_INC(ivar_set_ic_hit);
return val; /* inline cache hit */
- }
+ } else {
+ st_table * iv_idx_tbl = RCLASS_IV_INDEX_TBL(rb_class_real(klass));
+ if (index < iv_idx_tbl->num_entries) {
+ rb_init_iv_list(obj, ROBJECT_NUMIV(obj), iv_idx_tbl->num_entries, iv_idx_tbl);
+ ptr = ROBJECT_IVPTR(obj);
+ RB_OBJ_WRITE(obj, &ptr[index], val);
+ RB_DEBUG_COUNTER_INC(ivar_set_ic_hit);
+ return val; /* inline cache hit */
+ }
+ }
+ RB_DEBUG_COUNTER_INC(ivar_set_ic_miss_oorange);
}
else {
struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);