diff options
author | Aaron Patterson <tenderlove@ruby-lang.org> | 2020-11-06 10:11:20 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2020-11-09 09:44:16 -0800 |
commit | eb229994e5b53e201e776ea103970751d3b1725b (patch) | |
tree | 32a14cbd95117defe7aaed2a14b3fe8b8f669f17 /variable.c | |
parent | d14397bcc48b73070953c9fbd410ba435e5f4b8c (diff) |
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.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3740
Diffstat (limited to 'variable.c')
-rw-r--r-- | variable.c | 41 |
1 files changed, 23 insertions, 18 deletions
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); |