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

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.
This commit is contained in:
Aaron Patterson 2020-11-06 10:11:20 -08:00 committed by Aaron Patterson
parent d14397bcc4
commit eb229994e5
Notes: git 2020-11-10 02:44:53 +09:00
4 changed files with 50 additions and 20 deletions

View file

@ -0,0 +1,14 @@
prelude: |
class C
def initialize
@a = nil
@b = nil
@c = nil
@d = nil
@e = nil
end
end
benchmark:
vm_ivar_init: |
C.new
loop_count: 30000000

View file

@ -52,6 +52,7 @@ VALUE rb_gvar_get(ID);
VALUE rb_gvar_set(ID, VALUE); VALUE rb_gvar_set(ID, VALUE);
VALUE rb_gvar_defined(ID); VALUE rb_gvar_defined(ID);
void rb_const_warn_if_deprecated(const rb_const_entry_t *, VALUE, ID); void rb_const_warn_if_deprecated(const rb_const_entry_t *, VALUE, ID);
void rb_init_iv_list(VALUE obj, uint32_t len, uint32_t newsize, st_table * index_tbl);
MJIT_SYMBOL_EXPORT_END MJIT_SYMBOL_EXPORT_END
static inline bool static inline bool

View file

@ -1402,6 +1402,28 @@ rb_obj_transient_heap_evacuate(VALUE obj, int promote)
} }
#endif #endif
void
rb_init_iv_list(VALUE obj, uint32_t len, uint32_t newsize, st_table * index_tbl)
{
VALUE *ptr = ROBJECT_IVPTR(obj);
VALUE *newptr;
if (RBASIC(obj)->flags & ROBJECT_EMBED) {
newptr = obj_ivar_heap_alloc(obj, newsize);
MEMCPY(newptr, ptr, VALUE, len);
RBASIC(obj)->flags &= ~ROBJECT_EMBED;
ROBJECT(obj)->as.heap.ivptr = newptr;
} else {
newptr = obj_ivar_heap_realloc(obj, len, newsize);
}
for (; len < newsize; len++) {
newptr[len] = Qundef;
}
ROBJECT(obj)->as.heap.numiv = newsize;
ROBJECT(obj)->as.heap.iv_index_tbl = index_tbl;
}
static VALUE static VALUE
obj_ivar_set(VALUE obj, ID id, VALUE val) obj_ivar_set(VALUE obj, ID id, VALUE val)
{ {
@ -1419,25 +1441,8 @@ obj_ivar_set(VALUE obj, ID id, VALUE val)
len = ROBJECT_NUMIV(obj); len = ROBJECT_NUMIV(obj);
if (len <= ivup.index) { if (len <= ivup.index) {
VALUE *ptr = ROBJECT_IVPTR(obj);
VALUE *newptr;
uint32_t newsize = iv_index_tbl_newsize(&ivup); uint32_t newsize = iv_index_tbl_newsize(&ivup);
rb_init_iv_list(obj, len, newsize, ivup.u.iv_index_tbl);
if (RBASIC(obj)->flags & ROBJECT_EMBED) {
newptr = obj_ivar_heap_alloc(obj, newsize);
MEMCPY(newptr, ptr, VALUE, len);
RBASIC(obj)->flags &= ~ROBJECT_EMBED;
ROBJECT(obj)->as.heap.ivptr = newptr;
}
else {
newptr = obj_ivar_heap_realloc(obj, len, newsize);
}
for (; len < newsize; len++) {
newptr[len] = Qundef;
}
ROBJECT(obj)->as.heap.numiv = newsize;
ROBJECT(obj)->as.heap.iv_index_tbl = ivup.u.iv_index_tbl;
} }
RB_OBJ_WRITE(obj, &ROBJECT_IVPTR(obj)[ivup.index], val); RB_OBJ_WRITE(obj, &ROBJECT_IVPTR(obj)[ivup.index], val);

View file

@ -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); VALUE *ptr = ROBJECT_IVPTR(obj);
index = !is_attr ? ic->entry->index : vm_cc_attr_index(cc)-1; 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_OBJ_WRITE(obj, &ptr[index], val);
RB_DEBUG_COUNTER_INC(ivar_set_ic_hit); RB_DEBUG_COUNTER_INC(ivar_set_ic_hit);
return val; /* inline cache 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 { else {
struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj); struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);