From eb229994e5b53e201e776ea103970751d3b1725b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 6 Nov 2020 10:11:20 -0800 Subject: 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. --- variable.c | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'variable.c') diff --git a/variable.c b/variable.c index 7ebbcf85b8..669d47e8d3 100644 --- a/variable.c +++ b/variable.c @@ -1402,6 +1402,28 @@ rb_obj_transient_heap_evacuate(VALUE obj, int promote) } #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 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); if (len <= ivup.index) { - VALUE *ptr = ROBJECT_IVPTR(obj); - VALUE *newptr; uint32_t newsize = iv_index_tbl_newsize(&ivup); - - 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_init_iv_list(obj, len, newsize, ivup.u.iv_index_tbl); } RB_OBJ_WRITE(obj, &ROBJECT_IVPTR(obj)[ivup.index], val); -- cgit v1.2.3