Refactor rb_shape_transition_frozen to return a shape_id.

This commit is contained in:
Jean Boussier 2025-05-08 19:05:03 +02:00
parent 31d0a5815c
commit f82523f14b
Notes: git 2025-05-09 08:23:10 +00:00
4 changed files with 21 additions and 21 deletions

View File

@ -519,12 +519,12 @@ rb_obj_clone_setup(VALUE obj, VALUE clone, VALUE kwfreeze)
}
if (RB_OBJ_FROZEN(obj)) {
rb_shape_t *next_shape = rb_shape_transition_shape_frozen(clone);
if (!rb_shape_obj_too_complex(clone) && rb_shape_too_complex_p(next_shape)) {
shape_id_t next_shape_id = rb_shape_transition_frozen(clone);
if (!rb_shape_obj_too_complex(clone) && rb_shape_id_too_complex_p(next_shape_id)) {
rb_evict_ivars_to_hash(clone);
}
else {
rb_shape_set_shape(clone, next_shape);
rb_shape_set_shape_id(clone, next_shape_id);
}
}
break;
@ -541,14 +541,14 @@ rb_obj_clone_setup(VALUE obj, VALUE clone, VALUE kwfreeze)
argv[1] = freeze_true_hash;
rb_funcallv_kw(clone, id_init_clone, 2, argv, RB_PASS_KEYWORDS);
RBASIC(clone)->flags |= FL_FREEZE;
rb_shape_t *next_shape = rb_shape_transition_shape_frozen(clone);
shape_id_t next_shape_id = rb_shape_transition_frozen(clone);
// If we're out of shapes, but we want to freeze, then we need to
// evacuate this clone to a hash
if (!rb_shape_obj_too_complex(clone) && rb_shape_too_complex_p(next_shape)) {
if (!rb_shape_obj_too_complex(clone) && rb_shape_id_too_complex_p(next_shape_id)) {
rb_evict_ivars_to_hash(clone);
}
else {
rb_shape_set_shape(clone, next_shape);
rb_shape_set_shape_id(clone, next_shape_id);
}
break;
}

22
shape.c
View File

@ -700,28 +700,28 @@ rb_shape_transition_shape_remove_ivar(VALUE obj, ID id, rb_shape_t *shape, VALUE
return true;
}
rb_shape_t *
rb_shape_transition_shape_frozen(VALUE obj)
shape_id_t
rb_shape_transition_frozen(VALUE obj)
{
rb_shape_t *shape = rb_shape_get_shape(obj);
RUBY_ASSERT(shape);
RUBY_ASSERT(RB_OBJ_FROZEN(obj));
if (rb_shape_frozen_shape_p(shape)) {
return shape;
shape_id_t shape_id = rb_shape_get_shape_id(obj);
if (shape_id == ROOT_SHAPE_ID) {
return SPECIAL_CONST_SHAPE_ID;
}
rb_shape_t *next_shape;
rb_shape_t *shape = RSHAPE(shape_id);
RUBY_ASSERT(shape);
if (shape == rb_shape_get_root_shape()) {
return RSHAPE(SPECIAL_CONST_SHAPE_ID);
if (rb_shape_frozen_shape_p(shape)) {
return shape_id;
}
bool dont_care;
next_shape = get_next_shape_internal(shape, id_frozen, SHAPE_FROZEN, &dont_care, true);
rb_shape_t *next_shape = get_next_shape_internal(shape, id_frozen, SHAPE_FROZEN, &dont_care, true);
RUBY_ASSERT(next_shape);
return next_shape;
return rb_shape_id(next_shape);
}
static rb_shape_t *

View File

@ -165,7 +165,7 @@ bool rb_shape_id_too_complex_p(shape_id_t shape_id);
void rb_shape_set_shape(VALUE obj, rb_shape_t *shape);
rb_shape_t *rb_shape_get_shape(VALUE obj);
bool rb_shape_frozen_shape_p(rb_shape_t *shape);
rb_shape_t *rb_shape_transition_shape_frozen(VALUE obj);
shape_id_t rb_shape_transition_frozen(VALUE obj);
rb_shape_t *rb_shape_transition_shape_too_complex(VALUE obj);
bool rb_shape_transition_shape_remove_ivar(VALUE obj, ID id, rb_shape_t *shape, VALUE *removed);
rb_shape_t *rb_shape_get_next(rb_shape_t *shape, VALUE obj, ID id);

View File

@ -2010,14 +2010,14 @@ void rb_obj_freeze_inline(VALUE x)
RB_FL_UNSET_RAW(x, FL_USER2 | FL_USER3); // STR_CHILLED
}
rb_shape_t * next_shape = rb_shape_transition_shape_frozen(x);
shape_id_t next_shape_id = rb_shape_transition_frozen(x);
// If we're transitioning from "not complex" to "too complex"
// then evict ivars. This can happen if we run out of shapes
if (rb_shape_too_complex_p(next_shape) && !rb_shape_obj_too_complex(x)) {
if (rb_shape_id_too_complex_p(next_shape_id) && !rb_shape_obj_too_complex(x)) {
rb_evict_fields_to_hash(x);
}
rb_shape_set_shape(x, next_shape);
rb_shape_set_shape_id(x, next_shape_id);
if (RBASIC_CLASS(x)) {
rb_freeze_singleton_class(x);