Skip to content

Commit

Permalink
Initialize ivars to nil
Browse files Browse the repository at this point in the history
This branch initializes all ivar's to `nil`. By doing this we create
less new shapes becasue the ivar order is already set. This branch is
experimental. One caveat is this will break code that checks if an
instance variable is defined because it will always be
"instance-variable" after this change.
  • Loading branch information
eileencodes committed Aug 15, 2023
1 parent efd611c commit 8e3fc8e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2873,6 +2873,39 @@ rb_obj_embedded_size(uint32_t numiv)
return offsetof(struct RObject, as.ary) + (sizeof(VALUE) * numiv);
}

static enum rb_id_table_iterator_result
method_entry_i(ID key, VALUE value, void *data)
{
const rb_method_entry_t *me = (const rb_method_entry_t *)value;
int type = me->def->type;

if (type == VM_METHOD_TYPE_IVAR || type == VM_METHOD_TYPE_ATTRSET) {
ID mid = me->def->body.attr.id;
VALUE instance = (VALUE) data;
rb_ivar_set(instance, mid, Qnil);
}

return ID_TABLE_CONTINUE;
}

static void
add_instance_method_list(VALUE mod, VALUE instance)
{
struct rb_id_table *m_tbl = RCLASS_M_TBL(mod);
if (!m_tbl) return;
rb_id_table_foreach(m_tbl, method_entry_i, (void *) instance);
}

static void
initialize_default_ivs(VALUE klass, VALUE instance) {
VALUE super = RCLASS_SUPER(klass);
if (super) {
initialize_default_ivs(super, instance);
}

add_instance_method_list(klass, instance);
}

static VALUE
rb_class_instance_allocate_internal(VALUE klass, VALUE flags, bool wb_protected)
{
Expand All @@ -2895,6 +2928,8 @@ rb_class_instance_allocate_internal(VALUE klass, VALUE flags, bool wb_protected)
// SIZE_POOL_COUNT away from the root shape.
ROBJECT_SET_SHAPE_ID(obj, ROBJECT_SHAPE_ID(obj) + SIZE_POOL_COUNT);

initialize_default_ivs(klass, obj);

#if RUBY_DEBUG
RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
VALUE *ptr = ROBJECT_IVPTR(obj);
Expand Down

0 comments on commit 8e3fc8e

Please sign in to comment.