summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <[email protected]>2023-11-16 17:50:21 +0100
committerJean Boussier <[email protected]>2023-11-17 09:19:21 +0100
commit94c9f166632a901e563463933efd42e618432d70 ()
tree948304c7e1b048d53cba547c49c0bac828066659
parent498b086c374608005278c0f7d105df1925e13a22 (diff)
Refactor rb_obj_evacuate_ivs_to_hash_table
That function is a bit too low level to called from multiple places. It's always used in tandem with `rb_shape_set_too_complex` and both have to know how the object is laid out to update the `iv_ptr`. So instead we can provide two higher level function: - `rb_obj_copy_ivs_to_hash_table` to prepare a `st_table` from an arbitrary oject. - `rb_obj_convert_to_too_complex` to assign the new `st_table` to the old object, and safely free the old `iv_ptr`. Unfortunately both can't be combined into one, because `rb_obj_copy_ivar` need `rb_obj_copy_ivs_to_hash_table` to copy from one object to another.
-rw-r--r--internal/variable.h3
-rw-r--r--object.c14
-rw-r--r--shape.c7
-rw-r--r--shape.h1
-rw-r--r--variable.c90
5 files changed, 57 insertions, 58 deletions
@@ -47,7 +47,8 @@ VALUE rb_mod_set_temporary_name(VALUE, VALUE);
struct gen_ivtbl;
int rb_gen_ivtbl_get(VALUE obj, ID id, struct gen_ivtbl **ivtbl);
-int rb_obj_evacuate_ivs_to_hash_table(ID key, VALUE val, st_data_t arg);
void rb_evict_ivars_to_hash(VALUE obj);
RUBY_SYMBOL_EXPORT_BEGIN
@@ -293,12 +293,10 @@ rb_obj_copy_ivar(VALUE dest, VALUE obj)
rb_shape_t * src_shape = rb_shape_get_shape(obj);
if (rb_shape_obj_too_complex(obj)) {
st_table * table = rb_st_init_numtable_with_size(rb_st_table_size(ROBJECT_IV_HASH(obj)));
-
- rb_ivar_foreach(obj, rb_obj_evacuate_ivs_to_hash_table, (st_data_t)table);
- rb_shape_set_too_complex(dest);
-
- ROBJECT(dest)->as.heap.ivptr = (VALUE *)table;
return;
}
@@ -328,10 +326,8 @@ rb_obj_copy_ivar(VALUE dest, VALUE obj)
shape_to_set_on_dest = rb_shape_rebuild_shape(initial_shape, src_shape);
if (UNLIKELY(rb_shape_id(shape_to_set_on_dest) == OBJ_TOO_COMPLEX_SHAPE_ID)) {
st_table * table = rb_st_init_numtable_with_size(src_num_ivs);
-
- rb_ivar_foreach(obj, rb_obj_evacuate_ivs_to_hash_table, (st_data_t)table);
- rb_shape_set_too_complex(dest);
- ROBJECT(dest)->as.heap.ivptr = (VALUE *)table;
return;
}
@@ -915,13 +915,6 @@ rb_shape_obj_too_complex(VALUE obj)
return rb_shape_get_shape_id(obj) == OBJ_TOO_COMPLEX_SHAPE_ID;
}
-void
-rb_shape_set_too_complex(VALUE obj)
-{
- RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
- rb_shape_set_shape_id(obj, OBJ_TOO_COMPLEX_SHAPE_ID);
-}
-
size_t
rb_shape_edges_count(rb_shape_t *shape)
{
@@ -226,7 +226,6 @@ rb_shape_t *rb_shape_traverse_from_new_root(rb_shape_t *initial_shape, rb_shape_
bool rb_shape_set_shape_id(VALUE obj, shape_id_t shape_id);
VALUE rb_obj_debug_shape(VALUE self, VALUE obj);
-void rb_shape_set_too_complex(VALUE obj);
// For ext/objspace
RUBY_SYMBOL_EXPORT_BEGIN
@@ -1371,55 +1371,59 @@ rb_attr_delete(VALUE obj, ID id)
}
void
-rb_evict_ivars_to_hash(VALUE obj)
{
RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
- st_table *table = st_init_numtable_with_size(rb_ivar_count(obj));
-
- // Evacuate all previous values from shape into id_table
- rb_ivar_foreach(obj, rb_obj_evacuate_ivs_to_hash_table, (st_data_t)table);
switch (BUILTIN_TYPE(obj)) {
- case T_OBJECT:
- rb_shape_set_too_complex(obj);
-
- if (!(RBASIC(obj)->flags & ROBJECT_EMBED)) {
- xfree(ROBJECT(obj)->as.heap.ivptr);
- }
-
- ROBJECT_SET_IV_HASH(obj, table);
- break;
- case T_CLASS:
- case T_MODULE:
- rb_shape_set_too_complex(obj);
-
- xfree(RCLASS_IVPTR(obj));
- RCLASS_SET_IV_HASH(obj, table);
- break;
- default:
- RB_VM_LOCK_ENTER();
- {
- struct st_table *gen_ivs = generic_ivtbl_no_ractor_check(obj);
- st_data_t old_ivtbl;
- struct gen_ivtbl *ivtbl = NULL;
-
- if (st_delete(gen_ivs, &obj, &old_ivtbl)) {
- ivtbl = (struct gen_ivtbl *)old_ivtbl;
- }
-
- ivtbl = xrealloc(ivtbl, sizeof(struct gen_ivtbl));
- ivtbl->as.complex.table = table;
#if SHAPE_IN_BASIC_FLAGS
- rb_shape_set_too_complex(obj);
#else
- ivtbl->shape_id = OBJ_TOO_COMPLEX_SHAPE_ID;
#endif
- st_insert(gen_ivs, (st_data_t)obj, (st_data_t)ivtbl);
- }
- RB_VM_LOCK_LEAVE();
}
RUBY_ASSERT(rb_shape_obj_too_complex(obj));
}
@@ -1637,12 +1641,18 @@ rb_ensure_iv_list_size(VALUE obj, uint32_t current_capacity, uint32_t new_capaci
}
int
-rb_obj_evacuate_ivs_to_hash_table(ID key, VALUE val, st_data_t arg)
{
st_insert((st_table *)arg, (st_data_t)key, (st_data_t)val);
return ST_CONTINUE;
}
static VALUE *
obj_ivar_set_shape_ivptr(VALUE obj, void *_data)
{